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

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


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


You could use macros like __DATE__ and __TIME__. This is also portable for Visual Studio (see http://msdn.microsoft.com/en-us/library/b0084kay(v=vs.80).aspx)


You could always write a 5-line program that calls time and strftime and outputs that to a file, rather than using sh. [I have used this approach for updating "build numbers" and/or "version number" in the past].