__DATE__ as ISO string

B

Ben Hetland

I want something like content of __DATE__ compiled into my binary.
(Being the date of compilation that is important.) However, I'd like the
compiled-in string to be in the ISO standard format (YYYY-MM-DD) instead
of the default "US" format (Mmm DD YYYY).

Does anyone know a simple, reasonable portable and standard way to
achieve this kind of string being produced at compile-time?

Example:

// I could have:
const char* build_date = __DATE__; // gives "Feb 18 2005" for example
//
// I want:
const char* iso_date = "2005-02-18"; // have to change manually :-(


Any ideas?


-+-Ben-+-
 
V

Victor Bazarov

Ben said:
I want something like content of __DATE__ compiled into my binary.
(Being the date of compilation that is important.) However, I'd like the
compiled-in string to be in the ISO standard format (YYYY-MM-DD) instead
of the default "US" format (Mmm DD YYYY).

Does anyone know a simple, reasonable portable and standard way to
achieve this kind of string being produced at compile-time?

Example:

// I could have:
const char* build_date = __DATE__; // gives "Feb 18 2005" for example
//
// I want:
const char* iso_date = "2005-02-18"; // have to change manually :-(


Any ideas?

Write a tool that would generate this line _at_the_build_moment_ and put
it into a separate file. Then include that file where you need it.

V
 
B

Ben Hetland

Victor said:
Write a tool that would generate this line _at_the_build_moment_ and put
it into a separate file. Then include that file where you need it.

Yes, fair enough, but I'd rather not rely on any extra tool in this
case, because this is part of a library/include set and is used by
numerous projects already. (It would require me to change the build
process of all projects that happen to use it, and also have a tool
built/installed for each platform & computer.)

BTW, I also need it to work for debug builds where optimization is
disabled. Other than this I was kinda hoping there's a "clever" macro or
template trick that can have the compiler do this directly... ;-) But
perhaps enabling optimization just for this code section can be forced
anyway if that can help the compiler emit the right bits.


-+-Ben-+-
 
V

Victor Bazarov

Ben said:
Yes, fair enough, but I'd rather not rely on any extra tool in this
case, because this is part of a library/include set and is used by
numerous projects already. (It would require me to change the build
process of all projects that happen to use it, and also have a tool
built/installed for each platform & computer.)

BTW, I also need it to work for debug builds where optimization is
disabled. Other than this I was kinda hoping there's a "clever" macro or
template trick that can have the compiler do this directly... ;-) But
perhaps enabling optimization just for this code section can be forced
anyway if that can help the compiler emit the right bits.

I honestly think you're better off with a tool simply because there isn't
any mechanism in the C preprocessor to manipulate strings. If you find
a setting or a macro in your compiler that allows you to get a different
format of the compilation date, it's likely not to be portable. OTOH, if
you don't care about being portable, talk to the compiler maker to see if
they are willing to accommodate your request and implement it into their
next release.

V
 
J

Johannes Bauer

Ben said:
// I could have:
const char* build_date = __DATE__; // gives "Feb 18 2005" for example
//
// I want:
const char* iso_date = "2005-02-18"; // have to change manually :-(

Why don't you parse the __DATE__ at runtime?

Greetings,
Johannes

--
PLEASE verify my signature. Some forging troll is claiming to be me.
My GPG key id is 0xCC727E2E (dated 2004-11-03). You can get it from
wwwkeys.pgp.net or random.sks.keyserver.penguin.de.
Also: Messages from "Comcast Online" are ALWAYS forged.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFCF0KXCseFG8xyfi4RAvJeAJ9BKeOcYWqOU/joiUTpB/a4SpYnVgCgg2I9
WkhjQX0AHW18iDdVv2WMnLQ=
=b3Q0
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top