__TIME__ and __DATE macros in C

Hi,

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:

#define APP_COMPILE_DATE __DATE__
#define APP_COMPILE_TIME __TIME__

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:

printf("%s %s", APP_COMPILE_TIME, APP_COMPILE_DATE);

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?

Thanks
adamportAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

tomandCommented:

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

#define MyCompileTime APP_COMPILE_DATE __DATE__


...
printf(,..... MyCompileTime)

?
0
phoffricCommented:
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.
0
phoffricCommented:
>> 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.
0
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

Infinity08Commented:
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.
0
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?

Thanks
0
phoffricCommented:
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?
Yes.

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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.