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.

8
  • You can use #undef __DATE__ and #undef __TIME__ to undefine a macro so you can give it a different value. Commented Feb 15, 2021 at 13:19
  • 4
    All symbols with two leading underscores are reserved for the implementation. Creating a symbol like that is UB. source: timsong-cpp.github.io/cppwp/lex.name#3 Commented Feb 15, 2021 at 13:19
  • 4
    This is classic example of XY problem. Why do you need to do it? What is your actual goal? Commented Feb 15, 2021 at 13:22
  • 1
    If this problem is single event issue, then just change your system time for period of building. Best solution would be apply Jabberwockys answear. Commented Feb 15, 2021 at 13:59
  • 1
    You can try to use /u to undefine Microsoft-specific symbols that the compiler defines, see learn.microsoft.com/en-us/cpp/build/reference/… (I'm not sure if it works with standard macros like __DATE__ or only with additions made by MSVC). Commented Feb 15, 2021 at 13:59

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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 not sure because they wrote /D "__DATE__=\"Feb 10 2021\"" which is th exact useless format of __DATE__.
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:


But probably what you want is reproducible builds. It's very important nowadays so lots of projects are moving toward it including Windows. There are /experimental:deterministic, /d1nodatetime and /Brepro flags in MSVC for this

See also

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.