chiark / gitweb /
check responses are in right phase for article; implement notice_processed
[inn-innduct.git] / lib / gettime.c
1 /*  $Id: gettime.c 4135 2000-10-19 16:38:13Z kondou $
2 **
3 **  Find and return time information portably.
4 */
5 #include "config.h"
6 #include "libinn.h"
7
8 #ifdef HAVE_UNISTD_H
9 # include <unistd.h>
10 #endif
11
12 #ifdef TIME_WITH_SYS_TIME
13 # include <sys/time.h>
14 # include <time.h>
15 #else
16 # ifdef HAVE_SYS_TIME_H
17 #  include <sys/time.h>
18 # else
19 #  include <time.h>
20 # endif
21 #endif
22
23
24 int
25 GetTimeInfo(TIMEINFO *Now)
26 {
27     static time_t       NextHour;
28     static long         LastTzone;
29     struct tm           *tm;
30     int                 secondsUntilNextHour;
31
32     struct timeval      tv;
33
34 #ifndef HAVE_TM_GMTOFF
35     struct tm           local;
36     struct tm           gmt;
37 #endif
38
39     /* Get the basic time. */
40     if (gettimeofday(&tv, (struct timezone *) 0) == -1)
41         return -1;
42     Now->time = tv.tv_sec;
43     Now->usec = tv.tv_usec;
44
45     /* Now get the timezone if the last time < HH:00:00 <= now for some HH.  */
46     if (NextHour <= Now->time) {
47         tm = localtime(&Now->time);
48         if (tm == NULL)
49             return -1;
50         secondsUntilNextHour = 60 * (60 - tm->tm_min) - tm->tm_sec;
51
52 #ifdef HAVE_TM_GMTOFF
53         LastTzone = (0 - tm->tm_gmtoff) / 60;
54 #else
55         /* To get the timezone, compare localtime with GMT. */
56         local = *tm;
57         if ((tm = gmtime(&Now->time)) == NULL)
58             return -1;
59         gmt = *tm;
60
61         /* Assume we are never more than 24 hours away. */
62         LastTzone = gmt.tm_yday - local.tm_yday;
63         if (LastTzone > 1)
64             LastTzone = -24;
65         else if (LastTzone < -1)
66             LastTzone = 24;
67         else
68             LastTzone *= 24;
69
70         /* Scale in the hours and minutes; ignore seconds. */
71         LastTzone += gmt.tm_hour - local.tm_hour;
72         LastTzone *= 60;
73         LastTzone += gmt.tm_min - local.tm_min;
74 #endif  /* defined(HAVE_TM_GMTOFF) */
75
76         NextHour = Now->time + secondsUntilNextHour;
77     }
78     Now->tzone = LastTzone;
79     return 0;
80 }