Skeith
Skeith

Reputation: 2542

Create __TIME__ and __DATE__ by hand

I have recently changed my compiler to the xc16 gcc compiler. This new compiler does not have the preprocessor macro __TIME__ defined.

All the research I have done leads me to explanations on how to use __TIME__ and that it's required by the standard, but nothing about how it works.

Currently, I use __TIME__ and __DATE__ to create a stamp on compile time. This is checked periodically and if it is older than the stamp on the software on the server, then the server downloads the new software to the device. Without __TIME__ I cannot implement our auto update system.

I imagine I would need something along the lines of #define TIME FunctionToGetCurrentTime but I am having no luck using time_t now = time(0); – clearly I was wrong, as this is the first time I've tried to write a preprocessor macro. I have only used #define for values.

Upvotes: 1

Views: 1846

Answers (1)

bodangly
bodangly

Reputation: 2624

Assuming you use bash (or another POSIX compliant shell):

Add to your gcc call the following

-D __DATE__=$(date "+%Y-%m-%d") -D __TIME__=$(date "+%H:%M:%S")

or some combination of flags to the date command.

Flags taken from pmg's comment. If you don't use a POSIX compliant shell that implements $() (all modern ones I can think of do), you can use backticks instead ``.

Upvotes: 4

Related Questions