QDateTime::fromstring( __DATE__, “MMM d yyyy”) returns invalid

孤者浪人 提交于 2020-07-18 06:17:17

问题


Parsing the MSVC++ predefined __DATE__ (maybe in conjunction with __TIME__) macro with QDateTime::fromstring() returns nothing (= an invalid QDateTime object). Why?


回答1:


From http://msdn.microsoft.com/en-us/library/b0084kay%28v=vs.80%29.aspx:

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.

The dd part seems to be filled with a leading space for days 1..9.

QtDateTime::fromstring() only supports

d   the day as number without a leading zero (1 to 31)
dd  the day as number with a leading zero (01 to 31)

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.




回答2:


QLocale("en_US").toDate(QString(__DATE__).simplified(), "MMM d yyyy");                       


来源:https://stackoverflow.com/questions/17472735/qdatetimefromstring-date-mmm-d-yyyy-returns-invalid

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!