Monday, April 11, 2011

Print Date and Time In Visual Studio C++ build?

How would I print the date and time for the purposes of the build. Ie: When the console for my application starts up I want to do this:

Binary Build date: 03/03/2009 @ 10:00AM

I think this would be a super useful function for all applications to have behind the scenes for programmers, especially in a team environment.

Is there a simple way to do this using Visual Studio 2008 in C++. Thanks.

From stackoverflow
  • You can use the macros __TIME__ and __DATE__. Note the double underscores. These are unrolled at compile time and hence you will get the last compile time saved in your file(s).

  • One way of doing this would be using the built-in __DATE__ and __TIME__ macros. From MSDN (for VS 2005):

    __DATE__: The compilation date of the current source file. The date is a string literal of the form Mmm dd yyyy. The month name Mmm is the same as for dates generated by the library function asctime declared in TIME.H.

    __TIME__: The most recent compilation time of the current source file. The time is a string literal of the form hh:mm:ss.

  • Use preprocessor's __DATE__ and __TIME__.

    printf("Binary build date: %s @ %s\n", __DATE__, __TIME__);
    

    For making sure that cpp file that contains this code is really compiled, I use touch-utility for file as a pre-build step: touch file.cpp

    Touch.bat:

    @copy nul: /b +%1 tmp.$$$
    @move tmp.$$$ %1
    
  • Note that the time and date macros only work as desired if the particular file containing them is guaranteed to be compiled during every build.

  • Simple way to print date and time in VS VC++

    System::DateTime::Now
    

    This will print current Date and Time in the format 5/27/2009 2:41:56 PM

    Brock Woolf : maybe you should actually read the question and not just the question's title

0 comments:

Post a Comment