[asterisk-dev] CDR and daylight savings time

Hans Petter Selasky hselasky at c2i.net
Mon Jan 1 05:17:22 MST 2007


On Monday 01 January 2007 04:07, Kevin P. Fleming wrote:
> Hans Petter Selasky wrote:
> > From examining the code, this seems to be the case:
> >
> >                 gettimeofday(&cdr->start, NULL);
> >                 gettimeofday(&cdr->answer, NULL);
> >                 gettimeofday(&cdr->end, NULL);
>
> If you had read the man page for gettimeofday(), you would not have
> asked this question :-)
>
> gettimeofday(), when passed NULL as the second parameter (which we
> always do and is recommended practice) returns the number of
> seconds/microseconds since the Unix 'epoch' (January 1, 1970). It does
> not care about timezones at all, and is not affected by timezones or
> time shifts. In other words, it returns absolute time, not local time.

That is not the case.

Compile the following test program:

cat << EOF > testT.c
#include <stdio.h>
#include <time.h>
#include <sys/time.h>

int main()
{
        time_t now;
        struct timeval tv1;
        struct timespec tv2;

        gettimeofday(&tv1, NULL);

        clock_gettime(CLOCK_MONOTONIC, &tv2);

        now = time(NULL);

        printf("%d, %ld, %ld\n", now, tv1.tv_sec, tv2.tv_sec);

        return 0;
}
EOF

cc -o a.out testT.c

date 1300; ./a.out; date 1200; ./a.out

Result:

Mon Jan  1 13:00:00 CET 2007
1167652800, 1167652800, 1241
Mon Jan  1 12:00:00 CET 2007
1167649200, 1167649200, 1241

Here you clearly see that gettimeofday() is directly connected with the local 
time on the computer. On the other hand, CLOCK_MONOTONIC is not. That is what 
you want to use. Or am I wrong ?

--HPS


More information about the asterisk-dev mailing list