473,466 Members | 1,320 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

More __DATE__ preprocessor tricks?

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
2 9836
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

58
by: Jeff_Relf | last post by:
Hi Tom, You showed: << private const string PHONE_LIST = "495.1000__424.1111___(206)564-5555_1.800.325.3333"; static void Main( string args ) { foreach (string phoneNumber in Regex.Split...
11
by: Jonan | last post by:
Hello, For several reasons I want to replace the built-in memory management with some custom built. The mem management itlsef is not subject to my question - it's ok to the point that I have...
9
by: SerGioGio | last post by:
Hello, Is there a way to #define a (set of) macro that will turn: FUNC(a)(b)(c) into: func(a, b, c) regardless of the number of parameters? i.e: FUNC(a)(b)(c) ---> func(a, b, c)
5
by: Ben Hetland | last post by:
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...
7
by: Erik Cato | last post by:
Hi group! Anyone know a way to convert the __DATE__ predefined macro into a unsigned int representing the current date? It should be possible to make out what date it was from the beginning. My...
2
by: Jeffrey Bradshaw | last post by:
Are there compile date/time and file line equivalents for __DATE__ and __FILE__ in C#?
1
by: Sagaert Johan | last post by:
Hi Are there any predefined constants i could use in my C# code similar to __DATE__ __FILE__ __LINE__ in c++ ? Johan
31
by: Sam of California | last post by:
Is it accurate to say that "the preprocessor is just a pass in the parsing of the source file"? I responded to that comment by saying that the preprocessor is not just a pass. It processes...
1
by: Zero | last post by:
Hello all, is it possible to change the format of __FILE__ + __DATE__, for example instead of hh:mm:ss ss:mm:hh? Where can I get information on this topic?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.