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