1

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.

Jongware
  • 22,200
  • 8
  • 54
  • 100
Skeith
  • 2,512
  • 5
  • 35
  • 57
  • 2
    Note that `__TIME__` and `__DATE__` are not functions, they are *macros*, and whose replacement is a constant string literal. – Some programmer dude May 23 '16 at 11:37
  • I think you may be mixing up two quite different things. (1) Determining the current time *at run time*. (2) Determining when the program was built. #2 is what `__TIME__` is for. #1 is what `time()` is for. If you need one of them, the other will not help you at all. I bet what you actually want is #2. – Gareth McCaughan May 23 '16 at 11:37
  • 5
    `__DATE__` and `__TIME__` are the time at which the file was compiled, not the time at which the program is executed. If your compiler does not implement it, you're better off defining it from the command line with `-D`, constructing the actual string with some other program. – Quentin May 23 '16 at 11:37
  • 2
    `__DATE__` and `__TIME__` are macros defined at run time of the *preprocessor*. Trying to replace them with values computed at run time of the compiled *process* isn't going to work. – Andrew Henle May 23 '16 at 11:38
  • 4
    Looks like an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Please rewrite your question and just tell us what you **actually** try to achieve. – Jabberwocky May 23 '16 at 11:40
  • You could use a pre build step using a little program that generates an header file with those macro, e.g.: using [this post as example](http://stackoverflow.com/questions/1442116/how-to-get-date-and-time-value-in-c-program) – LPs May 23 '16 at 11:52
  • 2
    If a compiler does not provide the `__TIME__` macro, that compiler is not (configured as) a [Standard C11 compliant compiler](http://port70.net/~nsz/c/c11/n1570.html#6.10.8.1) or even [a C99 compliant compiler](http://port70.net/~nsz/c/c99/n1256.html#6.10.8). – pmg May 23 '16 at 14:39
  • @pmg: which is only a small consolation to OP. Skeith, I rewrote your question so it's clear what the problem is and what you tried. Agreed, or too radical? – Jongware May 23 '16 at 14:42
  • @RadLexus Thank you, your rewrite makes perfect sense. – Skeith May 23 '16 at 14:43

1 Answers1

4

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 ``.

bodangly
  • 2,473
  • 17
  • 28
  • 1
    `gcc -D __DATE__=\`date "+%Y-%m-%d"\` -D __TIME__=\`date "+%H:%M:%S"\`` works independent of bash: you can integrate this into sh scripts. – pmg May 23 '16 at 14:55