Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

visual-c++

qt

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

like image 315
handle Avatar asked Mar 24 '23 07:03

handle


2 Answers

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.

like image 153
handle Avatar answered Apr 06 '23 08:04

handle


QLocale("en_US").toDate(QString(__DATE__).simplified(), "MMM d yyyy");                       
like image 26
Jimmytaker Avatar answered Apr 06 '23 08:04

Jimmytaker