I'm trying to add the build date and time to my Qt 5.6 project file, so far I have added:
win32 {
DEFINES += BUILDTIME=\\\"$$system('echo %time%')\\\"
DEFINES += BUILDDATE=\\\"$$system('echo %date%')\\\"
} else {
DEFINES += BUILDTIME=\\\"$$system(date '+%H:%M')\\\"
DEFINES += BUILDDATE=\\\"$$system(date '+%d/%m/%y')\\\"
}
And in the source code:
QString strBuildDT = QString::fromLocal8Bit(BUILDDATE)
+ ", " + QString::fromLocal8Bit(BUILDTIME);
Using this as an example I would get:
12/10/16, 17:39
I would like to reformat the date to display:
12 October 2016, 17:39
From research it looks like the correct date format to use would be:
DEFINES += BUILDDATE=\\\"$$system(date '+%d %B %Y')\\\"
But this doesn't work and returns and empty string for BUILDDATE.
I found a mailing list thread about this. This works (the purpose of $$quote
is to prevent Qt from munging spaces, it actually should still produce a non-empty string without $$quote
, the real key is the outer \"
s)
DEFINES += \"BUILDDATE=\\\"$$quote($$system(date /T))\\\"\"
That works on Windows. I can't test on Linux right now but should be something like:
DEFINES += \"BUILDDATE=\\\"$$quote($$system(date '+%d %B %Y'))\\\"\"
This essentially puts quotes around the whole thing on the compiler command line and lets it work with spaces in the string. Example (Windows, mingw, Qt 4.8.1):
g++ ... -D"BUILDDATE=\"Wed 10/12/2016\"" ...
That said you still may want to just use date '+%s'
to get epoch time then format on display with a QDateTime
to use the current locale and timezone. Unfortunately, though, I do not know the command to get epoch time on Windows (cursory research does not bode well).