1

Previously I had a question about __DATE__

I want to use __DATE__ to get the build time. The product will be compiled on systems with different locale. Is the format for __DATE__ always the same? My goal is to get the buildtime from __DATE__ and i want to make sure it works on any system. Currently I use:

QDateTime(QLocale("en_US").toDate(QString(__DATE__).simplified(), "MMM d yyyy")).toMSecsSinceEpoch();

To get a datetime of the buildtime. But is it possible that in cases this wont work, e.g. is it possible __DATE__ does not return Jul 14 2020 but in a local format e.g. chinese? If the last is the case the todate method will not work right?

Sven van den Boogaart
  • 11,833
  • 21
  • 86
  • 169

1 Answers1

3

From C++ standard (draft):

__DATE__

The date of translation of the source file: a character string literal of the form "Mmm dd yyyy", where the names of the months are the same as those generated by the asctime function, and the first character of dd is a space character if the value is less than 10. If the date of translation is not available, an implementation-defined valid date shall be supplied.

http://eel.is/c++draft/cpp#predefined-1.2

The last sentence gives some freedom for compiler what to do if the date is not available. GCC does this:

If GCC cannot determine the current date, it will emit a warning message (once per compilation) and __DATE__ will expand to "??? ?? ????".

https://gcc.gnu.org/onlinedocs/cpp/Standard-Predefined-Macros.html

To sum up - the format is fixed and it is always "Mmm dd yyy", using English names.

pklimczu
  • 626
  • 1
  • 6
  • 15
  • Thanks, but what if the locale is not english wont Mmm give the month based on the locale,. so instead of Jul δΈƒζœˆ if the locale is chinese? or will it always be given in english? – Sven van den Boogaart Jul 15 '20 at 11:02
  • 2
    https://en.cppreference.com/w/cpp/chrono/c/asctime `asctime` returns _three-letter English abbreviated month name_. There is always a chance that somebody will create his or her own compiler where the returned name will be in another language (like Chinese) but then, it is not a standard C++ anymore. – pklimczu Jul 15 '20 at 11:09