More __DATE__ preprocessor tricks?

  • Thread starter Christopher Benson-Manica
  • Start date
C

Christopher Benson-Manica

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?
 
M

Mathew Hendry

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

Arthur J. O'Dwyer

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\00008\00009\00010\0"\
"11\00012\00013\00014\00015\00016\00017\00018\00019\00020\0"\
"21\00022\00023\00024\00025\00026\00027\00028\00029\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
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top