Questions about strptime()/strftime() [text format]

Richard Harvey Chapman hchapman@3gfp.com
Fri Jun 20 19:19:00 GMT 2008


(Sorry for the double post, but I didn't realize that Thunderbird 
defaults to HTML formatting for e-mails)

Hello all and thanks in advance.

I'm trying to transmit date and time in a user readable format from one
machine to another. My goal is a format like this:

"17:25:58 2008-06-20 -0500"


I am thinking of using the strftime() and strptime() functions except
that I don't get consistent results. I've attached a sample program and
included here the output on a recent 10.5.3 OS X machine and an Ubuntu
Linux machine using libc-2.3.6. The Linux version segfaults when
strftime() uses the tm struct that was filled using strptime().

Can someone suggest a reliable method for this?
Am I supposed to be initializing the tm struct?

OS X output:

harveybook:coding_tests hchapman$ make time_formats && ./time_formats
g++ -ggdb time_formats.cc -o time_formats
-----strftime(localtime(time())) output-----
Time now is "17:25:58 2008-06-20 -0500 UTC" (Local)
Time now is "17:25:58 2008-06-20 -0500 UTC" (GM)

-----printf() of strptime(strftime(...))-----
Reverse time is "17:25:58 2008-06-20 -0500 (null) DST" (Local)
Reverse time is "17:25:58 2008-06-20 -0500 (null) DST" (GM)

-----printf() of original tm structs used with strftime()-----
Reverse time is "17:25:58 2008-06-20 00000 UTC " (Local)
Reverse time is "17:25:58 2008-06-20 00000 UTC " (Local)

-----printf() of strftime(strptime(strftime(...)))-----
Time now is "17:25:58 2008-06-20 -0400 EDT" (Local)
Time now is "17:25:58 2008-06-20 " (GM)

-----Crude attempt to go back-and-forth using time() and ctime()-----
Time in seconds is "1213982758"
Time reversed is "Fri Jun 20 13:25:58 2008
"


Ubuntu output:

dev1:~# g++ -ggdb time_formats.cc -o time_formats && ./time_formats
-----strftime(localtime(time())) output-----
Time now is "17:25:03 2008-06-20 +0000 GMT" (Local)
Time now is "17:25:03 2008-06-20 +0000 GMT" (GM)

-----printf() of strptime(strftime(...))-----
Reverse time is
"17:25:03 2008-06-20 00000 U‰å]АU‰å]АU‰å‹U
Â
Òt‹E‹ DST" (Local)
Reverse time is "17:25:03 2008-06-20 00000 DST" (GM)

-----printf() of original tm structs used with strftime()-----
Reverse time is "17:25:03 2008-06-20 00000 GMT " (Local)
Reverse time is "17:25:03 2008-06-20 00000 GMT " (Local)

-----printf() of strftime(strptime(strftime(...)))-----
Time now is "17:25:03 2008-06-20 U‰å]АU‰å]АU‰å‹U
Â
Òt‹E‹" (Local)
Segmentation fault


-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: time_formats.cc
URL: <http://sourceware.org/pipermail/libc-alpha/attachments/20080620/0f0b18c0/attachment.cc>


More information about the Libc-alpha mailing list