Convert __DATE__ to unsigned int

Discussion in 'C Programming' started by Erik Cato, Dec 12, 2003.

  1. Erik Cato

    Erik Cato Guest

    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
     
    Erik Cato, Dec 12, 2003
    #1
    1. Advertising

  2. Erik Cato

    Eric Sosman Guest

    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.

    --
     
    Eric Sosman, Dec 12, 2003
    #2
    1. Advertising

  3. (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
     
    Martin Dickopp, Dec 12, 2003
    #3
  4. 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
     
    Martin Ambuhl, Dec 12, 2003
    #4
  5. Erik Cato

    Eric Sosman Guest

    Martin Dickopp wrote:
    >
    > (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!

    --
     
    Eric Sosman, Dec 12, 2003
    #5
  6. 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.
     
    Jeremy Yallop, Dec 12, 2003
    #6
  7. Jeremy Yallop <> 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
     
    Martin Dickopp, Dec 12, 2003
    #7
  8. Erik Cato

    Erik Cato Guest

    Martin Dickopp <> wrote in message news:<brd518$mj4$01$-online.com>...
    > Jeremy Yallop <> 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
     
    Erik Cato, Dec 15, 2003
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Timo Freiberger
    Replies:
    3
    Views:
    973
    Bob Hairgrove
    Oct 30, 2004
  2. Siemel Naran

    convert signed int to unsigned int

    Siemel Naran, Nov 28, 2004, in forum: C++
    Replies:
    3
    Views:
    34,142
    Ioannis Vranos
    Nov 29, 2004
  3. er
    Replies:
    6
    Views:
    500
    Andre Kostur
    Sep 14, 2007
  4. ciccio

    int*unsigned int = unsigned?

    ciccio, Jun 4, 2010, in forum: C++
    Replies:
    2
    Views:
    417
    Öö Tiib
    Jun 4, 2010
  5. pozz
    Replies:
    12
    Views:
    759
    Tim Rentsch
    Mar 20, 2011
Loading...

Share This Page