1 1.6 kre /* $NetBSD: t_mktime.c,v 1.6 2017/10/27 00:55:27 kre 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.5 jruoho ATF_TC(localtime_r_gmt); 37 1.5 jruoho ATF_TC_HEAD(localtime_r_gmt, tc) 38 1.5 jruoho { 39 1.5 jruoho atf_tc_set_md_var(tc, "descr", "Test that localtime_r(3) " 40 1.5 jruoho "returns localtime, not GMT (PR lib/28324)"); 41 1.5 jruoho } 42 1.5 jruoho 43 1.5 jruoho ATF_TC_BODY(localtime_r_gmt, tc) 44 1.5 jruoho { 45 1.5 jruoho struct tm *t; 46 1.5 jruoho struct tm tt; 47 1.5 jruoho time_t x; 48 1.5 jruoho 49 1.5 jruoho x = time(NULL); 50 1.5 jruoho localtime_r(&x, &tt); 51 1.5 jruoho t = localtime(&x); 52 1.5 jruoho 53 1.5 jruoho if (t->tm_sec != tt.tm_sec || t->tm_min != tt.tm_min || 54 1.5 jruoho t->tm_hour != tt.tm_hour || t->tm_mday != tt.tm_mday) 55 1.5 jruoho atf_tc_fail("inconsistencies between " 56 1.5 jruoho "localtime(3) and localtime_r(3)"); 57 1.5 jruoho } 58 1.5 jruoho 59 1.3 apb ATF_TC(mktime_negyear); 60 1.3 apb ATF_TC_HEAD(mktime_negyear, tc) 61 1.1 pgoyette { 62 1.1 pgoyette atf_tc_set_md_var(tc, "descr", "Test mktime(3) with negative year"); 63 1.1 pgoyette } 64 1.1 pgoyette 65 1.3 apb ATF_TC_BODY(mktime_negyear, tc) 66 1.1 pgoyette { 67 1.1 pgoyette struct tm tms; 68 1.4 martin time_t t; 69 1.1 pgoyette 70 1.1 pgoyette (void)memset(&tms, 0, sizeof(tms)); 71 1.6 kre tms.tm_year = -1; 72 1.6 kre tms.tm_mday = 1; 73 1.1 pgoyette 74 1.1 pgoyette errno = 0; 75 1.4 martin t = mktime(&tms); 76 1.6 kre ATF_REQUIRE(t != (time_t)-1); 77 1.1 pgoyette } 78 1.1 pgoyette 79 1.3 apb ATF_TC(timegm_epoch); 80 1.3 apb ATF_TC_HEAD(timegm_epoch, tc) 81 1.3 apb { 82 1.3 apb atf_tc_set_md_var(tc, "descr", "Test timegm(3) close to the epoch"); 83 1.3 apb } 84 1.3 apb 85 1.3 apb ATF_TC_BODY(timegm_epoch, tc) 86 1.3 apb { 87 1.3 apb struct tm tms; 88 1.4 martin time_t t; 89 1.3 apb 90 1.3 apb /* midnight on 1 Jan 1970 */ 91 1.3 apb (void)memset(&tms, 0, sizeof(tms)); 92 1.3 apb errno = 0; 93 1.3 apb tms.tm_year = 1970 - 1900; 94 1.3 apb tms.tm_mday = 1; 95 1.4 martin t = timegm(&tms); 96 1.6 kre ATF_REQUIRE(t == (time_t)0); 97 1.3 apb 98 1.3 apb /* one second after midnight on 1 Jan 1970 */ 99 1.3 apb (void)memset(&tms, 0, sizeof(tms)); 100 1.3 apb errno = 0; 101 1.3 apb tms.tm_year = 1970 - 1900; 102 1.3 apb tms.tm_mday = 1; 103 1.3 apb tms.tm_sec = 1; 104 1.4 martin t = timegm(&tms); 105 1.6 kre ATF_REQUIRE(t == (time_t)1); 106 1.3 apb 107 1.3 apb /* 108 1.3 apb * 1969-12-31 23:59:59 = one second before the epoch. 109 1.3 apb * Result should be -1 with errno = 0. 110 1.3 apb */ 111 1.3 apb (void)memset(&tms, 0, sizeof(tms)); 112 1.3 apb errno = 0; 113 1.3 apb tms.tm_year = 1969 - 1900; 114 1.3 apb tms.tm_mon = 12 - 1; 115 1.3 apb tms.tm_mday = 31; 116 1.3 apb tms.tm_hour = 23; 117 1.3 apb tms.tm_min = 59; 118 1.3 apb tms.tm_sec = 59; 119 1.4 martin t = timegm(&tms); 120 1.6 kre ATF_REQUIRE(t == (time_t)-1); 121 1.6 kre /* ATF_REQUIRE(errno == 0); does not work, errno is kept clear */ 122 1.3 apb 123 1.3 apb /* 124 1.3 apb * Another way of getting one second before the epoch: 125 1.3 apb * Set date to 1 Jan 1970, and time to -1 second. 126 1.3 apb */ 127 1.3 apb (void)memset(&tms, 0, sizeof(tms)); 128 1.3 apb errno = 0; 129 1.3 apb tms.tm_year = 1970 - 1900; 130 1.3 apb tms.tm_mday = 1; 131 1.3 apb tms.tm_sec = -1; 132 1.4 martin t = timegm(&tms); 133 1.6 kre ATF_REQUIRE(t == (time_t)-1); 134 1.3 apb 135 1.3 apb /* 136 1.3 apb * Two seconds before the epoch. 137 1.3 apb */ 138 1.3 apb (void)memset(&tms, 0, sizeof(tms)); 139 1.3 apb errno = 0; 140 1.3 apb tms.tm_year = 1970 - 1900; 141 1.3 apb tms.tm_mday = 1; 142 1.3 apb tms.tm_sec = -2; 143 1.4 martin t = timegm(&tms); 144 1.6 kre ATF_REQUIRE(t == (time_t)-2); 145 1.3 apb 146 1.3 apb } 147 1.3 apb 148 1.3 apb 149 1.1 pgoyette ATF_TP_ADD_TCS(tp) 150 1.1 pgoyette { 151 1.1 pgoyette 152 1.5 jruoho ATF_TP_ADD_TC(tp, localtime_r_gmt); 153 1.3 apb ATF_TP_ADD_TC(tp, mktime_negyear); 154 1.3 apb ATF_TP_ADD_TC(tp, timegm_epoch); 155 1.1 pgoyette 156 1.1 pgoyette return atf_no_error(); 157 1.1 pgoyette } 158