TAGS :Viewed: 16 - Published at: a few seconds ago

[ Format of CTIME output in Fortran ]

According to the current GCC documentation, when I call CTIME I should get a date formatted like this: "Sat Aug 19 18:13:14 1995". However, I'm running GCC 4.8.1 (MinGW) and the output I get is like this: "08/19/95 18:13:14". I actually have an ancient version of GCC (0.5) that I tested, and on the same machine that version formats the output correctly. Is there some way I can make CTIME output in the documented format, or am I stuck writing my own routine to get that format? I need that format thanks to some legacy code I'm dealing with.

Answer 1


First, please post GFortran bugs to the GCC bugzilla at https://gcc.gnu.org/bugzilla . There is no guarantee that bug reports posted to stackoverflow don't get lost in the noise. FWIW, I filed this bug at https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61310 .

For some background, this change in GFortran was due to an effort to get rid of thread-unsafe C library functions such as ctime(). POSIX provides a thread-safe variant called ctime_r(), which suffers from a number of issues such as, actually, different interfaces (some commercial unixes support functions based on drafts of the POSIX standard, where ctime_r has different parameters), bugs like running past the 26 bytes the spec says should be enough, and so on. In the end, POSIX 2008 gave up and marked ctime_r as obsolescent and recommended the use of strftime() instead. So GFortran followed this recommendation, after testing revealed that strftime() with the "%c" specifier generated strings in the same format as ctime(), as long as the program is in the default "C" locale. As GFortran itself never sets the locale (by calling setlocale), this was thus seen as good enough. Unfortunately, as you have found out, strftime() in MSVC libc doesn't generate a ctime()-like string for the %c specifier. Anyway, this should be fixed now, hopefully for good this time.

In any case, using the standard date_and_time would be much more robust than parsing the output of ctime.

Answer 2


From the link you gave:

[...] Unless the application has called setlocale, the output will be in the default locale, of length 24 and of the form ‘Sat Aug 19 18:13:14 1995’. In other locales, a longer string may result.

So the output depends on your current locale (and you probably have a different locale chosen on your machine than the guy that wrote the docs).