Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Robert Latest <boblatest@yahoo.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: time functions weirdness |
| Date | 2016-09-17 20:26 +0000 |
| Message-ID | <e45n6uF2va9U1@mid.individual.net> (permalink) |
| References | <e45d70Fk5lU1@mid.individual.net> <47758107-38f4-9571-76db-1e5991639649@verizon.net> |
James R. Kuyper wrote:
> strptime() is UNIX specific, but you'd run into similar problems if you
> used purely portable C code to parse the time string and load it into
> struct tm.
> Note that while your time string contains a time zone specifier (Z =>
> UTC), strptime() doesn't know anything about time zones. struct tm
> doesn't have a time zone field. The time string is therefore interpreted
> as local time, and tm is filled in accordingly.
>
Hi James, thanks for the clarification. I've come up with the following code
(that compiles cleanly with gcc -W -Wall -ansi -pedantic) in which I first
determine the beliefs of mktime() and use that to correct the timestamp of
the parsed struct tm.
BTW, what where the early C'ers smoking when they came up with the idea of
making the tm_mon field zero-based. Of course real coders always start
counting at zero, but then why does tm_mday start at 1?
Here's the code. It works with all dates that I've tested. Would this be
portable on any conforming implementation?
#include <time.h>
#include <stdio.h>
#define FORMAT "%Y-%m-%dT%H:%M:%SZ"
int isolocal2ts(const char *s, time_t *t) {
struct tm tm = {0};
int year, month;
if (6 == sscanf(s, "%d-%d-%dT%d:%d:%dZ",
&year, &month, &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec)) {
tm.tm_year = year-1900;
tm.tm_mon = month-1;
*t = mktime(&tm);
return 0;
} else {
return -1;
}
}
time_t ts_offset(void) {
struct tm o = {0};
o.tm_sec = 0; o.tm_min = 0; o.tm_hour = 0;
o.tm_mday = 1; o.tm_mon = 0; o.tm_year = 70;
return mktime(&o);
}
int main(void) {
time_t t, t_offset;
char buf[100];
char *orig_time = "2016-08-01T00:00:00Z";
t_offset = ts_offset();
if (0 != isolocal2ts(orig_time, &t)) {
fprintf(stderr, "Error during time conversion\n");
return 1;
}
t -= t_offset;
printf("Original timestr: %s\n", orig_time);
strftime(buf, sizeof buf, FORMAT, gmtime(&t));
printf("Converted back: %s\n", buf);
return 0;
}
--
robert
Back to comp.lang.c | Previous | Next — Previous in thread | 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