C ++ gets month as number at compile time

I have a C ++ project that should print a revision string. The audit string is set by the company, and the protocol includes build time as yyyy / mm / dd.

I use to indicate this as a macro from the build system, but this is no longer an option because the clutter fills the precompiled headers (in incremental builds when the day changes).

I am trying to implement this with getting the build date from the compiler, but __DATE__ and __TIMESTAMP__ specify the month in Mmm.

Any ideas how I can take a month as a number?


based on the answer below the version it ends with:

 #define __MONTH__ (\ __DATE__ [2] == 'n' ? (__DATE__ [1] == 'a' ? "01" : "06") \ : __DATE__ [2] == 'b' ? "02" \ : __DATE__ [2] == 'r' ? (__DATE__ [0] == 'M' ? "03" : "04") \ : __DATE__ [2] == 'y' ? "05" \ : __DATE__ [2] == 'l' ? "07" \ : __DATE__ [2] == 'g' ? "08" \ : __DATE__ [2] == 'p' ? "09" \ : __DATE__ [2] == 't' ? "10" \ : __DATE__ [2] == 'v' ? "11" \ : "12") ... std::string udate = __DATE__; std::string date = udate.substr(7, 4) + "/" + __MONTH__ + "/" + udate.substr(4, 2); boost::replace_all(date, " ", "0"); 

thanks

+4
source share
2 answers

I think the macro below matches your requirements. Here we are working on the 3rd letter of the month, since it is unique for most months (except for January / June, March / April), therefore it is easier to compare.

 #define MONTH (\ __DATE__ [2] == 'n' ? (__DATE__ [1] == 'a' ? 1 : 6) \ : __DATE__ [2] == 'b' ? 2 \ : __DATE__ [2] == 'r' ? (__DATE__ [0] == 'M' ? 3 : 4) \ : __DATE__ [2] == 'y' ? 5 \ : __DATE__ [2] == 'l' ? 7 \ : __DATE__ [2] == 'g' ? 8 \ : __DATE__ [2] == 'p' ? 9 \ : __DATE__ [2] == 't' ? 10 \ : __DATE__ [2] == 'v' ? 11 \ : 12) 
+7
source

This is similar, but the correct solution is Jun and Jan

 #define __MONTH__ (\ __DATE__[2] == 'n' ? (__DATE__[1] == 'a' ? "01" : "06") \ : __DATE__[2] == 'b' ? "02" \ : __DATE__[2] == 'r' ? (__DATE__[0] == 'M' ? "03" : "04") \ : __DATE__[2] == 'y' ? "05" \ : __DATE__[2] == 'l' ? "07" \ : __DATE__[2] == 'g' ? "08" \ : __DATE__[2] == 'p' ? "09" \ : __DATE__[2] == 't' ? "10" \ : __DATE__[2] == 'v' ? "11" \ : "12") 

If you find a mistake in your decision, IMPROVE me.

+1
source

All Articles