Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 11 Aug 2004 10:44:34 +0200 (MEST)
From:      Mipam <mipam@ibb.net>
To:        Giorgos Keramidas <keramida@ceid.upatras.gr>
Cc:        freebsd-questions@freebsd.org
Subject:   Re: localtime question
Message-ID:  <Pine.BSO.4.56.0408111039230.11899@ux11.ltcm.net>
In-Reply-To: <20040810171119.GA26303@orion.daedalusnetworks.priv>
References:  <Pine.BSO.4.56.0408101646230.26666@ux11.ltcm.net><20040810162612 .GC25389@orion.daedalusnetworks.priv><Pine.BSO.4.56.0408101842210.21307@ux1 1.ltcm.net> <20040810171119.GA26303@orion.daedalusnetworks.priv>

next in thread | previous in thread | raw e-mail | index | archive | help
[SNIP]

> 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?

I changed the first according your advice:

#include <sys/time.h>
#include <err.h>
#include <stdio.h>

int main(void)
{
struct timeval tv;
struct timeval tv_current;
if (gettimeofday(&tv_current, NULL) == -1)
        err(1, "Could not get local time of day");
tv.tv_sec = tv_current.tv_sec-86400;
printf("%s", ctime((const time_t *) &tv.tv_sec));
/* printf("%s\n", ctime(&tv.tv_sec)); */
return 0;
}
Output: Tue Aug 10 10:42:26 2004

I compiled both with: cc -O3 -mcpu=pentiumpro -o time time.c
Both compile without errors.
Bye,

Mipam.



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.BSO.4.56.0408111039230.11899>