File: gettime.c

package info (click to toggle)
inn2 2.4.5-5
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 8,912 kB
  • ctags: 7,860
  • sloc: ansic: 85,104; perl: 11,427; sh: 9,863; makefile: 2,498; yacc: 1,563; python: 298; lex: 252; tcl: 7
file content (80 lines) | stat: -rw-r--r-- 1,931 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*  $Id: gettime.c 4135 2000-10-19 16:38:13Z kondou $
**
**  Find and return time information portably.
*/
#include "config.h"
#include "libinn.h"

#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif

#ifdef TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# ifdef HAVE_SYS_TIME_H
#  include <sys/time.h>
# else
#  include <time.h>
# endif
#endif


int
GetTimeInfo(TIMEINFO *Now)
{
    static time_t       NextHour;
    static long         LastTzone;
    struct tm           *tm;
    int                 secondsUntilNextHour;

    struct timeval      tv;

#ifndef HAVE_TM_GMTOFF
    struct tm           local;
    struct tm           gmt;
#endif

    /* Get the basic time. */
    if (gettimeofday(&tv, (struct timezone *) 0) == -1)
        return -1;
    Now->time = tv.tv_sec;
    Now->usec = tv.tv_usec;

    /* Now get the timezone if the last time < HH:00:00 <= now for some HH.  */
    if (NextHour <= Now->time) {
        tm = localtime(&Now->time);
        if (tm == NULL)
            return -1;
        secondsUntilNextHour = 60 * (60 - tm->tm_min) - tm->tm_sec;

#ifdef HAVE_TM_GMTOFF
        LastTzone = (0 - tm->tm_gmtoff) / 60;
#else
        /* To get the timezone, compare localtime with GMT. */
        local = *tm;
        if ((tm = gmtime(&Now->time)) == NULL)
            return -1;
        gmt = *tm;

        /* Assume we are never more than 24 hours away. */
        LastTzone = gmt.tm_yday - local.tm_yday;
        if (LastTzone > 1)
            LastTzone = -24;
        else if (LastTzone < -1)
            LastTzone = 24;
        else
            LastTzone *= 24;

        /* Scale in the hours and minutes; ignore seconds. */
        LastTzone += gmt.tm_hour - local.tm_hour;
        LastTzone *= 60;
        LastTzone += gmt.tm_min - local.tm_min;
#endif  /* defined(HAVE_TM_GMTOFF) */

        NextHour = Now->time + secondsUntilNextHour;
    }
    Now->tzone = LastTzone;
    return 0;
}