0

Is there any possibility to redefine __DATE__ and __TIME__ predefined C++ macros (give them other than default values) in Microsoft Visual Studio?

I tried compiler option /D "__DATE__=\"Feb 10 2021\"" but I'm getting:

pch.cpp : warning C4117: macro name '__DATE__' is reserved, '#define' ignored

and no effect. Any idea except modification of the code (or a confirmation 'it is not possible')?

Reason: C++ project, that I have, has its builds versioned by date macro values (every build gets its version from __DATE__/__TIME__ values). I need to simulate an "older" build - basically, to cheat this versioning system. I don't need to change the macro value format. I also know about an option to have another user-defined macro mentioned by @Jabberwocky.

phuclv
  • 37,963
  • 15
  • 156
  • 475
Jarek C
  • 1,077
  • 6
  • 18
  • You can use `#undef __DATE__` and `#undef __TIME__` to undefine a macro so you can give it a different value. – Locke Feb 15 '21 at 13:19
  • 3
    All symbols with two leading underscores are reserved for the implementation. Creating a symbol like that is UB. source: https://timsong-cpp.github.io/cppwp/lex.name#3 – NathanOliver Feb 15 '21 at 13:19
  • 3
    This is classic example of [XY problem](http://xyproblem.info/). Why do you need to do it? What is your actual goal? – Marek R Feb 15 '21 at 13:22
  • @Locke no, this doesn't work either for the same reason – Jabberwocky Feb 15 '21 at 13:25
  • @MarekR, thanks for your point. Adding the reason. – Jarek C Feb 15 '21 at 13:36
  • I am curious about the actual goal but only because it sounds like saucy shenanigans. Is someone important using this to determine the compile date and you were supposed to do the work last week? Covering our tracks are we? ;) – JohnFilleau Feb 15 '21 at 13:57
  • 1
    If this problem is single event issue, then just change your system time for period of building. Best solution would be apply `Jabberwocky`s answear. – Marek R Feb 15 '21 at 13:59
  • 1
    You can try to use `/u` to undefine Microsoft-specific symbols that the compiler defines, see https://learn.microsoft.com/en-us/cpp/build/reference/u-u-undefine-symbols?view=msvc-160 (I'm not sure if it works with standard macros like `__DATE__` or only with additions made by MSVC). – icebp Feb 15 '21 at 13:59

2 Answers2

3

No you can't. But you don't need to either. Don't use __DATE__ but e.g. BUILD_DATE and add this:

#ifndef BUILD_DATE
#define BUILD_DATE __DATE__
#endif

And compile with:

/D "BUILD_DATE=\"Feb 10 2021\"" 

Then you get the exact behaviour you want.

But if you really can't replace __DATE__ with something of your own as suggested, you're out of luck.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • Probably they wanted a useful date format like for example in the form of integers, instead of C's useless `__DATE__` with months written as text. – Lundin Feb 15 '21 at 13:28
  • @Lundin not sure because they wrote `/D "__DATE__=\"Feb 10 2021\""` which is th exact useless format of `__DATE__`. – Jabberwocky Feb 15 '21 at 13:33
2

One way is to fake the time that's perceived by MSVC cl.exe. This can be done by running the compiler inside a virtual machine (like Windows Sandbox) with a different time, or use some 3rd party solution like RunAsDate to change the time of a process:

phuclv
  • 37,963
  • 15
  • 156
  • 475