P
Pietro Cerutti
Hi group,
#include <stdio.h>
#include <unistd.h>
#include <time.h>
int main(void)
{
time_t t1, t2;
char *st1, *st2;
t1 = time(NULL);
if(sleep(1)) {
printf("Signal received, exiting\n");
return (1);
}
t2 = time(NULL);
printf("t1 is %lu\n", (unsigned long)t1);
printf("t2 is %lu\n", (unsigned long)t2);
st1 = ctime(&t1);
st2 = ctime(&t2);
printf("st1 is %s", st1);
printf("st2 is %s", st2);
return(0);
}
The code above gives this output:
t1 is 1183506197
t2 is 1183506198
st1 is Wed Jul 4 01:43:18 2007
st2 is Wed Jul 4 01:43:18 2007
How can it happen that two outputs of ctime(3), given two different
inputs (t1 != t2) are the same (st1 == st2) ?
Moreover, on a shell console:
$ date -r 1183506197
Wed Jul 4 01:43:17 CEST 2007
$ date -r 1183506198
Wed Jul 4 01:43:18 CEST 2007
Here date gives a correct result (i.e. the first date is one second
before the second date).
I doubt it has something to do with the compiler grouping the two calls
to time(NULL). If it's the case, how can I avoid it?
Thank you.
#include <stdio.h>
#include <unistd.h>
#include <time.h>
int main(void)
{
time_t t1, t2;
char *st1, *st2;
t1 = time(NULL);
if(sleep(1)) {
printf("Signal received, exiting\n");
return (1);
}
t2 = time(NULL);
printf("t1 is %lu\n", (unsigned long)t1);
printf("t2 is %lu\n", (unsigned long)t2);
st1 = ctime(&t1);
st2 = ctime(&t2);
printf("st1 is %s", st1);
printf("st2 is %s", st2);
return(0);
}
The code above gives this output:
t1 is 1183506197
t2 is 1183506198
st1 is Wed Jul 4 01:43:18 2007
st2 is Wed Jul 4 01:43:18 2007
How can it happen that two outputs of ctime(3), given two different
inputs (t1 != t2) are the same (st1 == st2) ?
Moreover, on a shell console:
$ date -r 1183506197
Wed Jul 4 01:43:17 CEST 2007
$ date -r 1183506198
Wed Jul 4 01:43:18 CEST 2007
Here date gives a correct result (i.e. the first date is one second
before the second date).
I doubt it has something to do with the compiler grouping the two calls
to time(NULL). If it's the case, how can I avoid it?
Thank you.