PDA

View Full Version : Converting __DATE__ to ISO format



BobTheProg
27th October 2010, 11:56
From the "isn't that neat" department, and I hope it eventually helps somebody:


QString newDate =
QDate::fromString(__DATE__, "MMM dd yyyy").toString("yyyy-MM-dd");


--
Bob

igor
27th October 2010, 17:46
Why not say
QString newDate = QDate::currentDate().toString("yyyy-MM-dd"); ?
Can you guarantee that __DATE__ is defined ?

wysota
27th October 2010, 18:50
__DATE__ is defined by the compiler as the compilation date. QDate::currentDate() returns the runtime date not the compilation date.

igor
27th October 2010, 19:35
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() :cool:

Lesiok
28th October 2010, 10:23
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.

BobTheProg
29th October 2010, 22:49
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):


QLocale myLocale = QLocale::system();
QLocale::setDefault(QLocale::c()); -- using the shortest function name in the whole of Qt!
QDate::fromString(__DATE__, "MMM dd yyyy").toString(Qt::ISODate);
QLocale::setDefault(myLocale);

Lesiok
30th October 2010, 10:18
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 :


QDate CompilationDate( void )
{
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 )
rc = QDate( 1900,01,01 );
else
{
char year[5], day[3];

memcpy(year,compilation_date+7,4);
year[4] = 0x00;
memcpy(day,compilation_date+4,2);
day[2] = 0x00;

rc = QDate(atoi(year),i+1,atoi(day));
}

return rc;
}

caesarvanou
8th July 2011, 11:13
This sexy code is also working...



#include <QLocale>
#include <QDate>

QDate visualStudioCompilationDate(void)
{
QString strDate = __DATE__;
QStringList lstDate = strDate.split(QRegExp("\\s+"), QString::SkipEmptyParts);

QLocale us = QLocale("en_US");
int year = us.toDate(lstDate[2], "yyyy").year();
int month = us.toDate(lstDate[0], "MMM").month();
int day = us.toDate(lstDate[1], "d").day();

return QDate(year, month, day);
}

IMPOMEZIA
11th August 2011, 01:12
More simple code.

QLocale(QLocale::C).toDate(QString(__DATE__).simpl ified(), QLatin1String("MMM d yyyy"));

DavidCasper
9th December 2015, 14: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");
}