xref: /freebsd/crypto/openssh/openbsd-compat/bsd-timegm.c (revision f374ba41f55c1a127303d92d830dd58eef2f5243)
1*38a52bd3SEd Maste /*
2*38a52bd3SEd Maste  * Copyright (c) 1997 Kungliga Tekniska Högskolan
3*38a52bd3SEd Maste  * (Royal Institute of Technology, Stockholm, Sweden).
4*38a52bd3SEd Maste  * All rights reserved.
5*38a52bd3SEd Maste  *
6*38a52bd3SEd Maste  * Redistribution and use in source and binary forms, with or without
7*38a52bd3SEd Maste  * modification, are permitted provided that the following conditions
8*38a52bd3SEd Maste  * are met:
9*38a52bd3SEd Maste  *
10*38a52bd3SEd Maste  * 1. Redistributions of source code must retain the above copyright
11*38a52bd3SEd Maste  *    notice, this list of conditions and the following disclaimer.
12*38a52bd3SEd Maste  *
13*38a52bd3SEd Maste  * 2. Redistributions in binary form must reproduce the above copyright
14*38a52bd3SEd Maste  *    notice, this list of conditions and the following disclaimer in the
15*38a52bd3SEd Maste  *    documentation and/or other materials provided with the distribution.
16*38a52bd3SEd Maste  *
17*38a52bd3SEd Maste  * 3. Neither the name of the Institute nor the names of its contributors
18*38a52bd3SEd Maste  *    may be used to endorse or promote products derived from this software
19*38a52bd3SEd Maste  *    without specific prior written permission.
20*38a52bd3SEd Maste  *
21*38a52bd3SEd Maste  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22*38a52bd3SEd Maste  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23*38a52bd3SEd Maste  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24*38a52bd3SEd Maste  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25*38a52bd3SEd Maste  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26*38a52bd3SEd Maste  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27*38a52bd3SEd Maste  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28*38a52bd3SEd Maste  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29*38a52bd3SEd Maste  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30*38a52bd3SEd Maste  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31*38a52bd3SEd Maste  * SUCH DAMAGE.
32*38a52bd3SEd Maste  */
33*38a52bd3SEd Maste 
34*38a52bd3SEd Maste /*
35*38a52bd3SEd Maste   adapted for Samba4 by Andrew Tridgell
36*38a52bd3SEd Maste */
37*38a52bd3SEd Maste 
38*38a52bd3SEd Maste #include "includes.h"
39*38a52bd3SEd Maste 
40*38a52bd3SEd Maste #include <time.h>
41*38a52bd3SEd Maste 
42*38a52bd3SEd Maste #ifndef HAVE_TIMEGM
43*38a52bd3SEd Maste 
is_leap(unsigned y)44*38a52bd3SEd Maste static int is_leap(unsigned y)
45*38a52bd3SEd Maste {
46*38a52bd3SEd Maste 	y += 1900;
47*38a52bd3SEd Maste 	return (y % 4) == 0 && ((y % 100) != 0 || (y % 400) == 0);
48*38a52bd3SEd Maste }
49*38a52bd3SEd Maste 
timegm(struct tm * tm)50*38a52bd3SEd Maste time_t timegm(struct tm *tm)
51*38a52bd3SEd Maste {
52*38a52bd3SEd Maste 	static const unsigned ndays[2][12] ={
53*38a52bd3SEd Maste 		{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
54*38a52bd3SEd Maste 		{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
55*38a52bd3SEd Maste 	time_t res = 0;
56*38a52bd3SEd Maste 	unsigned i;
57*38a52bd3SEd Maste 
58*38a52bd3SEd Maste 	if (tm->tm_mon > 12 ||
59*38a52bd3SEd Maste 	    tm->tm_mon < 0 ||
60*38a52bd3SEd Maste 	    tm->tm_mday > 31 ||
61*38a52bd3SEd Maste 	    tm->tm_min > 60 ||
62*38a52bd3SEd Maste 	    tm->tm_sec > 60 ||
63*38a52bd3SEd Maste 	    tm->tm_hour > 24) {
64*38a52bd3SEd Maste 		/* invalid tm structure */
65*38a52bd3SEd Maste 		return 0;
66*38a52bd3SEd Maste 	}
67*38a52bd3SEd Maste 
68*38a52bd3SEd Maste 	for (i = 70; i < tm->tm_year; ++i)
69*38a52bd3SEd Maste 		res += is_leap(i) ? 366 : 365;
70*38a52bd3SEd Maste 
71*38a52bd3SEd Maste 	for (i = 0; i < tm->tm_mon; ++i)
72*38a52bd3SEd Maste 		res += ndays[is_leap(tm->tm_year)][i];
73*38a52bd3SEd Maste 	res += tm->tm_mday - 1;
74*38a52bd3SEd Maste 	res *= 24;
75*38a52bd3SEd Maste 	res += tm->tm_hour;
76*38a52bd3SEd Maste 	res *= 60;
77*38a52bd3SEd Maste 	res += tm->tm_min;
78*38a52bd3SEd Maste 	res *= 60;
79*38a52bd3SEd Maste 	res += tm->tm_sec;
80*38a52bd3SEd Maste 	return res;
81*38a52bd3SEd Maste }
82*38a52bd3SEd Maste #endif /* HAVE_TIMEGM */
83