Need hints on parsing time strings

tomas@tuxteam.de tomas@tuxteam.de
Tue Nov 2 15:37:00 GMT 2010


Hi,

I'm trying to parse date/time strings which look like:

    2009-04-14T13:00:00 CEST
    2009-04-14T13:00:00+0
    2009-04-14T13:00:00+Z
    2009-04-14T13:00:00CEST
    2009-04-14T13:00:00 CET

You get the idea. My questions:

 (Q1) Is strptime the right approach?
 (Q2) Does strptime help me with the timezone thing?

My approach at the moment is (yes, it is awkward, so I guess I'm doing
someething wrong):

  (1) parse the string first with "%FT%T%z" to see whether it "makes
      sense"

        strftime(datetime, "%FT%T%z", &tm);

  (2) re-parse it with ""%FT%T" to get hold of the time zone part

        char *tz = strftime(datetime, "%FT%T%z", &tm);

      (the return value of strftime should now point at (the beginning
      of) the assumed time zone part, right?)

  (3) Convert the whole struct tm to a time_t, whithin the time zone
      gathered in (2); that's achieved by temporarily setting the TZ
      environment variable to whatever timezone we have:

        char *oldtz = getenv("TZ");
        if(tz && *tz) setenv("TZ", tz, 1); else setenv("TZ, "", 1); /* want UTC as default */
        time_t tim = mktime(&tm);
	if(oldtz) setenv("TZ", oldtz, 1); else unsetenv("TZ");

        (This raises te next question:

  (Q3) How does that work in multithreaded environments?)

Needless to say, this doesn't always work: the timezone database has
entries for CET (for example), but not for CEST (I'm on some vanilla
GNU/Linux, more or less current Debian).

Attached is a little test program which does roughly what I outlined
above.

Am I missing something? Am I better off cooking up something by myself?

Thanks a million for any hints

Regards
-- tomás
-------------- next part --------------
A non-text attachment was scrubbed...
Name: tform.c
Type: text/x-csrc
Size: 2145 bytes
Desc: not available
URL: <http://sourceware.org/pipermail/libc-help/attachments/20101102/35c3cdad/attachment.bin>
-------------- next part --------------
2009-04-14T13:00:00 CEST
2009-04-14T13:00:00+0
2009-04-14T13:00:00+Z
2009-04-14T13:00:00CEST
2009-04-14T13:00:00Z
2009-04-14T25:62:+00:00
2009-04-14T12:00:00+00:00
2009-04-14T11:00+00:00
2009-04-14T13:00:00Z
2009-04-14T14:00+01:00
2008-07-19T17:28:27.650950+00:00
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: <http://sourceware.org/pipermail/libc-help/attachments/20101102/35c3cdad/attachment.sig>


More information about the Libc-help mailing list