gmt_mktime.c (505d05c7) gmt_mktime.c (159d09a2)
1/*
2 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
4 */
5
6#pragma ident "%Z%%M% %I% %E% SMI"
7
8/* This code placed in the public domain by Mark W. Eichin */
9
10#include <stdio.h>
1/* This code placed in the public domain by Mark W. Eichin */
2
3#include <stdio.h>
11#include <k5-int.h>
4#include "autoconf.h"
12
13#ifdef HAVE_SYS_TYPES_H
14#include <sys/types.h>
15#endif
16#ifdef HAVE_SYS_TIME_H
17#include <sys/time.h>
18#ifdef TIME_WITH_SYS_TIME
19#include <time.h>

--- 18 unchanged lines hidden (view full) ---

38243, /* sep 30 */
39273, /* oct 31 */
40304, /* nov 30 */
41334 /* dec 31 */
42};
43
44#define hasleapday(year) (year%400?(year%100?(year%4?0:1):0):1)
45
5
6#ifdef HAVE_SYS_TYPES_H
7#include <sys/types.h>
8#endif
9#ifdef HAVE_SYS_TIME_H
10#include <sys/time.h>
11#ifdef TIME_WITH_SYS_TIME
12#include <time.h>

--- 18 unchanged lines hidden (view full) ---

31243, /* sep 30 */
32273, /* oct 31 */
33304, /* nov 30 */
34334 /* dec 31 */
35};
36
37#define hasleapday(year) (year%400?(year%100?(year%4?0:1):0):1)
38
46time_t gmt_mktime(struct tm *t)
39time_t krb5int_gmt_mktime(struct tm *t)
47{
48 time_t accum;
49
50#define assert_time(cnd) if(!(cnd)) return (time_t) -1
51
52 /*
53 * For 32-bit signed time_t centered on 1/1/1970, the range is:
54 * time 0x80000000 -> Fri Dec 13 16:45:52 1901
55 * time 0x7fffffff -> Mon Jan 18 22:14:07 2038
56 *
57 * So years 1901 and 2038 are allowable, but we can't encode all
58 * dates in those years, and we're not doing overflow/underflow
59 * checking for such cases.
60 */
61 assert_time(t->tm_year>=1);
62 assert_time(t->tm_year<=138);
40{
41 time_t accum;
42
43#define assert_time(cnd) if(!(cnd)) return (time_t) -1
44
45 /*
46 * For 32-bit signed time_t centered on 1/1/1970, the range is:
47 * time 0x80000000 -> Fri Dec 13 16:45:52 1901
48 * time 0x7fffffff -> Mon Jan 18 22:14:07 2038
49 *
50 * So years 1901 and 2038 are allowable, but we can't encode all
51 * dates in those years, and we're not doing overflow/underflow
52 * checking for such cases.
53 */
54 assert_time(t->tm_year>=1);
55 assert_time(t->tm_year<=138);
56
63 assert_time(t->tm_mon>=0);
64 assert_time(t->tm_mon<=11);
65 assert_time(t->tm_mday>=1);
66 assert_time(t->tm_mday<=31);
67 assert_time(t->tm_hour>=0);
68 assert_time(t->tm_hour<=23);
69 assert_time(t->tm_min>=0);
70 assert_time(t->tm_min<=59);

--- 21 unchanged lines hidden (view full) ---

92 accum += t->tm_hour;
93 accum *= 60; /* 60 minute/hour */
94 accum += t->tm_min;
95 accum *= 60; /* 60 seconds/minute */
96 accum += t->tm_sec;
97
98 return accum;
99}
57 assert_time(t->tm_mon>=0);
58 assert_time(t->tm_mon<=11);
59 assert_time(t->tm_mday>=1);
60 assert_time(t->tm_mday<=31);
61 assert_time(t->tm_hour>=0);
62 assert_time(t->tm_hour<=23);
63 assert_time(t->tm_min>=0);
64 assert_time(t->tm_min<=59);

--- 21 unchanged lines hidden (view full) ---

86 accum += t->tm_hour;
87 accum *= 60; /* 60 minute/hour */
88 accum += t->tm_min;
89 accum *= 60; /* 60 seconds/minute */
90 accum += t->tm_sec;
91
92 return accum;
93}
94
95#ifdef TEST_LEAP
96int
97main (int argc, char *argv[])
98{
99 int yr;
100 time_t t;
101 struct tm tm = {
102 .tm_mon = 0, .tm_mday = 1,
103 .tm_hour = 0, .tm_min = 0, .tm_sec = 0,
104 };
105 for (yr = 60; yr <= 104; yr++)
106 {
107 printf ("1/1/%d%c -> ", 1900 + yr, hasleapday((1900+yr)) ? '*' : ' ');
108 tm.tm_year = yr;
109 t = gmt_mktime (&tm);
110 if (t == (time_t) -1)
111 printf ("-1\n");
112 else
113 {
114 long u;
115 if (t % (24 * 60 * 60))
116 printf ("(not integral multiple of days) ");
117 u = t / (24 * 60 * 60);
118 printf ("%3ld*365%+ld\t0x%08lx\n",
119 (long) (u / 365), (long) (u % 365),
120 (long) t);
121 }
122 }
123 t = 0x80000000, printf ("time 0x%lx -> %s", t, ctime (&t));
124 t = 0x7fffffff, printf ("time 0x%lx -> %s", t, ctime (&t));
125 return 0;
126}
127#endif