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

next in thread | previous in thread | raw e-mail | index | archive | help
On 2004-08-10 18:45, Mipam <mipam@ibb.net> wrote:
> > Try calling ctime() with the address of tv.tv_sec:
> >
> >         printf("%s\n", ctime(&tv.tv_sec));
>
> #include <stdio.h>
> #include <sys/time.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\n", ctime(&tv.tv_sec));
> return 0;
> }
>
> Does the job, thanks!
> At compile time i get:
>
> tijd.c: In function `main':
> tijd.c:11: warning: passing arg 1 of `ctime' from incompatible pointer type

That's because struct timeval's members are `long' not time_t.

    printf("%s\n", ctime((const time_t *) &tv.tv_sec));

Add <err.h> to your includes too and sort them in this order:

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

System headers first, the rest afterwards; each list sorted
alphabetically unless there is a good reason to do otherwise.

This becomes handy when there are long lists of headers as it's easy to
spot and fix duplicate includes :-)

> Only thing left is another format like: 2004 Aug  9 18:44:04
> Any hints?

You'd have to use strftime() and a local buffer for that.

Cheers



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