Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preprocessor macro to remove code if compiled after a certain date

I would like three lines of code not to be included if compiled after a certain date. The reason being is they provide backwards compatibility. To support a staggered release between client and embedding it is required to be there now.

Once the next software release comes along this support is to be dropped to force customers to upgrade the embedded software. Since this is a few months away there is risk of these lines being forgotten.

So ideally I would like a

#if __DATE__ > MyDate
    code here
#endif

or something equivalent. Is there any way of doing this?

*The code is compiled with GCC

like image 381
Hector Avatar asked Dec 21 '22 00:12

Hector


1 Answers

This solution is specifically for Windows platform and is something I use in production.

I exploit the environment variable %DATE%, and in a batch file used to launch my IDE, I have VA_CURRENT_DATE=%DATE:~6,4%%DATE:~3,2%%DATE:~0,2% (which transforms to an ISO8601 date for my specific locale).

Then in my preprocessor definitions for my project I define VA_BUILD_DATE to VA_CURRENT_DATE

Then I have some code like:

long day = VA_BUILD_DATE;
long year = day / 10000;
day -= year * 10000;
long month = day / 100;
day -= month * 100;
like image 53
Bathsheba Avatar answered May 19 '23 02:05

Bathsheba