QDateTime::fromstring( __DATE__, "MMM d yyyy") 返回无效 [英] QDateTime::fromstring( __DATE__, "MMM d yyyy") returns invalid
问题描述
使用 QDateTime::fromstring() 解析 MSVC++ 预定义的 __DATE__
(可能与 __TIME__
结合使用)宏不返回任何内容(= 无效的 QDateTime 对象).为什么?
Parsing the MSVC++ predefined __DATE__
(maybe in conjunction with __TIME__
) macro with QDateTime::fromstring() returns nothing (= an invalid QDateTime object). Why?
推荐答案
来自 http://msdn.microsoft.com/en-us/library/b0084kay%28v=vs.80%29.aspx:
DATE 当前源文件的编译日期.日期是 Mmm dd yyyy 形式的字符串文字.月份名称 Mmm 是与声明的库函数 asctime 生成的日期相同在 TIME.H.
DATE The compilation date of the current source file. The date is a string literal of the form Mmm dd yyyy. The month name Mmm is the same as for dates generated by the library function asctime declared in TIME.H.
dd
部分似乎填充了第 1..9 天的前导空格.
The dd
part seems to be filled with a leading space for days 1..9.
d the day as number without a leading zero (1 to 31)
dd the day as number with a leading zero (01 to 31)
一种解决方案可能是在解析之前从 __DATE__
字符串中删除重复的空格,例如使用 QString::replace(" "," ")
并用单个 d
解析当天.
One solution might be to remove duplicate spaces from the __DATE__
string prior to parsing, e.g. with QString::replace(" ", " ")
and parse the day with the single d
.
这篇关于QDateTime::fromstring( __DATE__, "MMM d yyyy") 返回无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!