Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to get the time (of the day) and date at time of compilation?

What are the possibilities to know at compile time the time and date (BUILDTIME) in order to include in the binaries (executable/libraries) the information about the moment in which they have been created, in a portable way?

We have currently a solution that uses sh.exe, and requires to install msys under Windows, but I was wondering if it was possible doing without it.

Searching things like "build time/date", "compile time/date" did not lead to any relevant result.

Edit:

When I got to know about __TIME__, it was then possible to find this question had previously been asked: Recording the time when you compile a source

like image 414
Antonio Avatar asked Jun 25 '13 08:06

Antonio


1 Answers

The standard macros __DATE__ and __TIME__ do the job.

Be careful that this will provide you the compilation date of the file where they are used. Not the link date. Thus, you have to touch the file each time it is build, or do a pre-build step in MSVC.

The C99 standard says:

6.10.8 Predefined macro names

The following macro names shall be defined by the implementation:

  • __DATE__: The date of translation of the preprocessing translation unit: 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.
  • __TIME__: The time of translation of the preprocessing translation unit: a character string literal of the form "hh:mm:ss" as in the time generated by the asctime function. If the time of translation is not available, an implementation-defined valid time shall be supplied.

I copied the C99 text here, but these macros are much older than C99... I did not manage to find the standard text for older C...

like image 80
Matthieu Rouget Avatar answered Oct 17 '22 21:10

Matthieu Rouget