probably a dumb date question

Roger Oberholtzer roger
Thu Aug 18 08:53:39 PDT 2005


On Thu, 2005-08-18 at 14:49, Brad De Vries wrote:
> 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"

Bingo! Thanks.

+????????????????????????????+???????????????????????????????+
? Roger Oberholtzer          ?   E-mail: roger at opq.se        ?
? OPQ Systems AB             ?      WWW: http://www.opq.se/  ?
? Kapellgr?nd 7              ?                               ?
? P. O. Box 4205             ?    Phone: Int + 46 8   314223 ?
? 102 65 Stockholm           ?   Mobile: Int + 46 733 621657 ?
? Sweden                     ?      Fax: Int + 46 8   314223 ?
+????????????????????????????+???????????????????????????????+



More information about the Linux-users mailing list