Fix btls build for Linux.
[mono.git] / mono / btls / btls-time64.c
1 /*
2
3 Copyright (c) 2007-2008  Michael G Schwern
4
5 This software originally derived from Paul Sheer's pivotal_gmtime_r.c.
6
7 The MIT License:
8
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 THE SOFTWARE.
26
27 */
28
29 /* See http://code.google.com/p/y2038 for this code's origin */
30
31 #include <assert.h>
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <stdint.h>
35 #include <string.h>
36 #include <time.h>
37 #include <errno.h>
38
39 /* Spec says except for stftime() and the _r() functions, these
40    all return static memory.  Stabbings! */
41 static struct tm   Static_Return_Date;
42 static char        Static_Return_String[35];
43
44 static const int days_in_month[2][12] = {
45     {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
46     {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
47 };
48
49 static const int julian_days_by_month[2][12] = {
50     {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334},
51     {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335},
52 };
53
54 static char const wday_name[7][3] = {
55     "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
56 };
57
58 static char const mon_name[12][3] = {
59     "Jan", "Feb", "Mar", "Apr", "May", "Jun",
60     "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
61 };
62
63 static const int length_of_year[2] = { 365, 366 };
64
65 /* Some numbers relating to the gregorian cycle */
66 static const int64_t     years_in_gregorian_cycle   = 400;
67 #define               days_in_gregorian_cycle      ((365 * 400) + 100 - 4 + 1)
68 static const int64_t seconds_in_gregorian_cycle = days_in_gregorian_cycle * 60LL * 60LL * 24LL;
69
70 /* Year range we can trust the time funcitons with */
71 #define MAX_SAFE_YEAR 2037
72 #define MIN_SAFE_YEAR 1971
73
74 /* 28 year Julian calendar cycle */
75 #define SOLAR_CYCLE_LENGTH 28
76
77 /* Year cycle from MAX_SAFE_YEAR down. */
78 static const int safe_years_high[SOLAR_CYCLE_LENGTH] = {
79     2016, 2017, 2018, 2019,
80     2020, 2021, 2022, 2023,
81     2024, 2025, 2026, 2027,
82     2028, 2029, 2030, 2031,
83     2032, 2033, 2034, 2035,
84     2036, 2037, 2010, 2011,
85     2012, 2013, 2014, 2015
86 };
87
88 /* Year cycle from MIN_SAFE_YEAR up */
89 static const int safe_years_low[SOLAR_CYCLE_LENGTH] = {
90     1996, 1997, 1998, 1971,
91     1972, 1973, 1974, 1975,
92     1976, 1977, 1978, 1979,
93     1980, 1981, 1982, 1983,
94     1984, 1985, 1986, 1987,
95     1988, 1989, 1990, 1991,
96     1992, 1993, 1994, 1995,
97 };
98
99 /* Let's assume people are going to be looking for dates in the future.
100    Let's provide some cheats so you can skip ahead.
101    This has a 4x speed boost when near 2008.
102 */
103 /* Number of days since epoch on Jan 1st, 2008 GMT */
104 #define CHEAT_DAYS  (1199145600 / 24 / 60 / 60)
105 #define CHEAT_YEARS 108
106
107 #define IS_LEAP(n)      ((!(((n) + 1900) % 400) || (!(((n) + 1900) % 4) && (((n) + 1900) % 100))) != 0)
108 #define WRAP(a,b,m)     ((a) = ((a) <  0  ) ? ((b)--, (a) + (m)) : (a))
109
110 /* timegm() is not in the C or POSIX spec, but it is such a useful
111    extension I would be remiss in leaving it out.  Also I need it
112    for localtime64()
113 */
114 int64_t btls_timegm64(const struct tm *date) {
115     int64_t days    = 0;
116     int64_t seconds = 0;
117     int64_t     year;
118     int64_t     orig_year = (int64_t)date->tm_year;
119     int      cycles  = 0;
120
121     if( orig_year > 100 ) {
122         cycles = (orig_year - 100) / 400;
123         orig_year -= cycles * 400;
124         days      += (int64_t)cycles * days_in_gregorian_cycle;
125     }
126     else if( orig_year < -300 ) {
127         cycles = (orig_year - 100) / 400;
128         orig_year -= cycles * 400;
129         days      += (int64_t)cycles * days_in_gregorian_cycle;
130     }
131
132     if( orig_year > 70 ) {
133         year = 70;
134         while( year < orig_year ) {
135             days += length_of_year[IS_LEAP(year)];
136             year++;
137         }
138     }
139     else if ( orig_year < 70 ) {
140         year = 69;
141         do {
142             days -= length_of_year[IS_LEAP(year)];
143             year--;
144         } while( year >= orig_year );
145     }
146
147
148     days += julian_days_by_month[IS_LEAP(orig_year)][date->tm_mon];
149     days += date->tm_mday - 1;
150
151     seconds = days * 60 * 60 * 24;
152
153     seconds += date->tm_hour * 60 * 60;
154     seconds += date->tm_min * 60;
155     seconds += date->tm_sec;
156
157     return(seconds);
158 }