1

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.

SPlatten
  • 5,334
  • 11
  • 57
  • 128
  • 1
    That format string *should* be correct. What's the output of `date '+%d %B %Y'` when you run it on the command line? – Jason C Oct 12 '16 at 16:49
  • I agree from the command line it works, but nothing when used in the project file. – SPlatten Oct 12 '16 at 16:50
  • Also just out of curiosity does it work without spaces (I'm not in a place where I can try it right this moment) e.g. `+%d_%B_%Y`? Btw you could also consider `date +%s` then format it to the current locale and timezone at runtime. – Jason C Oct 12 '16 at 16:52
  • Yes, using '_' instead of spaces works...I'll try using the +%s – SPlatten Oct 12 '16 at 16:55
  • Would it recognise %20 instead of a space.....No :( – SPlatten Oct 12 '16 at 16:57
  • I'm confused. `system` does not return a string. It returns a status code. Does Qt capture the output of the spawned process and return it as a string for you? – Trevor Hickey Oct 12 '16 at 16:58
  • I found the original suggestion for this when doing a search for how to get the build date and time into your build....it does work, but not in the using the format I would like. – SPlatten Oct 12 '16 at 16:59
  • 1
    I am certain that it's an issue with the spaces (I opened up the laptop and am testing with Qt now but I'm on Windows so can't get your test exactly). I'm able to reproduce the issue, and I'm trying all sorts of combinations of quotes around things. So far I have not found a solution. I recommend using `+%s` for the time being and formatting internally with a `QDateTime`. I'll post an answer when I figure it out I'm sure it's documented somewhere or exists as a question here already. When you build the application, in the compile output tab, what is the compiler command it generates? – Jason C Oct 12 '16 at 17:01
  • Thank you, going with the +%s solution. – SPlatten Oct 12 '16 at 17:02

2 Answers2

1

Solution for RedHat:

DEFINES += BUILDDATE=\\\"$$system(date '+%s')\\\"

In code:

QString strBuildDT = QString::fromLocal8Bit(BUILDDATE);
QDateTime qDT = QDateTime::fromMSecsSinceEpoch(strBuildDT.toLong() * 1000);
strBuildDT = qDT.toString("dd MMMM yyyy, HH:mm");

This works well, thank you to https://stackoverflow.com/users/616460/jason-c for the suggestion to try +s

Community
  • 1
  • 1
SPlatten
  • 5,334
  • 11
  • 57
  • 128
1

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

Community
  • 1
  • 1
Jason C
  • 38,729
  • 14
  • 126
  • 182