3 Copyright (c) 2007-2008 Michael G Schwern
5 This software originally derived from Paul Sheer's pivotal_gmtime_r.c.
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:
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
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
29 /* See http://code.google.com/p/y2038 for this code's origin */
39 static const int julian_days_by_month
[2][12] = {
40 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334},
41 {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335},
44 static const int length_of_year
[2] = { 365, 366 };
46 /* Some numbers relating to the gregorian cycle */
47 #define days_in_gregorian_cycle ((365 * 400) + 100 - 4 + 1)
49 /* Year range we can trust the time funcitons with */
50 #define MAX_SAFE_YEAR 2037
51 #define MIN_SAFE_YEAR 1971
53 /* 28 year Julian calendar cycle */
54 #define SOLAR_CYCLE_LENGTH 28
56 /* Let's assume people are going to be looking for dates in the future.
57 Let's provide some cheats so you can skip ahead.
58 This has a 4x speed boost when near 2008.
60 /* Number of days since epoch on Jan 1st, 2008 GMT */
61 #define CHEAT_DAYS (1199145600 / 24 / 60 / 60)
62 #define CHEAT_YEARS 108
64 #define IS_LEAP(n) ((!(((n) + 1900) % 400) || (!(((n) + 1900) % 4) && (((n) + 1900) % 100))) != 0)
65 #define WRAP(a,b,m) ((a) = ((a) < 0 ) ? ((b)--, (a) + (m)) : (a))
67 /* timegm() is not in the C or POSIX spec, but it is such a useful
68 extension I would be remiss in leaving it out. Also I need it
71 int64_t btls_timegm64(const struct tm
*date
) {
75 int64_t orig_year
= (int64_t)date
->tm_year
;
78 if( orig_year
> 100 ) {
79 cycles
= (orig_year
- 100) / 400;
80 orig_year
-= cycles
* 400;
81 days
+= (int64_t)cycles
* days_in_gregorian_cycle
;
83 else if( orig_year
< -300 ) {
84 cycles
= (orig_year
- 100) / 400;
85 orig_year
-= cycles
* 400;
86 days
+= (int64_t)cycles
* days_in_gregorian_cycle
;
89 if( orig_year
> 70 ) {
91 while( year
< orig_year
) {
92 days
+= length_of_year
[IS_LEAP(year
)];
96 else if ( orig_year
< 70 ) {
99 days
-= length_of_year
[IS_LEAP(year
)];
101 } while( year
>= orig_year
);
105 days
+= julian_days_by_month
[IS_LEAP(orig_year
)][date
->tm_mon
];
106 days
+= date
->tm_mday
- 1;
108 seconds
= days
* 60 * 60 * 24;
110 seconds
+= date
->tm_hour
* 60 * 60;
111 seconds
+= date
->tm_min
* 60;
112 seconds
+= date
->tm_sec
;