~ubuntu-branches/ubuntu/raring/bind9/raring

« back to all changes in this revision

Viewing changes to lib/dns/time.c

  • Committer: Package Import Robot
  • Author(s): LaMont Jones
  • Date: 2012-01-19 12:30:31 UTC
  • mfrom: (1.9.1) (26.1.6 sid)
  • Revision ID: package-import@ubuntu.com-20120119123031-0j2wlg66ll5ogpz2
Tags: 1:9.8.1.dfsg.P1-1~build1
preciseĀ upload

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * Copyright (C) 2004, 2005, 2007, 2009, 2010  Internet Systems Consortium, Inc. ("ISC")
 
2
 * Copyright (C) 2004, 2005, 2007, 2009-2011  Internet Systems Consortium, Inc. ("ISC")
3
3
 * Copyright (C) 1998-2003  Internet Software Consortium.
4
4
 *
5
5
 * Permission to use, copy, modify, and/or distribute this software for any
15
15
 * PERFORMANCE OF THIS SOFTWARE.
16
16
 */
17
17
 
18
 
/* $Id: time.c,v 1.33.186.2 2010-04-21 23:50:05 tbox Exp $ */
 
18
/* $Id: time.c,v 1.35.132.2 2011-03-09 23:46:55 tbox Exp $ */
19
19
 
20
20
/*! \file */
21
21
 
28
28
 
29
29
#include <isc/print.h>
30
30
#include <isc/region.h>
 
31
#include <isc/serial.h>
31
32
#include <isc/stdtime.h>
32
33
#include <isc/util.h>
33
34
 
44
45
        unsigned int l;
45
46
        isc_region_t region;
46
47
 
47
 
        REQUIRE(t >= 0);
48
 
 
 
48
/*
 
49
 * Warning. Do NOT use arguments with side effects with these macros.
 
50
 */
49
51
#define is_leap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
50
52
#define year_secs(y) ((is_leap(y) ? 366 : 365 ) * 86400)
51
53
#define month_secs(m,y) ((days[m] + ((m == 1 && is_leap(y)) ? 1 : 0 )) * 86400)
52
54
 
53
55
        tm.tm_year = 70;
 
56
        while (t < 0) {
 
57
                if (tm.tm_year == 0)
 
58
                        return (ISC_R_RANGE);
 
59
                tm.tm_year--;
 
60
                secs = year_secs(tm.tm_year + 1900);
 
61
                t += secs;
 
62
        }
54
63
        while ((secs = year_secs(tm.tm_year + 1900)) <= t) {
55
64
                t -= secs;
56
65
                tm.tm_year++;
98
107
dns_time32_totext(isc_uint32_t value, isc_buffer_t *target) {
99
108
        isc_stdtime_t now;
100
109
        isc_int64_t start;
101
 
        isc_int64_t base;
102
110
        isc_int64_t t;
103
111
 
104
112
        /*
109
117
         */
110
118
        isc_stdtime_get(&now);
111
119
        start = (isc_int64_t) now;
112
 
        start -= 0x7fffffff;
113
 
        base = 0;
114
 
        while ((t = (base + value)) < start) {
115
 
                base += 0x80000000;
116
 
                base += 0x80000000;
117
 
        }
 
120
        if (isc_serial_gt(value, now))
 
121
                t = start + (value - now);
 
122
        else
 
123
                t = start - (now - value);
118
124
        return (dns_time64_totext(t, target));
119
125
}
120
126
 
145
151
                   &year, &month, &day, &hour, &minute, &second) != 6)
146
152
                return (DNS_R_SYNTAX);
147
153
 
148
 
        RANGE(1970, 9999, year);
 
154
        RANGE(0, 9999, year);
149
155
        RANGE(1, 12, month);
150
156
        RANGE(1, days[month - 1] +
151
157
                 ((month == 2 && is_leap(year)) ? 1 : 0), day);
154
160
        RANGE(0, 60, second);           /* 60 == leap second. */
155
161
 
156
162
        /*
157
 
         * Calculate seconds since epoch.
 
163
         * Calculate seconds from epoch.
 
164
         * Note: this uses a idealized calendar.
158
165
         */
159
166
        value = second + (60 * minute) + (3600 * hour) + ((day - 1) * 86400);
160
167
        for (i = 0; i < (month - 1); i++)
161
168
                value += days[i] * 86400;
162
169
        if (is_leap(year) && month > 2)
163
170
                value += 86400;
164
 
        for (i = 1970; i < year; i++) {
165
 
                secs = (is_leap(i) ? 366 : 365) * 86400;
166
 
                value += secs;
 
171
        if (year < 1970) {
 
172
                for (i = 1969; i >= year; i--) {
 
173
                        secs = (is_leap(i) ? 366 : 365) * 86400;
 
174
                        value -= secs;
 
175
                }
 
176
        } else {
 
177
                for (i = 1970; i < year; i++) {
 
178
                        secs = (is_leap(i) ? 366 : 365) * 86400;
 
179
                        value += secs;
 
180
                }
167
181
        }
168
182
 
169
183
        *target = value;