View previous topic :: View next topic |
Author |
Message |
lucasromeiro
Joined: 27 Mar 2010 Posts: 167
|
Problem with time.c |
Posted: Tue May 31, 2016 6:44 pm |
|
|
Hello guys.
I have a problem with the library time.c
I convert the time in seconds and then convert again in date.
but this padou conversion function yesterday!
when he entered the May 30 began to give trouble!
when it will convert the date the month field comes as (0)
Code: |
ds1307_get_date( &_dia, &_mes, &_ano, &_dow);
ds1307_get_time( &_hora, &_min, &_seg);
struct_tm tmUTC;
tmUTC.tm_sec = _seg;
tmUTC.tm_min = _min;
tmUTC.tm_hour = _hora;
tmUTC.tm_mday = _dia;
tmUTC.tm_mon = _mes;
tmUTC.tm_year = (2000 + _ano)-1900;
tmUTC.tm_wday = 0;
tmUTC.tm_yday = 0;
//time_t timestamp = 0;
timestamp = mktime(&tmUTC);
struct_tm tmUTC1;
tmUTC1=localtime(×tamp);
int String_data[12];
String_data[0]=tmUTC1.tm_mday/10;
String_data[1]=tmUTC1.tm_mday - (String_data[0]*10);
String_data[2]=tmUTC1.tm_mon/10;
String_data[3]=tmUTC1.tm_mon - (String_data[2]*10);
String_data[4]=(tmUTC1.tm_year-100)/10;
String_data[5]=(tmUTC1.tm_year-100) - (String_data[4]*10);
String_data[6]=tmUTC1.tm_hour/10;
String_data[7]=tmUTC1.tm_hour - (String_data[6]*10);
String_data[8]=tmUTC1.tm_min/10;
String_data[9]=tmUTC1.tm_min - (String_data[8]*10);
String_data[10]=tmUTC1.tm_sec/10;
String_data[11]=tmUTC1.tm_sec - (String_data[10]*10);
Printf("\r\nTimestamp:<%Lu>",timestamp);
Printf("\r\ndia:<%d>",tmUTC1.tm_mday);
Printf("\r\nString:<%d%d%d%d%d%d%d%d%d%d%d%d>", String_data[0], String_data[1], String_data[2], String_data[3], String_data[4], String_data[5], String_data[6], String_data[7], String_data[8], String_data[9], String_data[10], String_data[11]);
delay_ms(1000);
|
RESULT:
Timestamp:<1464780189>
day:<0>
String:<000516112309> |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue May 31, 2016 10:55 pm |
|
|
Your post wasn't very helpful. You gave us this code where you are
filling in the tmUTC structure with values. Then we are supposed to
crank it through the time.c functions and see the result.
But in the structure below, you don't tell us what numeric values are in
_seg, _min, _hora, etc.
Quote: | tmUTC.tm_sec = _seg;
tmUTC.tm_min = _min;
tmUTC.tm_hour = _hora;
tmUTC.tm_mday = _dia;
tmUTC.tm_mon = _mes;
tmUTC.tm_year = (2000 + _ano)-1900;
tmUTC.tm_wday = 0;
tmUTC.tm_yday = 0;
//time_t timestamp = 0; |
You said you get this result:
Quote: | Timestamp:<1464780189> |
But we can't duplicate your problem because you didn't give us your
input values !
And equally important, we need to know your PIC and your CCS compiler
version. Then we can re-create your test environment and possibly work
on your problem. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Wed Jun 01, 2016 12:24 am |
|
|
The problem though is a misunderstanding of pointers....
tmUTC1=localtime(×tamp);
Wrong.....
localtime does not return a variable of type struct tm.
It returns a _pointer_ to such a variable.
You need to declare:
Code: |
struct_tm * tmUTC1;
tmUTC1=localtime(×tamp);
//then use the variable as:
tmUTC1->tm_mday //etc.
|
Fundamentally wrong types involved..... |
|
|
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
|
Posted: Wed Jun 01, 2016 4:07 am |
|
|
Another thing to think about:
Code: | /* Converts the given calendar time (in seconds) to local time
* and sets this time in the global g_lTime
* Returns a pointer to g_lTime
*/
struct_tm * localtime(time_t * timer) |
This means that every time this routine is called, it returns the same pointer, to a global, that is changed. So, the values pointed to are only valid until the next call of localtime(). |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9225 Location: Greensville,Ontario
|
|
Posted: Wed Jun 01, 2016 5:19 am |
|
|
this confuses me...
. yes, it doesn't take much these days !
tmUTC.tm_year = (2000 + _ano)-1900;
Is there a real purpose to add 200 then subtract 1900 ? Why not just add 100 ??
Jay |
|
|
lucasromeiro
Joined: 27 Mar 2010 Posts: 167
|
|
Posted: Wed Jun 01, 2016 8:38 am |
|
|
PCM programmer wrote: | Your post wasn't very helpful. You gave us this code where you are
filling in the tmUTC structure with values. Then we are supposed to
crank it through the time.c functions and see the result.
But in the structure below, you don't tell us what numeric values are in
_seg, _min, _hora, etc.
Quote: | tmUTC.tm_sec = _seg;
tmUTC.tm_min = _min;
tmUTC.tm_hour = _hora;
tmUTC.tm_mday = _dia;
tmUTC.tm_mon = _mes;
tmUTC.tm_year = (2000 + _ano)-1900;
tmUTC.tm_wday = 0;
tmUTC.tm_yday = 0;
//time_t timestamp = 0; |
You said you get this result:
Quote: | Timestamp:<1464780189> |
But we can't duplicate your problem because you didn't give us your
input values !
And equally important, we need to know your PIC and your CCS compiler
version. Then we can re-create your test environment and possibly work
on your problem. |
Hello, I get it!
Thanks for explaining.
I forgot these details!
I use compiler: 5,008
My pic is: 18LF2685
I use the standard library time.c
In print (String) the data has the following format:
String: <00/05/16: 10/20/30>
day / month / year: hour / minute / second
(I put tabs for better understanding)
Below is an example with values:
Results obtained to date: 05.30.2016 - 10:20:30
Timestamp: <1464776430>
DayM: <0>
String: <000516102030>
Results obtained to date: 05.31.2016 - 10:20:30
Timestamp: <1464862830>
DayM: <1>
String: <010516102030>
Both dates go wrong!
I tested it on other dates and conversion out correct!
Code: |
While(true){
struct_tm tmUTC;
tmUTC.tm_sec = 30;
tmUTC.tm_min = 20;
tmUTC.tm_hour = 10;
tmUTC.tm_mday = 30; //30 or 31
tmUTC.tm_mon = 5;
tmUTC.tm_year = (2000 + 16)-1900;
tmUTC.tm_wday = 0;
tmUTC.tm_yday = 0;
//time_t timestamp = 0;
timestamp = mktime(&tmUTC);
struct_tm tmUTC1;
tmUTC1=localtime(×tamp);
int String_data[12];
String_data[0]=tmUTC1.tm_mday/10;
String_data[1]=tmUTC1.tm_mday - (String_data[0]*10);
String_data[2]=tmUTC1.tm_mon/10;
String_data[3]=tmUTC1.tm_mon - (String_data[2]*10);
String_data[4]=(tmUTC1.tm_year-100)/10;
String_data[5]=(tmUTC1.tm_year-100) - (String_data[4]*10);
String_data[6]=tmUTC1.tm_hour/10;
String_data[7]=tmUTC1.tm_hour - (String_data[6]*10);
String_data[8]=tmUTC1.tm_min/10;
String_data[9]=tmUTC1.tm_min - (String_data[8]*10);
String_data[10]=tmUTC1.tm_sec/10;
String_data[11]=tmUTC1.tm_sec - (String_data[10]*10);
Printf("\r\nTimestamp:<%Lu>",timestamp);
Printf("\r\nDayM:<%d>",tmUTC1.tm_mday);
Printf("\r\nString:<%d%d%d%d%d%d%d%d%d%d%d%d>", String_data[0], String_data[1], String_data[2], String_data[3], String_data[4], String_data[5], String_data[6], String_data[7], String_data[8], String_data[9], String_data[10], String_data[11]);
delay_ms(1000);
}
|
|
|
|
lucasromeiro
Joined: 27 Mar 2010 Posts: 167
|
|
Posted: Wed Jun 01, 2016 8:41 am |
|
|
Ttelmah wrote: | The problem though is a misunderstanding of pointers....
tmUTC1=localtime(×tamp);
Wrong.....
localtime does not return a variable of type struct tm.
It returns a _pointer_ to such a variable.
You need to declare:
Code: |
struct_tm * tmUTC1;
tmUTC1=localtime(×tamp);
//then use the variable as:
tmUTC1->tm_mday //etc.
|
Fundamentally wrong types involved..... |
I do not understand, how is wrong if all the dates work, except these two days?
understood?
looks the answer I posted just above. |
|
|
lucasromeiro
Joined: 27 Mar 2010 Posts: 167
|
|
Posted: Wed Jun 01, 2016 8:44 am |
|
|
temtronic wrote: | this confuses me...
. yes, it doesn't take much these days !
tmUTC.tm_year = (2000 + _ano)-1900;
Is there a real purpose to add 200 then subtract 1900 ? Why not just add 100 ??
Jay |
It is so because I was auditioning.
I left so to understand how it works inside the library. understood?
but it works the way you spoke! |
|
|
lucasromeiro
Joined: 27 Mar 2010 Posts: 167
|
|
Posted: Wed Jun 01, 2016 8:29 pm |
|
|
you saw the response? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Jun 01, 2016 10:15 pm |
|
|
I just did. I'm working on it right now. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Jun 01, 2016 11:26 pm |
|
|
I think your problem may be due to a misinterpretation of how months
are counted in time.c and time.h. The months start with January = 0.
So May is month 4 (not 5). See this reference:
http://pubs.opengroup.org/onlinepubs/007908775/xsh/time.h.html
Re-compile your code with 30 04 16 and 31 04 16. See what happens.
Also, you are using vs. 5.008. That version was released on
July 4, 2013. My vs. 5.059 compiler has this comment in time.h:
Quote: |
/// VERSION HISTORY
///
/// June 9 2015:
/// Fixed the struct_tm typedef so 'struct tm' would be legal syntax.
/// The parameters that CCS added to asctime() and ctime()
/// are now optional.
/// Fixed a bug where tm_mday was sometimes off by 1 day. |
The first version in which that bug fix would appear is vs. 5.047, released
on June 20, 2015. So your vs. 5.008 probably has that bug, whatever it
was. The fix could be found by comparing files between 5.008 and a
more modern version. |
|
|
lucasromeiro
Joined: 27 Mar 2010 Posts: 167
|
|
Posted: Thu Jun 02, 2016 8:31 am |
|
|
PCM programmer wrote: | I think your problem may be due to a misinterpretation of how months
are counted in time.c and time.h. The months start with January = 0.
So May is month 4 (not 5). See this reference:
http://pubs.opengroup.org/onlinepubs/007908775/xsh/time.h.html
Re-compile your code with 30 04 16 and 31 04 16. See what happens.
Also, you are using vs. 5.008. That version was released on
July 4, 2013. My vs. 5.059 compiler has this comment in time.h:
Quote: |
/// VERSION HISTORY
///
/// June 9 2015:
/// Fixed the struct_tm typedef so 'struct tm' would be legal syntax.
/// The parameters that CCS added to asctime() and ctime()
/// are now optional.
/// Fixed a bug where tm_mday was sometimes off by 1 day. |
The first version in which that bug fix would appear is vs. 5.047, released
on June 20, 2015. So your vs. 5.008 probably has that bug, whatever it
was. The fix could be found by comparing files between 5.008 and a
more modern version. |
Hello,
I do not think the problem is when january = 0.
because the library works properly every day, except the dates posted above.
I do not know when this event can be repeated or if it was only in these two days.
tries to repeat the situation in your compiler. Please!
I updated the library to the most current version. June 9, 2015.
but the error continues to be repeated! So in the new library they have not corrected this problem. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jun 02, 2016 9:18 am |
|
|
But months have different amounts of days in them. You can't give it
an invalid input. If you give time.c a value of 05 31, that's June 31.
But June only goes up to 30.
You cannot give invalid inputs to a function. |
|
|
|