473,395 Members | 1,335 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Convert __DATE__ to unsigned int

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 idéa was this format: ((Year - 2000)*12 + (month - 1))*31 + day
So 12 Dec 2003 would result in: ((2003 - 2000)*12 + (12 - 1))*31 + 12 = 1469

<OT>
What im trying to accomplish is to display a number representing
the build date of the code in a 4-digit 7-segment display.
</OT>

Preferbly i would have a solution that does it at compile time?

Something like this:

#define DATE_AS_INT /* something nice here */

displayAsInt(DATE_AS_INT);

The next best thing is a function returning the int. But i cannot use
any library functions.

Like this

unsigned int getDateAsInt()
{
/* Code goes here */
}

displayAsInt(getDateAsInt());

The reason for the somewhat strange constraints is that im developing for
an embedded system and im having a little short on code memory.

Greatful for any suggestion!

//Erik
Nov 14 '05 #1
7 13544
Erik Cato wrote:

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 idéa was this format: ((Year - 2000)*12 + (month - 1))*31 + day
So 12 Dec 2003 would result in: ((2003 - 2000)*12 + (12 - 1))*31 + 12 =1469


You won't be able to do this with "preprocessor magic"
at compile time, because the preprocessor has no way to turn
"Dec" into 12.

You could write a run-time function that would derive the
desired number from the __DATE__ string, turning "Dec 12 2003"
into 12, 12, 2003 and then encoding it as desired.

Perhaps a better solution is to do neither of these, but
to write a "helper" program that runs early in your build
procedure. This program would write a #define directive to
a one-line .h file, which would then be #include'd in any
compilations that needed it. Hey, presto! no preprocessor
magic required, no run-time overhead, and you don't get into
ambiguous situations if the program is built from multiple
modules compiled on different days.

--
Er*********@sun.com
Nov 14 '05 #2
er*******@japro.se (Erik Cato) writes:
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 idéa was this format: ((Year - 2000)*12 + (month - 1))*31 + day
So 12 Dec 2003 would result in: ((2003 - 2000)*12 + (12 - 1))*31 + 12 = 1469

<OT>
What im trying to accomplish is to display a number representing
the build date of the code in a 4-digit 7-segment display.
</OT>

Preferbly i would have a solution that does it at compile time?

Something like this:

#define DATE_AS_INT /* something nice here */


Here is some code which does this. DATE_AS_INT expands to a constant
expression, so an optimizing compiler should be able to compute its
value at compile time.
#include <stdio.h>

#define YEAR ((((__DATE__ [7] - '0') * 10 + (__DATE__ [8] - '0')) * 10 \
+ (__DATE__ [9] - '0')) * 10 + (__DATE__ [10] - '0'))

#define MONTH (__DATE__ [2] == 'n' ? 0 \
: __DATE__ [2] == 'b' ? 1 \
: __DATE__ [2] == 'r' ? (__DATE__ [0] == 'M' ? 2 : 3) \
: __DATE__ [2] == 'y' ? 4 \
: __DATE__ [2] == 'n' ? 5 \
: __DATE__ [2] == 'l' ? 6 \
: __DATE__ [2] == 'g' ? 7 \
: __DATE__ [2] == 'p' ? 8 \
: __DATE__ [2] == 't' ? 9 \
: __DATE__ [2] == 'v' ? 10 : 11)

#define DAY ((__DATE__ [4] == ' ' ? 0 : __DATE__ [4] - '0') * 10 \
+ (__DATE__ [5] - '0'))

#define DATE_AS_INT (((YEAR - 2000) * 12 + MONTH) * 31 + DAY)

int main (void)
{
printf ("%d-%02d-%02d = %d\n", YEAR, MONTH + 1, DAY, DATE_AS_INT);
return 0;
}
Martin
Nov 14 '05 #3
Erik Cato wrote:
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 idéa was this format: ((Year - 2000)*12 + (month - 1))*31 + day
So 12 Dec 2003 would result in: ((2003 - 2000)*12 + (12 - 1))*31 + 12 = 1469

__DATE__ has the for "Mmm dd yyyy", so "12 Dec 2003" is not a possible
value. Get the number for the month by using strstr on an array containing
the month names as used by asctime (char monthname[] = "JanFebMarApr...)

This is a trivial exercise, and I'm sorry to say that your post does not
make it clear what your problem is, apart from your not having the form for
__DATE__ right and asking the preprocessor to do something for which it is
not designed.
Preferbly i would have a solution that does it at compile time?


Good luck. Looking up the number corresponding to the month seems a bit
difficult at compile time.


--
Martin Ambuhl

Nov 14 '05 #4
Martin Dickopp wrote:

er*******@japro.se (Erik Cato) writes:
Anyone know a way to convert the __DATE__ predefined macro into a
unsigned int representing the current date? It should

[...]
Here is some code which does this. DATE_AS_INT expands to a constant
expression, so an optimizing compiler should be able to compute its
value at compile time. [...]


Yikes! I stand corrected, and my hat's off to you!

--
Er*********@sun.com
Nov 14 '05 #5
Martin Dickopp wrote:
#define YEAR ((((__DATE__ [7] - '0') * 10 + (__DATE__ [8] - '0')) * 10 \
+ (__DATE__ [9] - '0')) * 10 + (__DATE__ [10] - '0'))

