|
Home > Archive > Unix Programming > April 2005 > mktime() vs gmtime()
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
mktime() vs gmtime()
|
|
| joe@invalid.address 2005-04-23, 5:51 pm |
| This is primarily for Solaris 9, but I don't know why it would be
different for other systems.
I'm trying to take a string timestamp and convert it to local and UTC
time_t values. When deriving the UTC value I'm using the calculations
given in the POSIX spec, to avoid the local corrections made by
mktime().
I've probably just gotten myself confused, but I'm unsure about the
results I'm getting. For example
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
int main()
{
struct tm tmLocal, tmGMT;
time_t tLocal, tGMT;
const char *ts = "2005-04-03 03:00:00";
memset(&tmLocal, 0, sizeof tmLocal);
sscanf(ts, "%4d-%2d-%2d %2d:%2d:%2d",
&tmLocal.tm_year,
&tmLocal.tm_mon,
&tmLocal.tm_mday,
&tmLocal.tm_hour,
&tmLocal.tm_min,
&tmLocal.tm_sec);
tmLocal.tm_mon -= 1;
tmLocal.tm_year -= 1900;
tmLocal.tm_isdst = -1;
tLocal = mktime(&tmLocal);
printf("tLocal = %ld - %s", tLocal, ctime(&tLocal));
tmGMT = *gmtime(&tLocal);
tGMT = tmGMT.tm_sec +
tmGMT.tm_min*60 +
tmGMT.tm_hour*3600 +
tmGMT.tm_yday*86400 +
(tmGMT.tm_year-70)*31536000 +
((tmGMT.tm_year-69)/4)*86400 -
((tmGMT.tm_year-1)/100)*86400 +
((tmGMT.tm_year+299)/400)*86400;
printf("calculated tGMT = %ld - %s", tGMT, ctime(&tGMT));
putenv("TZ=GMT");
tzset();
printf("same thing w/ TZ=GMT = %ld - %s", tGMT, ctime(&tGMT));
}
Output on my computer
tLocal = 1112515200 - Sun Apr 3 03:00:00 2005
calculated tGMT = 1112515200 - Sun Apr 3 03:00:00 2005
same thing w/ TZ=GMT = 1112515200 - Sun Apr 3 08:00:00 2005
Given that I'm in US Central time, shouldn't the time_t value for UTC
be larger than the local value?
If not, what am I misunderstanding?
Thanks
Joe
| |
| James Antill 2005-04-23, 5:51 pm |
| On Sat, 23 Apr 2005 14:17:42 -0500, joe wrote:
> This is primarily for Solaris 9, but I don't know why it would be
> different for other systems.
>
> I'm trying to take a string timestamp and convert it to local and UTC
> time_t values. When deriving the UTC value I'm using the calculations
> given in the POSIX spec, to avoid the local corrections made by
> mktime().
mktime() doesn't return something in "localtime", it applies whatever
data is in the struct tm. So if you do...
time_t now = time(NULL);
struct tm *tmp = gmtime(&now);
tmp->tm_year = ... ;
/* ... */
tmp->tm_sec = ... ;
time_t t = mktime(tmp);
....you'll get the answer you want. The only other way is to set _all_ of
the struct tm fields ... however SuS3 allows the implementation to have
more fields than are in the std., and those fields will also need to be
set (this is true, for instance, on Linux/glibc).
--
James Antill -- james@and.org
Need an efficient and powerful string library for C?
http://www.and.org/vstr/
| |
| joe@invalid.address 2005-04-23, 5:51 pm |
| James Antill <james-netnews@and.org> writes:
> On Sat, 23 Apr 2005 14:17:42 -0500, joe wrote:
>
>
> mktime() doesn't return something in "localtime", it applies whatever
> data is in the struct tm. So if you do...
>
> time_t now = time(NULL);
> struct tm *tmp = gmtime(&now);
>
> tmp->tm_year = ... ;
> /* ... */
> tmp->tm_sec = ... ;
>
> time_t t = mktime(tmp);
>
> ...you'll get the answer you want. The only other way is to set
> _all_ of the struct tm fields ... however SuS3 allows the
> implementation to have more fields than are in the std., and those
> fields will also need to be set (this is true, for instance, on
> Linux/glibc).
Maybe I'm reading the following from the SUS mktime() man page wrong
"The relationship between the tm structure (defined in the <time.h>
header) and the time in seconds since the Epoch is that the result
shall be as specified in the expression given in the definition of
seconds since the Epoch (see the Base Definitions volume of IEEE
Std 1003.1-2001, Section 4.14, Seconds Since the Epoch) corrected
for timezone and any seasonal time adjustments, where the names in
the structure and in the expression correspond."
The bit about "... corrected for timezone and any seasonal time
adjustments ..." made me think that the time_t value was something
other than UTC. I do indeed see what I expect when I use mktime(), but
I was afraid I was seeing that due to using ctime() to print it
out. Thinking more about it, it looks like what it's saying is that it
uses the local timezone and seasonal adjustments to go from localtime
in the tm struct to UTC.
Thanks
Joe
| |
| joe@invalid.address 2005-04-24, 7:47 am |
| James Antill <james-netnews@and.org> writes:
> On Sat, 23 Apr 2005 14:17:42 -0500, joe wrote:
>
>
> mktime() doesn't return something in "localtime", it applies whatever
> data is in the struct tm. So if you do...
>
> time_t now = time(NULL);
> struct tm *tmp = gmtime(&now);
>
> tmp->tm_year = ... ;
> /* ... */
> tmp->tm_sec = ... ;
>
> time_t t = mktime(tmp);
>
> ...you'll get the answer you want. The only other way is to set _all_ of
> the struct tm fields ... however SuS3 allows the implementation to have
> more fields than are in the std., and those fields will also need to be
> set (this is true, for instance, on Linux/glibc).
What's still confusing me is that the time_t value for GMT doesn't
stay the same from one timezone to the next. It seems intuitive to me
that GMT should be an absolute value for any point in time, and
ctime() would take care of adjusting the display for local time.
Adjusting the test program I posted earlier to use gmtime() I get
output like the following. The code is now:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
int main(int argc, char *argv[])
{
struct tm tmLocal, tmGMT;
time_t tLocal, tGMT;
// Get local time_t adjusted for timezone & DST
memset(&tmLocal, 0, sizeof tmLocal);
sscanf(argv[1], "%4d%2d%2dT%2d%2d",
&tmLocal.tm_year,
&tmLocal.tm_mon,
&tmLocal.tm_mday,
&tmLocal.tm_hour,
&tmLocal.tm_min);
tmLocal.tm_mon -= 1;
tmLocal.tm_year -= 1900;
tmLocal.tm_sec = 0;
tmLocal.tm_isdst = -1;
tLocal = mktime(&tmLocal);
printf("tLocal = %ld - %s", tLocal, ctime(&tLocal));
tmGMT = *gmtime(&tLocal);
tmGMT.tm_isdst = -1;
tGMT = mktime(&tmGMT);
printf("tGMT = %ld - %s", tGMT, ctime(&tGMT));
}
Sample output:
$ TZ=Indian/Mahe a.out 20050403T030000
tLocal = 1112482800 - Sun Apr 3 03:00:00 2005
tGMT = 1112468400 - Sat Apr 2 23:00:00 2005
$ TZ=Asia/Calcutta a.out 20050403T030000
tLocal = 1112477400 - Sun Apr 3 03:00:00 2005
tGMT = 1112457600 - Sat Apr 2 21:30:00 2005
$ TZ=America/Chicago a.out 20050403T030000
tLocal = 1112515200 - Sun Apr 3 03:00:00 2005
tGMT = 1112533200 - Sun Apr 3 08:00:00 2005
Am I wrong to think that tGMT should always have the same value?
Thanks
Joe
| |
| joe@invalid.address 2005-04-24, 2:46 pm |
| One of the things I'm trying to understand, which I hope someone here
will straighten me out on, is whether or not it's possible to use
gmtime()/mktime() to arrive at an epoch time that can be converted to
the local time in other timezones by adding the appropriate offset.
For example, what I've got so far gives me results like
TZ = America/Chicago
local = 1112515200 - Sun Apr 3 03:00:00 2005
gmt = 1112533200 - Sun Apr 3 08:00:00 2005
TZ = America/Los_Angeles
local = 1112522400 - Sun Apr 3 03:00:00 2005
gmt = 1112547600 - Sun Apr 3 10:00:00 2005
TZ = America/Chicago
local = 1112522400 - Sun Apr 3 05:00:00 2005
gmt = 1112540400 - Sun Apr 3 10:00:00 2005
If the timestamp given for US/Central is GMT, then I should be able to
add the offset for America/Indianapolis to arrive at the time
there. That doesn't work though.
1112533200 - 1112515200 = 18000 (America/Chicago)
1112547600 - 1112522400 = 25200 (America/Los_Angeles)
So why can't I add the offset for America/Chicago to the epoch time
for America/LosAngeles to come up with the local time in
America/Chicago?
1112547600 - 18000 = 1112529600
So what does gmtime()/mktime() actually leave me with? If the result
from calling these actually gives me UTC, why can't I get the local
time for another timezone by adding the appropiate offset?
Indeed, this ties back into another question, why does
gmtime()/mktime() give me a different time_t value depending on the
local time? In the output above, there are two different time_t
values given for 10:00 GMT.
Does POSIX say something about this I'm missing?
Thanks
Joe
| |
| James Antill 2005-04-24, 5:48 pm |
| On Sun, 24 Apr 2005 07:55:10 -0500, joe wrote:
> What's still confusing me is that the time_t value for GMT doesn't
> stay the same from one timezone to the next. It seems intuitive to me
Yes, it does ... but what you are asking for is the GMT time at a
certain time in localtime() ... which obviously _must_ change, if the
timezones are different.
> that GMT should be an absolute value for any point in time, and
> ctime() would take care of adjusting the display for local time.
>
> Adjusting the test program I posted earlier to use gmtime() I get
> output like the following. The code is now:
>
> #include <stdio.h>
> #include <time.h>
> #include <stdlib.h>
> #include <errno.h>
> #include <string.h>
>
> int main(int argc, char *argv[])
> {
> struct tm tmLocal, tmGMT;
> time_t tLocal, tGMT;
>
> // Get local time_t adjusted for timezone & DST
> memset(&tmLocal, 0, sizeof tmLocal);
This line should be...
time_t now = time(NULL);
tmLocal = *localtime(&now);
....memset() is not a valid std. way to init. a struct tm.
> sscanf(argv[1], "%4d%2d%2dT%2d%2d",
Should check argc.
> &tmLocal.tm_year,
> &tmLocal.tm_mon,
> &tmLocal.tm_mday,
> &tmLocal.tm_hour,
> &tmLocal.tm_min);
I would also generally recommend against the *scanf() family of
functions, for instance note that you are passing an extra 0.
> tmLocal.tm_mon -= 1;
> tmLocal.tm_year -= 1900;
> tmLocal.tm_sec = 0;
> tmLocal.tm_isdst = -1;
>
> tLocal = mktime(&tmLocal);
> printf("tLocal = %ld - %s", tLocal, ctime(&tLocal));
>
> tmGMT = *gmtime(&tLocal);
> tmGMT.tm_isdst = -1;
This line altering tm_isdst should be removed.
> tGMT = mktime(&tmGMT);
> printf("tGMT = %ld - %s", tGMT, ctime(&tGMT));
> }
Should return EXIT_SUCCESS.
> Sample output:
>
> $ TZ=Indian/Mahe a.out 20050403T030000
> tLocal = 1112482800 - Sun Apr 3 03:00:00 2005
> tGMT = 1112468400 - Sat Apr 2 23:00:00 2005
>
> $ TZ=Asia/Calcutta a.out 20050403T030000
> tLocal = 1112477400 - Sun Apr 3 03:00:00 2005
> tGMT = 1112457600 - Sat Apr 2 21:30:00 2005
Note that the _localtime_ is the same, so the GMT _must_ be different.
Ie. you are saying, given a time of year=2005, month=04, mday=03, hour=03,
minute=00 in Indian/Mahe what would GMT be ... and again for locale ==
Asia/Calcutta.
--
James Antill -- james@and.org
Need an efficient and powerful string library for C?
http://www.and.org/vstr/
| |
| joe@invalid.address 2005-04-24, 5:48 pm |
| James Antill <james-netnews@and.org> writes:
> On Sun, 24 Apr 2005 07:55:10 -0500, joe wrote:
[...]
>
> Note that the _localtime_ is the same, so the GMT _must_ be
> different. Ie. you are saying, given a time of year=2005, month=04,
> mday=03, hour=03, minute=00 in Indian/Mahe what would GMT be ... and
> again for locale == Asia/Calcutta.
Ok, the light went on. I think I've got it now. Thanks
Joe
|
|
|
|
|