From owner-freebsd-questions@FreeBSD.ORG Wed Aug 11 09:42:55 2004 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 0454916A4CE for ; Wed, 11 Aug 2004 09:42:55 +0000 (GMT) Received: from kane.otenet.gr (kane.otenet.gr [195.170.0.27]) by mx1.FreeBSD.org (Postfix) with ESMTP id 0842743D48 for ; Wed, 11 Aug 2004 09:42:53 +0000 (GMT) (envelope-from keramida@ceid.upatras.gr) Received: from orion.daedalusnetworks.priv (aris.bedc.ondsl.gr [62.103.39.226])i7B9gn1D032402; Wed, 11 Aug 2004 12:42:50 +0300 Received: from orion.daedalusnetworks.priv (orion [127.0.0.1]) i7B9gj45031362; Wed, 11 Aug 2004 12:42:46 +0300 (EEST) (envelope-from keramida@ceid.upatras.gr) Received: (from keramida@localhost)i7B9girb031361; Wed, 11 Aug 2004 12:42:44 +0300 (EEST) (envelope-from keramida@ceid.upatras.gr) Date: Wed, 11 Aug 2004 12:42:44 +0300 From: Giorgos Keramidas To: Mipam Message-ID: <20040811094244.GA30843@orion.daedalusnetworks.priv> References: <20040810171119.GA26303@orion.daedalusnetworks.priv> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: cc: freebsd-questions@freebsd.org Subject: Re: localtime question X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 11 Aug 2004 09:42:55 -0000 On 2004-08-11 10:44, Mipam wrote: > > You'd have to use strftime() and a local buffer for that. > > I found an example and adjusted it: > > #include > #include > > 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.