localtime question
Giorgos Keramidas
keramida at ceid.upatras.gr
Tue Aug 10 10:11:25 PDT 2004
On 2004-08-10 18:45, Mipam <mipam at 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
More information about the freebsd-questions
mailing list