在Fortran中输出CTIME的格式 [英] Format of CTIME output in Fortran

查看:202
本文介绍了在Fortran中输出CTIME的格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据目前的GCC文档,当我打电话给CTIME时,我应该得到日期格式如下:Sat Aug 19 18:13:14 1995。但是,我正在运行GCC 4.8.1(MinGW),我得到的输出是这样的:08/19/95 18:13:14。我实际上有一个我测试过的GCC(0.5)的古老版本,并且在同一台机器上版本格式化正确的输出。有没有什么办法可以让CTIME输出的文件格式,还是我坚持写我自己的例程,以获得该格式?我需要这种格式,这要归功于我正在处理的一些遗留代码。

解决方案

首先,请将GFortran的bug发布到GCC bugzilla在 https://gcc.gnu.org/bugzilla 。不能保证发布到stackoverflow的错误报告不会在噪音中丢失。 FWIW,我在 https://gcc.gnu.org/bugzilla/上提交了这个错误在一些背景下,GFortran的这个变化是由于努力摆脱线程不安全的C库函数如ctime()。 POSIX提供了一个称为ctime_r()的线程安全变体,它遇到了许多问题,如实际上不同的接口(一些商业unix支持基于POSIX标准草案的函数,其中ctime_r具有不同的参数),bug规范说26个字节应该足够了,依此类推。最后,POSIX 2008放弃了ctime_r标记,并推荐使用strftime()。所以GFortran遵循这个建议,经过测试发现,带有%c说明符的strftime()生成的字符串格式与ctime()相同,只要程序处于默认的C语言环境。由于GFortran本身从不设置语言环境(通过调用setlocale),所以这被认为是足够好的。不幸的是,正如你所发现的,MSVC libc中的strftime()不会为%c说明符生成类似ctime()的字符串。无论如何,现在这个问题应该得到解决,希望这次能够取得好成绩。 在任何情况下,使用标准的date_and_time都比解析ctime的输出更健壮。


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.

解决方案

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.

这篇关于在Fortran中输出CTIME的格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