Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the UTC offset for __TIME__?

Tags:

time

c99

We are using __TIME__ to embed the time of compilation in a binary. Unfortunately, a time without a UTC Offset is meaningless. I don't see an obvious way to get the timezone that the compiler is running in. I suppose I could grab this from the configure script. But is there a better way?

If I do it from a configure script, I'm going to need to put that into a .h or .c file somehow, and do something intelligent if the file is not there.

like image 200
vy32 Avatar asked Nov 04 '22 22:11

vy32


1 Answers

you can use compiler Flags to pass that externally. Most compiler provide '-D' flag to define #define macros externally. Let's suppose you put the UTC information in an environment variable. Pass that environment variable as -D flag

set $myUTC="-7"
gcc -c mysource.cpp -DUTC=$myUTC

In your mysource.cpp use the macro UTC:

printf("Compiled at %d Offset %d", __TIME__, UTC);
like image 177
asami Avatar answered Dec 05 '22 01:12

asami