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