Results 1 to 10 of 10

Thread: Converting __DATE__ to ISO format

  1. #1
    Join Date
    Jan 2010
    Location
    UK
    Posts
    12
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Red face Converting __DATE__ to ISO format

    From the "isn't that neat" department, and I hope it eventually helps somebody:
    Qt Code:
    1. QString newDate =
    2. QDate::fromString(__DATE__, "MMM dd yyyy").toString("yyyy-MM-dd");
    To copy to clipboard, switch view to plain text mode 

    --
    Bob

  2. #2
    Join Date
    Jan 2007
    Posts
    30
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    MacOS X

    Default Re: Converting __DATE__ to ISO format

    Why not say
    Qt Code:
    1. QString newDate = QDate::currentDate().toString("yyyy-MM-dd");
    To copy to clipboard, switch view to plain text mode 
    ?
    Can you guarantee that __DATE__ is defined ?

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Converting __DATE__ to ISO format

    __DATE__ is defined by the compiler as the compilation date. QDate::currentDate() returns the runtime date not the compilation date.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Jan 2007
    Posts
    30
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    MacOS X

    Default Re: Converting __DATE__ to ISO format

    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()

  5. #5
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,525
    Thanked 279 Times in 274 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Converting __DATE__ to ISO format

    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.

  6. #6
    Join Date
    Jan 2010
    Location
    UK
    Posts
    12
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Converting __DATE__ to ISO format

    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):
    Qt Code:
    1. QLocale myLocale = QLocale::system();
    2. QLocale::setDefault(QLocale::c()); -- using the shortest function name in the whole of Qt!
    3. QDate::fromString(__DATE__, "MMM dd yyyy").toString(Qt::ISODate);
    4. QLocale::setDefault(myLocale);
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,525
    Thanked 279 Times in 274 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Converting __DATE__ to ISO format

    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:
    1. QDate CompilationDate( void )
    2. {
    3. char *compilation_date = __DATE__;
    4. char *months[] = {"Jan","Feb","Mar","Apr","May","Jun",
    5. "Jul","Aug","Sep","Oct","Nov","Dec",NULL};
    6. int i;
    7. QDate rc;
    8.  
    9. for( i = 0; months[i] != NULL; i++ )
    10. if( memcmp( compilation_date, months[i], 3 ) == 0 )
    11. break;
    12.  
    13. if( months[i] == NULL )
    14. rc = QDate( 1900,01,01 );
    15. else
    16. {
    17. char year[5], day[3];
    18.  
    19. memcpy(year,compilation_date+7,4);
    20. year[4] = 0x00;
    21. memcpy(day,compilation_date+4,2);
    22. day[2] = 0x00;
    23.  
    24. rc = QDate(atoi(year),i+1,atoi(day));
    25. }
    26.  
    27. return rc;
    28. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jul 2011
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Smile Re: Converting __DATE__ to ISO format

    This sexy code is also working...

    Qt Code:
    1. #include <QLocale>
    2. #include <QDate>
    3.  
    4. QDate visualStudioCompilationDate(void)
    5. {
    6. QString strDate = __DATE__;
    7. QStringList lstDate = strDate.split(QRegExp("\\s+"), QString::SkipEmptyParts);
    8.  
    9. QLocale us = QLocale("en_US");
    10. int year = us.toDate(lstDate[2], "yyyy").year();
    11. int month = us.toDate(lstDate[0], "MMM").month();
    12. int day = us.toDate(lstDate[1], "d").day();
    13.  
    14. return QDate(year, month, day);
    15. }
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Aug 2009
    Posts
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Converting __DATE__ to ISO format

    More simple code.
    Qt Code:
    1. QLocale(QLocale::C).toDate(QString(__DATE__).simplified(), QLatin1String("MMM d yyyy"));
    To copy to clipboard, switch view to plain text mode 
    Last edited by IMPOMEZIA; 11th August 2011 at 01:30.

  10. #10
    Join Date
    Feb 2013
    Posts
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Converting __DATE__ to ISO format

    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");
    }

Similar Threads

  1. Qt to Matlab (mat format) export data to Matlab .mat format -v4
    By windsword in forum Qt-based Software
    Replies: 4
    Last Post: 26th February 2013, 21:01
  2. converting image into monochrome format
    By aj2903 in forum Qt Programming
    Replies: 2
    Last Post: 13th November 2009, 16:53
  3. Converting UIC 2 .h & .Cpp
    By jibolso in forum Newbie
    Replies: 5
    Last Post: 5th September 2009, 14:28
  4. Converting C++ to Qt4
    By ComaWhite in forum Qt Programming
    Replies: 8
    Last Post: 11th July 2008, 09:33
  5. Converting my UI to Qt4
    By Honestmath in forum Qt Programming
    Replies: 1
    Last Post: 15th April 2006, 00:58

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.