probably a dumb date question

Brad De Vries devriesbj
Thu Aug 18 07:13:57 PDT 2005


On 8/18/05, Roger Oberholtzer <roger at opq.se> wrote:
> 
> I need to get the date into a file as text for use in a C program, from
> a Makefile. Sounds simple enough:
> 
>         #define _MONTH 8
> 
> The date command has ways to print various values. However, it seems
> that to print the month as a number always has a leading 0. Day of the
> month has a format with and a format without the leading 0. But, oddly,
> not the month. For August, the date command insists on a leading 0.
> There is no octal 08...
> 
> This is the code snippet I am trying:
> 
>         echo "#define _MONTH `date "+%m"`"
> 
> This will not work as the C interpreter figures 08 is meant to be octal.
> 
> I then tried to see if I could get the shell to do this (not interpret
> the leading 0 as introducing an octal number):
> 
>         let x=`date "+%m"`; echo "#define _MONTH $$x"
> 
> Same problem. Not the shell complains in the let statement. It seems
> that every program is using scanf() to parse these, so the leading '0'
> forces an octal interpretation everywhere. I think that is correct. The
> culprit here is the date command, printing invalid numbers!
> 
> Is my coffee too weak this morning?

How about something like:

    let x=`date "+%m" | sed -e "s/^0//"`; echo "#define _MONTH $x"

(use 'sed' to strip any leading zeros)

Brad.



More information about the Linux-users mailing list