Date bug? For me, August changes to September

Nils Kassube kassube at gmx.net
Sun Oct 13 07:11:46 UTC 2013


David Fletcher wrote:
> I find my curiosity aroused too. At the moment I'm working on a little
> project to interpret gpx track files from my Garmin GPS. The lines in
> the file that contain the time the track point was recorded are like
> this:-
> <time>2013-08-12T16:08:21Z</time>
> 
> Using code similar to that in a previous project,
> PointTimeStruct.tm_year = atoi(GFTag.c_str() + 6) - 1900;
> PointTimeStruct.tm_mon = atoi(GFTag.c_str() + 11);
> PointTimeStruct.tm_mday = atoi(GFTag.c_str() + 14);
> PointTimeStruct.tm_hour = atoi(GFTag.c_str() + 17);
> PointTimeStruct.tm_min = atoi(GFTag.c_str() + 20);
> PointTimeStruct.tm_sec = atoi(GFTag.c_str() + 23);
> PointTimeStruct.tm_isdst = 0;
> 
> NextPt->SecondsSinceEpoch = mktime(&PointTimeStruct);
> 
> where GFTag is a string object loaded with the above gpx <time> line.
> 
> Debug output of the structure elements assigned above shows
> Year    113
> Month   8
> Date    12
> Hour    16
> Minute  8
> Second  21
> so I'm picking out the correct data from the gpx file. I can't
> remember the reason for subtracting 1900 from the year,

>From the mktime man page:

| tm_year  
|  The number of years since 1900.

> but debug
> output showing NextPt->SecondsSinceEpoch is
> lat is  51.5798 degrees
> lon is  -1.82932 degrees
> ele is  87.836 metres
> Time is 1379002101 seconds
> 
> Now, if I put the time in seconds back into the date command that
> Johnny R was using I get:-
> dave at Tosh-NB520:~/code$ date -d @1379002101 +'%F %T'
> 2013-09-12 17:08:21
> 
> So, the hour has gone up by one, which I'm not too bothered about.

I suppose your local time is UTC+1 which would explain the difference of 
one hour. If you leave away the format string, the date command might 
even show the time zone used. As an alternative you might use the -u 
option to get a UTC output.

> Everything else is correct EXCEPT that August has changed into
> September! How the hell has that happened?

>From the mktime man page:

| tm_mon  
|  The number of months since January, in the range 0 to 11.

Therefore you should use this line to assign the month value:

PointTimeStruct.tm_mon = atoi(GFTag.c_str() + 11) - 1;


Nils





More information about the ubuntu-users mailing list