3660e08ec66fc86769a1df585bd9f0174da4013a
1 #ifndef _BABELTRACE_UTC_H
2 #define _BABELTRACE_UTC_H
5 * Copyright (C) 2011-2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 /* If set, use GNU or BSD timegm(3) */
29 #if defined(_BSD_SOURCE) || defined(_SVID_SOURCE)
32 time_t bt_timegm(struct tm
*tm
)
37 #elif defined(__MINGW32__)
40 time_t bt_timegm(struct tm
*tm
)
50 * This is a simple implementation of timegm() it just turns the "struct tm" into
51 * a GMT time_t. It does not normalize any of the fields of the "struct tm", nor
52 * does it set tm_wday or tm_yday.
56 int bt_leapyear(int year
)
58 return ((year
% 4) == 0 && ((year
% 100) != 0 || (year
% 400) == 0));
62 time_t bt_timegm(struct tm
*tm
)
64 int year
, month
, total_days
;
66 int monthlen
[2][12] = {
67 /* Days per month for a regular year */
68 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
69 /* Days per month for a leap year */
70 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
73 if ((tm
->tm_mon
>= 12) ||
74 (tm
->tm_mday
>= 32) ||
75 (tm
->tm_hour
>= 24) ||
82 /* Add 365 days for each year since 1970 */
83 total_days
= 365 * (tm
->tm_year
- 70);
85 /* Add one day for each leap year since 1970 */
86 for (year
= 70; year
< tm
->tm_year
; year
++) {
87 if (bt_leapyear(1900 + year
)) {
92 /* Add days for each remaining month */
93 for (month
= 0; month
< tm
->tm_mon
; month
++) {
94 total_days
+= monthlen
[bt_leapyear(1900 + year
)][month
];
97 /* Add remaining days */
98 total_days
+= tm
->tm_mday
- 1;
100 return ((((total_days
* 24) + tm
->tm_hour
) * 60 + tm
->tm_min
) * 60 + tm
->tm_sec
);
105 #endif /* _BABELTRACE_UTC_H */
This page took 0.030575 seconds and 3 git commands to generate.