#define MONTH (__DATE__ [2] == 'n' ? 0 \
#define MONTH (__DATE__ [2] == 'n' ? (__DATE__ [1] == 'a' ? 0 : 5) \
: __DATE__ [2] == 'b' ? 1 \
: __DATE__ [2] == 'r' ? (__DATE__ [0] == 'M' ? 2 : 3) \
: __DATE__ [2] == 'y' ? 4 \
: __DATE__ [2] == 'n' ? 5 \
Also, delete the above line.
: __DATE__ [2] == 'l' ? 6 \
: __DATE__ [2] == 'g' ? 7 \
: __DATE__ [2] == 'p' ? 8 \
: __DATE__ [2] == 't' ? 9 \
: __DATE__ [2] == 'v' ? 10 : 11)

#define DAY ((__DATE__ [4] == ' ' ? 0 : __DATE__ [4] - '0') * 10 \
+ (__DATE__ [5] - '0'))

#define DATE_AS_INT (((YEAR - 2000) * 12 + MONTH) * 31 + DAY)


Jeremy.
Nov 14 '05 #6
Jeremy Yallop <je****@jdyallop.freeserve.co.uk> writes:
#define MONTH (__DATE__ [2] == 'n' ? 0 \


#define MONTH (__DATE__ [2] == 'n' ? (__DATE__ [1] == 'a' ? 0 : 5) \
: __DATE__ [2] == 'b' ? 1 \
: __DATE__ [2] == 'r' ? (__DATE__ [0] == 'M' ? 2 : 3) \
: __DATE__ [2] == 'y' ? 4 \
: __DATE__ [2] == 'n' ? 5 \


Also, delete the above line.
: __DATE__ [2] == 'l' ? 6 \
: __DATE__ [2] == 'g' ? 7 \
: __DATE__ [2] == 'p' ? 8 \
: __DATE__ [2] == 't' ? 9 \
: __DATE__ [2] == 'v' ? 10 : 11)


Oops, of course! Thanks for the correction.

Martin
Nov 14 '05 #7
Martin Dickopp <ex****************@zero-based.org> wrote in message news:<br*************@news.t-online.com>...
Jeremy Yallop <je****@jdyallop.freeserve.co.uk> writes:
#define MONTH (__DATE__ [2] == 'n' ? 0 \


#define MONTH (__DATE__ [2] == 'n' ? (__DATE__ [1] == 'a' ? 0 : 5) \
: __DATE__ [2] == 'b' ? 1 \
: __DATE__ [2] == 'r' ? (__DATE__ [0] == 'M' ? 2 : 3) \
: __DATE__ [2] == 'y' ? 4 \
: __DATE__ [2] == 'n' ? 5 \


Also, delete the above line.
: __DATE__ [2] == 'l' ? 6 \
: __DATE__ [2] == 'g' ? 7 \
: __DATE__ [2] == 'p' ? 8 \
: __DATE__ [2] == 't' ? 9 \
: __DATE__ [2] == 'v' ? 10 : 11)


Oops, of course! Thanks for the correction.

Martin


Thanks a lot for all answers. I suspected that something like this
could be done but could not find the solution. :-)

The idéa of making a helper program hadn´t occured for me before but
that
is an idéa worth looking into a little more. Thanks!
The only problem with that is how to make my compile and build
enviroment call
that program before every build. But i will surely look into it.

//Erik
Nov 14 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Goran | last post by:
Hi! I need to convert from a unsigned char array to a float. I don't think i get the right results in the program below. unsigned char array1 = { 0xde, 0xc2, 0x44, 0x23}; //I'm not sure in...
2
by: Christopher Benson-Manica | last post by:
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...
1
by: b83503104 | last post by:
When are they not consistent?
1
by: vsk | last post by:
Hai, I need to know how to convert an unsigned char array into hexstring. can anyone help me in this regard?. Thanks.
11
by: QQ | last post by:
I know a char is 2 bytes, the conversion is like byte byte_array = new byte; //Allocate double mem as that of char then for each char do byte = (byte) char & 0xff byte = (byte)( char >> 8 &...
1
by: War Eagle | last post by:
Is it possible to convert an unsigned long to byte array. For instance, unsigned long = 23480923 unsigned long longNumber; longNumber=23480923; // in binary = 1 0110 0110 0100 1010 0101 1011 //...
3
by: Mike Miller | last post by:
What is the best way to convert a managed unsigned int64 to an unsigned long? Basically I need to do the following: System::UInt64 managedInt = 10; unsigned long unmanagedInt; unmanagedInt =...
4
by: techie | last post by:
I have defined a number of unsigned integer types as follows: typedef unsigned char uint8; typedef unsigned short uint16; typedef unsigned int uint32; typedfe long long uint64; Is it...
2
by: Bo Berglund | last post by:
I am trying to port code from Delphi to C++ as described in a previous post titled: "How to swap bytes in variables (VS2005)???" Now I have run into a bit of a snag due to my ignorance of C++, no...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
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...

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.