4

I have the following little program:

#include <iostream>
int main() {
   std::cout << "Time: " << __TIME__ << std::endl;
   std::cout << "Timestamp: " << __TIMESTAMP__ << std::endl;
   return 0;
}

which produces the following output:

Time and Timestamp output

As you can see, __TIME__ has a quite big difference with __TIMESTAMP__, it is 4 seconds ahead.

Why does this happen? How can there be such a difference between the two times?

Even the program's build time was less than 4 seconds, so where does this difference come from?

4

2 Answers 2

21

The two macros do entirely different things:


GCC

__TIME__

This macro expands to a string constant that describes the time at which the preprocessor is being run. The string constant contains eight characters and looks like "23:59:01". If GCC cannot determine the current time, it will emit a warning message (once per compilation) and __TIME__ will expand to "??:??:??".

(source)

__TIMESTAMP__

This macro expands to a string constant that describes the date and time of the last modification of the current source file. The string constant contains abbreviated day of the week, month, day of the month, time in hh:mm:ss form, year and looks like "Sun Sep 16 01:03:52 1973". If the day of the month is less than 10, it is padded with a space on the left.

If GCC cannot determine the current date, it will emit a warning message (once per compilation) and __TIMESTAMP__ will expand to "??? ??? ?? ??:??:?? ????".

(source)


Visual Studio

__TIME__: The most recent compilation time of the current source file. The time is a string literal of the form hh:mm:ss.
__TIMESTAMP__: The date and time of the last modification of the current source file, expressed as a string literal in the form Ddd Mmm Date hh:mm:ss yyyy, where Ddd is the abbreviated day of the week and Date is an integer from 1 to 31.

(source)

1
  • 2
    Ohhhhh this is interesting! Must be it! Dec 29, 2014 at 14:59
0
__TIME__

Time when the script started compiling according to the current locale.

__TIMESTAMP__

Date & time of the last modification to the script file according to the current locale.

5
  • 3
    5.2.6? 5.2.7? What are you referencing there? What is this funny ${} notation? Why did you write non-code in code formatting? Dec 29, 2014 at 15:00
  • @LightnessRacesinOrbit I just copied it from documentation...that funny ${} is also copied.
    – ravi
    Dec 29, 2014 at 15:01
  • 1
    What documentation? Cite your sources bro! Dec 29, 2014 at 15:01
  • 3
    I just listed the problems. Dec 29, 2014 at 15:05
  • 3
    You're still not citing any sources and you're still employing bizarre formatting to text that is not code! Dec 29, 2014 at 15:08

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.