From the "isn't that neat" department, and I hope it eventually helps somebody:
--
Bob
From the "isn't that neat" department, and I hope it eventually helps somebody:
--
Bob
Why not say?Qt Code:
To copy to clipboard, switch view to plain text mode
Can you guarantee that __DATE__ is defined ?
__DATE__ is defined by the compiler as the compilation date. QDate::currentDate() returns the runtime date not the compilation date.
Thanks for the clarification.
Now we have examples of both runtime and compile time.
You could easily do the same thing with __TIME__ and QTime::currentTime()
This is working ONLY if locale is some English language, because months name is in English. On all systems today __DATE__ is converted to string "Oct 28 2010". In example on system with Polish locale short name of October is Paź and QDate::fromString didn't recognise what is Oct.
Thanks Lesiok. I plead guilty to being excessively Anglo-centric.
Is there a modification that will work with, for instance, Polish? Perhaps something like this (has to be done in the master thread):
No, it is not working. I think that problem is with locale and system language. You can set ie. locale to US_english and language to Polish or something else. Locale defines format of string representing dat, money etc. Language defines names of months. String returned by __DATE__ is independent from locale and language settings.
I'm using this small function to convert __DATE__ to QDate :
Qt Code:
{ char *compilation_date = __DATE__; char *months[] = {"Jan","Feb","Mar","Apr","May","Jun", "Jul","Aug","Sep","Oct","Nov","Dec",NULL}; int i; QDate rc; for( i = 0; months[i] != NULL; i++ ) if( memcmp( compilation_date, months[i], 3 ) == 0 ) break; if( months[i] == NULL ) else { char year[5], day[3]; memcpy(year,compilation_date+7,4); year[4] = 0x00; memcpy(day,compilation_date+4,2); day[2] = 0x00; } return rc; }To copy to clipboard, switch view to plain text mode
This sexy code is also working...
Qt Code:
#include <QLocale> #include <QDate> { QString strDate = __DATE__; int year = us.toDate(lstDate[2], "yyyy").year(); int month = us.toDate(lstDate[0], "MMM").month(); int day = us.toDate(lstDate[1], "d").day(); }To copy to clipboard, switch view to plain text mode
More simple code.
Qt Code:
To copy to clipboard, switch view to plain text mode
Last edited by IMPOMEZIA; 11th August 2011 at 00:30.
If only this worked, it would be good. Unfortunately __DATE__ can return single digit days of month. So to really work you need:
QDate versionDate = QDate::fromString(__DATE__,"MMM dd yyyy");
if (!versionDate.isValid())
{
versionDate = QDate::fromString(__DATE__,"MMM d yyyy");
}
Bookmarks