Date: Wed, 11 Aug 2004 12:42:44 +0300 From: Giorgos Keramidas <keramida@ceid.upatras.gr> To: Mipam <mipam@ibb.net> Cc: freebsd-questions@freebsd.org Subject: Re: localtime question Message-ID: <20040811094244.GA30843@orion.daedalusnetworks.priv> In-Reply-To: <Pine.BSO.4.56.0408111039230.11899@ux11.ltcm.net> References: <20040810171119.GA26303@orion.daedalusnetworks.priv> <Pine.BSO.4.56.0408111039230.11899@ux11.ltcm.net>
next in thread | previous in thread | raw e-mail | index | archive | help
On 2004-08-11 10:44, Mipam <mipam@ibb.net> wrote: > > You'd have to use strftime() and a local buffer for that. > > I found an example and adjusted it: > > #include <time.h> > #include <stdio.h> > > int main() > { > struct tm *ptr; > time_t tm; > char str[60]; > char str2[60]; > char str3[60]; > > tm = time(NULL)-86400; > ptr = localtime(&tm); > strftime(str ,100 , "%d",ptr); > strftime(str2 ,100 , "%m",ptr); > strftime(str3 ,100 , "%Y",ptr); > printf("%s %s %s\n",str3,str2,str); > > return 0; > } > > This runs just fine: 2004 08 10 > I dont know what the 100 is good for? It's the size of the buffer that strftime() gets as the first argument. In this case 100 is a bug waiting to happen, because the buffers are allocated with only 60 bytes of data. The manpage of strftime() explains what each argument is supposed to be. $ man strftime > I compiled both with: cc -O3 -mcpu=pentiumpro -o time time.c > Both compile without errors. Note that -O3 might be a it unsafe on FreeBSD. Even -O2 is not always a good idea.
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20040811094244.GA30843>