By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,413 Members | 1,017 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,413 IT Pros & Developers. It's quick & easy.

More __DATE__ preprocessor tricks?

P: n/a
Recently a great example of converting __DATE__ into an integer via
preprocessor tricks was posted. I've been trying to accomplish
something similar - convert __DATE__ into a string in the format
YYYYMMDD. MONTH is easy - replace the numbers with strings and voila.
YEAR is also easy - __DATE__ + 7 works nicely. DAY, however, has me
stumped. There's got to be something better than

#define DAY ((__DATE__[4] == ' ' ? \
__DATE__[5] == '1' ? "01" : \
__DATE__[5] == '2' ? "02" : \
...
: __DATE__[4] == '1' ? ...

Right?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Fri, 26 Dec 2003 19:42:49 +0000 (UTC), Christopher Benson-Manica
<at***@nospam.cyberspace.org> wrote:
Recently a great example of converting __DATE__ into an integer via
preprocessor tricks was posted. I've been trying to accomplish
something similar - convert __DATE__ into a string in the format
YYYYMMDD. MONTH is easy - replace the numbers with strings and voila.
YEAR is also easy - __DATE__ + 7 works nicely. DAY, however, has me
stumped. There's got to be something better than

#define DAY ((__DATE__[4] == ' ' ? \
__DATE__[5] == '1' ? "01" : \
__DATE__[5] == '2' ? "02" : \
...
: __DATE__[4] == '1' ? ...

Right?


Lookup table?

"0\0001\0002\0003\0004\000..."

-- Mat.

Nov 14 '05 #2

P: n/a

On Sat, 27 Dec 2003, Mathew Hendry wrote:

On Fri, 26 Dec 2003, Christopher Benson-Manica wrote:
Recently a great example of converting __DATE__ into an integer via
preprocessor tricks was posted. I've been trying to accomplish
something similar - convert __DATE__ into a string in the format
YYYYMMDD. MONTH is easy - replace the numbers with strings and voila.
YEAR is also easy - __DATE__ + 7 works nicely. DAY, however, has me
stumped. There's got to be something better than

#define DAY ((__DATE__[4] == ' ' ? \
__DATE__[5] == '1' ? "01" : \
__DATE__[5] == '2' ? "02" : \


Lookup table?

"0\0001\0002\0003\0004\000..."


You mean something like

#define DIGIT(ch) ( ((ch)==' ')? 0: ((ch)-'0') )
#define DAYNO (10*DIGIT(__DATE__[4])+DIGIT(__DATE__[5]))
#define DAY \
("00\00001\00002\00003\00004\00005\00006\00007\000 08\00009\00010\0"\
"11\00012\00013\00014\00015\00016\00017\00018\0001 9\00020\0"\
"21\00022\00023\00024\00025\00026\00027\00028\0002 9\00030\00031" \
+ (3*DAYNO))
That's quite clever [IMHO]! It does suffer aesthetically in that
it requires storage for a 60-some-byte string literal no matter what
day it is (even though only three bytes of that string are relevant);
and it doesn't produce a result that could be used everywhere an
unadorned literal like "foo" could be used (i.e., no string-pasting
like 'puts("Today is " MONTH " the " DAY "th")') -- but then neither
does the original.

Incidentally, that's a heck of a lot of '0's in the source code
representation of a very simple string literal. Is there a shorter
way to represent a string literal like this one, all numerals and
embedded nulls?

-Arthur
Nov 14 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.