• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2152
  • Last Modified:

__TIME__ and __DATE macros in C


I would like to include the compile date and time into my C application. I am using the macros __DATE__ and __TIME__ at the minute but have run into some problems.

In one header file I have defined these:


I have three other source files in my project that include the header file above and use the variables directly. e.g. Each source file would contain:


My problem is that when I run the program, all of the calls to APP_COMPILE_TIME return a different time from eachother.

Is there a way to make APP_COMPILE_DATE and APP_COMPILE_TIME constant in the header file?

1 Solution

I am not C programmer but something like this is not working?

#define MyCompileTime APP_COMPILE_DATE __DATE__

printf(,..... MyCompileTime)

When each compilation unit (i.e., source code file) gets compiled and pulls in the common header, then the macros are preprocessed with the current time.

So, just put the macros in one .c file (say, the main), and set a const char * string to each one. The headers should declare these two stings as extern.
>> all of the calls to APP_COMPILE_TIME
When I think of "calls", I think of assembly code that the compiler has generated to a function.

But you can't call a macro (even if it looks like you are calling a macro - there are function like macros). The compiler never even sees a macro. There is a pre-processor step that occurs before the compiler gets in the act which converts macros into text. It is this transformed text that is sent to the compiler.
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

What phoffric described in http:#28967413, is a good approach to take, BUT there is a caveat : You need to make sure that that .c file is re-compiled every time you build the application (most build tools will not re-compile a file if it hasn't changed since the last compilation).

You'll also have to define which time you want to use. Is it the time you started the compilation ? Or the time the application is linked ? Or something else ?
And then use that consistently.
adamportAuthor Commented:
phoffric: could you explain your solution a little more

do I declare these as global variables:

const char * app_compile_date = __DATE__;
const char * app_compile_time = __TIME__;

and then use them in my other source files?

Or what was your method of declaring them as extern in the headers?

Yes, time is relative, so "use it consistently".

If the time is the time of the build, you can force (in *nix) with the appropriate touch commands in your makefile. By controlling the order of compilation you can force the macro to be processed either at the beginning or end of the build. A makefile can also pass in the macro value (using a -D option flag) if you want to compute the time in the makefile and pass it to the desired source code. (In that case you would not use __DATE__ and __TIME__ in the source code - you would use the macro from the makefile.

>> then use them in my other source files?

Here is an example of how it may look:
// in one .c file (say the one containing main function):
const char * app_compile_date = __DATE__;
const char * app_compile_time = __TIME__;

// in the header file:
extern char * app_compile_date;
extern char * app_compile_time;

// in your other files that include this header file
printf("%s %s", app_compile_date, app_compile_time);

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now