Subject: Re: libc timegm()
To: Wolfgang S. Rupprecht <wolfgang+gnus20031110T111227@wsrcc.com>
From: Luke Mewburn <lukem@NetBSD.org>
List: netbsd-users
Date: 11/11/2003 12:24:47
On Mon, Nov 10, 2003 at 11:22:11AM -0800, Wolfgang S. Rupprecht wrote:
|
| > On a related note, a function similar to tzset(3) that took a
| > timezone argument rather than requiring the $TZ environment to be
| > modified would allow mktime(3) to be used.
|
| I don't yet understand all that is going on, but
| /usr/src/lib/libc/time/localtime.c:mktime() sure looks more
| heavyweight than timegm() with extra locking and TZ string malloc-ing
| and copying. I'm not sure I really want to do that on a per
| invocation basis when I convert a few thousand lines in a text file
| that have a UTC timestamp up front.
Right, and that's what I used timegm() for.
In my case, I had a datestamp in the format that `date -u` returns.
I parse it with something like:
char *timestamp;
struct tm newtm;
time_t ntime;
timestamp = get_timestamp_string();
// timestamp is of the form:
// Tue Nov 11 01:24:38 UTC 2003
if (strptime(timestamp, "%a %b %d %T UTC %Y", &newtm) == NULL)
barf();
if ((ntime = timegm(&newtm)) == -1)
barf();
printf("parsed `%s' as %ld `%.24s'\n",
timestamp, (long)ntime, asctime(&newtm));
Cheers,
Luke.