3

I have a source file. When I compile the code, I want the executable file to remember when it was built. I am wondering if it is possible. For example:

 int main(){
    time_t t = ???  // Time when this line is compiled
    //print out value of t in certain format. 
    return t 
 } 
Flexo
  • 87,323
  • 22
  • 191
  • 272
Yan Zhu
  • 4,036
  • 3
  • 21
  • 37

4 Answers4

11

You can use the __TIME__ and __DATE__ macros to get the time the preprocessor ran at. It's a string, so yo need to convert it to a time_t from there.

A quick example I put together:

#include <time.h>
#include <iostream>
#include <cassert>

time_t build_time() {
  static const char *built = __DATE__" "__TIME__;  
  struct tm t;
  const char *ret = strptime(built, "%b %d %Y %H:%M:%S", &t);
  assert(ret);
  return mktime(&t);
}

int main() {
  std::cout << build_time() << std::endl;
}

I was a little worried about how this interacted with different locales, so I had a quick look in a recent C standard and found the following passage:

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

asctime is quite clear that:

... The abbreviations for the months are "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", and "Dec" ...

But %b of strptime() says:

%b or %B or %h

The month name according to the current locale, in abbreviated form or the full name.

So you need to be aware that this is making an assumption about what the locale will be set to at run time.

(You could in theory write a constexpr function or two to do that at compile time in C++11, but that's non-trivial to say the least!)

Community
  • 1
  • 1
Flexo
  • 87,323
  • 22
  • 191
  • 272
  • Code has 2 bug (even after 7 years). `struct tm t;` does not initialize its members and `strptime()` does not set them all, leading to potential invalid results with `mktime()`. Further the `__TIME__` is local time and uses local daylight time settings. `t.tm_isdst` is not initialized leading to a potential off by 1 hour in `mktime()`. Fix both issues with `struct tm t = { .tm_isdst = -1 };` – chux - Reinstate Monica Apr 12 '19 at 20:22
5

You can record the time as string through the __DATE__ and __TIME__ predefined macros.

If you want a time_t, you'll have to convert it at runtime.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
2

It is not perfectly addressing your problem, but in visual studio you can add a post built events. Add some console command like creating a new file or updating an existing one to see when it was last built successfully. I am doing this to copy my report files to the directory that I need them in. I just build my project and they all go there :)

Andrius Naruševičius
  • 8,348
  • 7
  • 49
  • 78
0

Read the last date and time modified properties of your executable in your code.

webgenius
  • 856
  • 3
  • 13
  • 30
  • This won't tell you when the source file was compiled. It tells you when the exe file was created. Perhaps nitpicky, but for large source bases there could be a lot of time between compiling the first CPP file and finally writing the EXE. – John Dibling May 04 '12 at 19:52
  • I believe that is what the OP wants when he says "I want the execute file to remember when it has been built". I might have mis-understood the question here, but I feel he wants to know the last time when the executable was prepared. – webgenius May 04 '12 at 19:55
  • Or option 2: Run a shell script from makefile. Just keep appending the current date and time values. With this you'll have a huge log, which can be read from your code if required. – webgenius May 04 '12 at 19:57
  • 2
    But if the executable file is copied, the copy may end up with a different timestamp (depending on the copy methodology). That fails to accurately report the time built. – dave May 04 '12 at 23:31