Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Robert Latest <boblatest@yahoo.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | time functions weirdness |
| Date | 2016-09-17 17:35 +0000 |
| Message-ID | <e45d70Fk5lU1@mid.individual.net> (permalink) |
Hello,
the task is simple: Convert a time string to a time_t and back. I'm not even
concerned with timezones, I want everything to be in UTC. But for some
reason it just doesn't work and I don't know why. Is it possible that
mktime()'s and gmtime()'s notions of the UNIX epoch differ by one hour?
My questions in detail:
1. Why
dh@dotcom:~/c/gpx$ gcc -W -Wall timetest.c && ./a.out
1. Original timestr: 1970-01-01T00:00:00Z
2. After mktime(): 1970-01-01T00:00:00Z
UNIX timestamp: -3600
3. gmtime() = 1969-12-31T23:00:00Z
dh@dotcom:~/c/gpx$ gcc -W -Wall timetest.c && ./a.out
1. Original timestr: 1970-08-01T00:00:00Z
2. After mktime(): 1970-08-01T00:00:00Z
UNIX timestamp: 18313200
3. gmtime() = 1970-07-31T23:00:00Z
dh@dotcom:~/c/gpx$ gcc -W -Wall timetest.c && ./a.out
1. Original timestr: 2016-08-01T00:00:00Z
2. After mktime(): 2016-08-01T01:00:00Z
UNIX timestamp: 1470006000
3. gmtime() = 2016-07-31T23:00:00Z
dh@dotcom:~/c/gpx$
#define _XOPEN_SOURCE /* for strftime */
#include <time.h>
#include <stdio.h>
#include <locale.h>
#define FORMAT "%Y-%m-%dT%H:%M:%SZ"
int main(void) {
struct tm tm = {0};
time_t t;
char buf[100];
char *orig_time = "2016-08-01T00:00:00Z";
setlocale(LC_ALL, "C"); /* doesn't make a difference */
strptime(orig_time, FORMAT, &tm);
printf("1. Original timestr: %s\n", orig_time);
tm.tm_isdst = 0; /* doesn't matter if I set this to 1
or to 0, or if I set it before or after mktime() */
t = mktime(&tm);
strftime(buf, sizeof buf, FORMAT, &tm);
printf("2. After mktime(): %s\n", buf);
printf(" UNIX timestamp: %d\n", (int) t);
strftime(buf, sizeof buf, FORMAT, gmtime(&t));
printf("3. gmtime() = %s\n", buf);
return 0;
}
Back to comp.lang.c | Previous | Next — Next in thread | Find similar | Unroll thread
time functions weirdness Robert Latest <boblatest@yahoo.com> - 2016-09-17 17:35 +0000
Re: time functions weirdness Robert Latest <boblatest@yahoo.com> - 2016-09-17 17:38 +0000
Re: time functions weirdness "James R. Kuyper" <jameskuyper@verizon.net> - 2016-09-17 14:26 -0400
Re: time functions weirdness Robert Latest <boblatest@yahoo.com> - 2016-09-17 20:26 +0000
Re: time functions weirdness "James R. Kuyper" <jameskuyper@verizon.net> - 2016-09-17 17:21 -0400
Re: time functions weirdness Keith Thompson <kst-u@mib.org> - 2016-09-17 16:54 -0700
Re: time functions weirdness "James R. Kuyper" <jameskuyper@verizon.net> - 2016-09-17 18:00 -0400
Re: time functions weirdness Ian Collins <ian-news@hotmail.com> - 2016-09-18 10:45 +1200
Re: time functions weirdness Keith Thompson <kst-u@mib.org> - 2016-09-17 17:24 -0700
Re: time functions weirdness Robert Latest <boblatest@yahoo.com> - 2016-09-18 06:25 +0000
Re: time functions weirdness Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-09-18 20:40 +0100
Re: time functions weirdness supercat@casperkitty.com - 2016-09-18 16:00 -0700
Re: time functions weirdness Keith Thompson <kst-u@mib.org> - 2016-09-17 17:04 -0700
csiph-web