t_mktime.c revision 1.4 1 1.4 martin /* $NetBSD: t_mktime.c,v 1.4 2012/01/07 15:05:22 martin Exp $ */
2 1.1 pgoyette
3 1.1 pgoyette /*-
4 1.1 pgoyette * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 1.1 pgoyette * All rights reserved.
6 1.1 pgoyette *
7 1.1 pgoyette * Redistribution and use in source and binary forms, with or without
8 1.1 pgoyette * modification, are permitted provided that the following conditions
9 1.1 pgoyette * are met:
10 1.1 pgoyette * 1. Redistributions of source code must retain the above copyright
11 1.1 pgoyette * notice, this list of conditions and the following disclaimer.
12 1.1 pgoyette * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 pgoyette * notice, this list of conditions and the following disclaimer in the
14 1.1 pgoyette * documentation and/or other materials provided with the distribution.
15 1.1 pgoyette *
16 1.1 pgoyette * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 pgoyette * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 pgoyette * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 pgoyette * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 pgoyette * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 pgoyette * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 pgoyette * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 pgoyette * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 pgoyette * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 pgoyette * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 pgoyette * POSSIBILITY OF SUCH DAMAGE.
27 1.1 pgoyette */
28 1.1 pgoyette
29 1.1 pgoyette #include <atf-c.h>
30 1.1 pgoyette
31 1.1 pgoyette #include <err.h>
32 1.1 pgoyette #include <errno.h>
33 1.1 pgoyette #include <string.h>
34 1.1 pgoyette #include <time.h>
35 1.1 pgoyette
36 1.3 apb ATF_TC(mktime_negyear);
37 1.1 pgoyette
38 1.3 apb ATF_TC_HEAD(mktime_negyear, tc)
39 1.1 pgoyette {
40 1.1 pgoyette
41 1.1 pgoyette atf_tc_set_md_var(tc, "descr", "Test mktime(3) with negative year");
42 1.1 pgoyette }
43 1.1 pgoyette
44 1.3 apb ATF_TC_BODY(mktime_negyear, tc)
45 1.1 pgoyette {
46 1.1 pgoyette struct tm tms;
47 1.4 martin time_t t;
48 1.1 pgoyette
49 1.1 pgoyette (void)memset(&tms, 0, sizeof(tms));
50 1.1 pgoyette tms.tm_year = ~0;
51 1.1 pgoyette
52 1.1 pgoyette errno = 0;
53 1.4 martin t = mktime(&tms);
54 1.4 martin ATF_REQUIRE_ERRNO(0, t != (time_t)-1);
55 1.1 pgoyette }
56 1.1 pgoyette
57 1.3 apb ATF_TC(timegm_epoch);
58 1.3 apb
59 1.3 apb ATF_TC_HEAD(timegm_epoch, tc)
60 1.3 apb {
61 1.3 apb
62 1.3 apb atf_tc_set_md_var(tc, "descr", "Test timegm(3) close to the epoch");
63 1.3 apb }
64 1.3 apb
65 1.3 apb ATF_TC_BODY(timegm_epoch, tc)
66 1.3 apb {
67 1.3 apb struct tm tms;
68 1.4 martin time_t t;
69 1.3 apb
70 1.3 apb /* midnight on 1 Jan 1970 */
71 1.3 apb (void)memset(&tms, 0, sizeof(tms));
72 1.3 apb errno = 0;
73 1.3 apb tms.tm_year = 1970 - 1900;
74 1.3 apb tms.tm_mday = 1;
75 1.4 martin t = timegm(&tms);
76 1.4 martin ATF_REQUIRE_ERRNO(0, t == (time_t)0);
77 1.3 apb
78 1.3 apb /* one second after midnight on 1 Jan 1970 */
79 1.3 apb (void)memset(&tms, 0, sizeof(tms));
80 1.3 apb errno = 0;
81 1.3 apb tms.tm_year = 1970 - 1900;
82 1.3 apb tms.tm_mday = 1;
83 1.3 apb tms.tm_sec = 1;
84 1.4 martin t = timegm(&tms);
85 1.4 martin ATF_REQUIRE_ERRNO(0, t == (time_t)1);
86 1.3 apb
87 1.3 apb /*
88 1.3 apb * 1969-12-31 23:59:59 = one second before the epoch.
89 1.3 apb * Result should be -1 with errno = 0.
90 1.3 apb */
91 1.3 apb (void)memset(&tms, 0, sizeof(tms));
92 1.3 apb errno = 0;
93 1.3 apb tms.tm_year = 1969 - 1900;
94 1.3 apb tms.tm_mon = 12 - 1;
95 1.3 apb tms.tm_mday = 31;
96 1.3 apb tms.tm_hour = 23;
97 1.3 apb tms.tm_min = 59;
98 1.3 apb tms.tm_sec = 59;
99 1.4 martin t = timegm(&tms);
100 1.4 martin ATF_REQUIRE_ERRNO(0, t == (time_t)-1);
101 1.3 apb
102 1.3 apb /*
103 1.3 apb * Another way of getting one second before the epoch:
104 1.3 apb * Set date to 1 Jan 1970, and time to -1 second.
105 1.3 apb */
106 1.3 apb (void)memset(&tms, 0, sizeof(tms));
107 1.3 apb errno = 0;
108 1.3 apb tms.tm_year = 1970 - 1900;
109 1.3 apb tms.tm_mday = 1;
110 1.3 apb tms.tm_sec = -1;
111 1.4 martin t = timegm(&tms);
112 1.4 martin ATF_REQUIRE_ERRNO(0, t == (time_t)-1);
113 1.3 apb
114 1.3 apb /*
115 1.3 apb * Two seconds before the epoch.
116 1.3 apb */
117 1.3 apb (void)memset(&tms, 0, sizeof(tms));
118 1.3 apb errno = 0;
119 1.3 apb tms.tm_year = 1970 - 1900;
120 1.3 apb tms.tm_mday = 1;
121 1.3 apb tms.tm_sec = -2;
122 1.4 martin t = timegm(&tms);
123 1.4 martin ATF_REQUIRE_ERRNO(0, t == (time_t)-2);
124 1.3 apb
125 1.3 apb }
126 1.3 apb
127 1.3 apb
128 1.1 pgoyette ATF_TP_ADD_TCS(tp)
129 1.1 pgoyette {
130 1.1 pgoyette
131 1.3 apb ATF_TP_ADD_TC(tp, mktime_negyear);
132 1.3 apb ATF_TP_ADD_TC(tp, timegm_epoch);
133 1.1 pgoyette
134 1.1 pgoyette return atf_no_error();
135 1.1 pgoyette }
136