2 * SPDX-License-Identifier: MIT
4 * Copyright (C) 2011-2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 #ifndef _BABELTRACE_UTC_H
8 #define _BABELTRACE_UTC_H
12 /* If set, use GNU or BSD timegm(3) */
13 #if defined(_BSD_SOURCE) || defined(_SVID_SOURCE)
16 time_t bt_timegm(struct tm
*tm
)
21 #elif defined(__MINGW32__)
24 time_t bt_timegm(struct tm
*tm
)
34 * This is a simple implementation of timegm() it just turns the "struct tm" into
35 * a GMT time_t. It does not normalize any of the fields of the "struct tm", nor
36 * does it set tm_wday or tm_yday.
40 int bt_leapyear(int year
)
42 return ((year
% 4) == 0 && ((year
% 100) != 0 || (year
% 400) == 0));
46 time_t bt_timegm(struct tm
*tm
)
48 int year
, month
, total_days
;
50 int monthlen
[2][12] = {
51 /* Days per month for a regular year */
52 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
53 /* Days per month for a leap year */
54 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
57 if ((tm
->tm_mon
>= 12) ||
58 (tm
->tm_mday
>= 32) ||
59 (tm
->tm_hour
>= 24) ||
66 /* Add 365 days for each year since 1970 */
67 total_days
= 365 * (tm
->tm_year
- 70);
69 /* Add one day for each leap year since 1970 */
70 for (year
= 70; year
< tm
->tm_year
; year
++) {
71 if (bt_leapyear(1900 + year
)) {
76 /* Add days for each remaining month */
77 for (month
= 0; month
< tm
->tm_mon
; month
++) {
78 total_days
+= monthlen
[bt_leapyear(1900 + year
)][month
];
81 /* Add remaining days */
82 total_days
+= tm
->tm_mday
- 1;
84 return ((((total_days
* 24) + tm
->tm_hour
) * 60 + tm
->tm_min
) * 60 + tm
->tm_sec
);
89 #endif /* _BABELTRACE_UTC_H */
This page took 0.041502 seconds and 4 git commands to generate.