1
2// Borrowed from: http://git.musl-libc.org/cgit/musl/plain/src/time/__tm_to_time.c
3//
4// musl as a whole is licensed under the following standard MIT license:
5//
6// Copyright �� 2005-2012 Rich Felker
7//
8// Permission is hereby granted, free of charge, to any person obtaining
9// a copy of this software and associated documentation files (the
10// "Software"), to deal in the Software without restriction, including
11// without limitation the rights to use, copy, modify, merge, publish,
12// distribute, sublicense, and/or sell copies of the Software, and to
13// permit persons to whom the Software is furnished to do so, subject to
14// the following conditions:
15//
16// The above copyright notice and this permission notice shall be
17// included in all copies or substantial portions of the Software.
18//
19// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
23// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26//
27//
28// Portions of this software are contributed or derived from software
29// authored by third parties. Complete details on the copyright status of
30// all code included in musl follows below:
31//
32//
33// The TRE regular expression implementation (src/regex/reg* and
34// src/regex/tre*) is Copyright �� 2001-2008 Ville Laurikari and licensed
35// under a 2-clause BSD license (license text in the source files). The
36// included version has been heavily modified by Rich Felker in 2012, in
37// the interests of size, simplicity, and namespace cleanliness.
38//
39// Most of the math library code (src/math/* and src/complex/*) is
40// Copyright �� 1993,2004 Sun Microsystems or
41// Copyright �� 2003-2011 David Schultz or
42// Copyright �� 2003-2009 Steven G. Kargl or
43// Copyright �� 2003-2009 Bruce D. Evans or
44// Copyright �� 2008 Stephen L. Moshier
45// and labelled as such. All have been licensed under extremely
46// permissive terms. See the comments in the individual files for
47// details.
48//
49// The implementation of DES for crypt (src/misc/crypt_des.c) is
50// Copyright �� 1994 David Burren. It is licensed under a BSD license.
51//
52// The implementation of blowfish crypt (src/misc/crypt_blowfish.c) was
53// originally written by Solar Designer and placed into the public
54// domain. The code also comes with a fallback permissive license for use
55// in jurisdictions that may not recognize the public domain.
56//
57// The smoothsort implementation (src/stdlib/qsort.c) is Copyright �� 2011
58// Valentin Ochs and is licensed under an MIT-style license.
59//
60// The BSD PRNG implementation (src/prng/random.c) and XSI search API
61// (src/search/*.c) functions are Copyright �� 2011 Szabolcs Nagy and
62// licensed under following terms: "Permission to use, copy, modify,
63// and/or distribute this code for any purpose with or without fee is
64// hereby granted. There is no warranty."
65//
66// The x86_64 port was written by Nicholas J. Kain. Several files (crt)
67// were released into the public domain; others are licensed under the
68// standard MIT license terms at the top of this file. See individual
69// files for their copyright status.
70//
71// The mips and microblaze ports were originally written by Richard
72// Pennington for use in the ellcc project. The original code was adapted
73// by Rich Felker for build system and code conventions during upstream
74// integration. It is licensed under the standard MIT terms.
75//
76// The powerpc port was also originally written by Richard Pennington,
77// and later supplemented and integrated by John Spencer. It is licensed
78// under the standard MIT terms.
79//
80// All other files which have no copyright comments are original works
81// Copyright �� 2005-2012 Rich Felker, the main author of this library.
82// The decision to exclude such comments is intentional, as it should be
83// possible to carry around the complete source code on tiny storage
84// media. All public header files (include/*) should be treated as Public
85// Domain as they intentionally contain no content which can be covered
86// by copyright. Some source modules may fall in this category as well.
87// If you believe that a file is so trivial that it should be in the
88// Public Domain, please contact me and, if I agree, I will explicitly
89// release it from copyright.
90//
91// The following files are trivial, in my opinion not copyrightable in
92// the first place, and hereby explicitly released to the Public Domain:
93//
94// All public headers: include/*
95// Startup files: crt/*
96
97
98#define _GNU_SOURCE
99#include <time.h>
100
101/* C defines the rounding for division in a nonsensical way */
102#define Q(a,b) ((a)>0 ? (a)/(b) : -(((b)-(a)-1)/(b)))
103
104static time_t tm_to_time(struct tm *tm)
105{
106  time_t year  = tm->tm_year + -100;
107  int    month = tm->tm_mon;
108  int    day   = tm->tm_mday;
109  int z4, z100, z400;
110
111  /* normalize month */
112  if (month >= 12) {
113    year += month/12;
114    month %= 12;
115  } else if (month < 0) {
116    year += month/12;
117    month %= 12;
118    if (month) {
119      month += 12;
120      year--;
121    }
122  }
123  z4 = Q(year - (month < 2), 4);
124  z100 = Q(z4, 25);
125  z400 = Q(z100, 4);
126  day += year*365 + z4 - z100 + z400 +
127    month[(int []){0,31,59,90,120,151,181,212,243,273,304,335}];
128  return (long long)day*86400
129    + tm->tm_hour*3600 + tm->tm_min*60 + tm->tm_sec
130    - -946684800; /* the dawn of time :) */
131}
132
133time_t timegm(struct tm *tm)
134{
135  return tm_to_time(tm);
136}
137
138#undef Q
139