Coverage Report

Created: 2026-06-09 06:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/open62541_15/deps/libc_time.c
Line
Count
Source
1
/* Originally released by the musl project (http://www.musl-libc.org/) under the
2
 * MIT license. Taken from the file /src/time/__secs_to_tm.c */
3
4
#include <limits.h>
5
#include "libc_time.h"
6
7
/* 2000-03-01 (mod 400 year, immediately after feb29 */
8
32.2k
#define LEAPOCH (946684800LL + 86400*(31+29))
9
10
87.7k
#define DAYS_PER_400Y (365*400 + 97)
11
64.4k
#define DAYS_PER_100Y (365*100 + 24)
12
64.4k
#define DAYS_PER_4Y   (365*4   + 1)
13
14
int
15
32.2k
musl_secs_to_tm(long long t, struct musl_tm *tm) {
16
32.2k
    long long days, secs, years;
17
32.2k
    int remdays, remsecs, remyears;
18
32.2k
    int qc_cycles, c_cycles, q_cycles;
19
32.2k
    int months;
20
32.2k
    int wday, yday, leap;
21
32.2k
    static const char days_in_month[] = {31,30,31,30,31,31,30,31,30,31,31,29};
22
23
    /* Reject time_t values whose year would overflow int */
24
32.2k
    if (t < INT_MIN * 31622400LL || t > INT_MAX * 31622400LL)
25
0
        return -1;
26
27
32.2k
    secs = t - LEAPOCH;
28
32.2k
    days = secs / 86400LL;
29
32.2k
    remsecs = (int)(secs % 86400);
30
32.2k
    if (remsecs < 0) {
31
19.0k
        remsecs += 86400;
32
19.0k
        --days;
33
19.0k
    }
34
35
32.2k
    wday = (int)((3+days)%7);
36
32.2k
    if (wday < 0) wday += 7;
37
38
32.2k
    qc_cycles = (int)(days / DAYS_PER_400Y);
39
32.2k
    remdays = (int)(days % DAYS_PER_400Y);
40
32.2k
    if (remdays < 0) {
41
23.3k
        remdays += DAYS_PER_400Y;
42
23.3k
        --qc_cycles;
43
23.3k
    }
44
45
32.2k
    c_cycles = remdays / DAYS_PER_100Y;
46
32.2k
    if (c_cycles == 4) --c_cycles;
47
32.2k
    remdays -= c_cycles * DAYS_PER_100Y;
48
49
32.2k
    q_cycles = remdays / DAYS_PER_4Y;
50
32.2k
    if (q_cycles == 25) --q_cycles;
51
32.2k
    remdays -= q_cycles * DAYS_PER_4Y;
52
53
32.2k
    remyears = remdays / 365;
54
32.2k
    if (remyears == 4) --remyears;
55
32.2k
    remdays -= remyears * 365;
56
57
32.2k
    leap = !remyears && (q_cycles || !c_cycles);
58
32.2k
    yday = remdays + 31 + 28 + leap;
59
32.2k
    if (yday >= 365+leap) yday -= 365+leap;
60
61
32.2k
    years = remyears + 4*q_cycles + 100*c_cycles + 400LL*qc_cycles;
62
63
272k
    for (months=0; days_in_month[months] <= remdays; months++)
64
240k
        remdays -= days_in_month[months];
65
66
32.2k
    if (months >= 10) {
67
15.2k
        months -= 12;
68
15.2k
        years++;
69
15.2k
    }
70
71
32.2k
    if (years+100 > INT_MAX || years+100 < INT_MIN)
72
0
        return -1;
73
74
32.2k
    tm->tm_year = (int)(years + 100);
75
32.2k
    tm->tm_mon = months + 2;
76
32.2k
    tm->tm_mday = remdays + 1;
77
32.2k
    tm->tm_wday = wday;
78
32.2k
    tm->tm_yday = yday;
79
80
32.2k
    tm->tm_hour = remsecs / 3600;
81
32.2k
    tm->tm_min = remsecs / 60 % 60;
82
32.2k
    tm->tm_sec = remsecs % 60;
83
84
32.2k
    return 0;
85
32.2k
}
86
87
static const int secs_through_month[] =
88
    {0, 31*86400, 59*86400, 90*86400,
89
     120*86400, 151*86400, 181*86400, 212*86400,
90
     243*86400, 273*86400, 304*86400, 334*86400 };
91
92
static int
93
84.4k
musl_month_to_secs(int month, int is_leap) {
94
84.4k
    int t = secs_through_month[month];
95
84.4k
    if (is_leap && month >= 2)
96
33.8k
        t+=86400;
97
84.4k
    return t;
98
84.4k
}
99
100
static long long
101
84.4k
musl_year_to_secs(const long long year, int *is_leap) {
102
84.4k
    if (year-2ULL <= 136) {
103
8.89k
        int y = (int)year;
104
8.89k
        int leaps = (y-68)>>2;
105
8.89k
        if (!((y-68)&3)) {
106
5.61k
            leaps--;
107
5.61k
            if (is_leap) *is_leap = 1;
108
5.61k
        } else if (is_leap) *is_leap = 0;
109
8.89k
        return 31536000*(y-70) + 86400*leaps;
110
8.89k
    }
111
112
75.5k
    int cycles, centuries, leaps, rem, dummy;
113
114
75.5k
    if (!is_leap) is_leap = &dummy;
115
75.5k
    cycles = (int)((year-100) / 400);
116
75.5k
    rem = (int)((year-100) % 400);
117
75.5k
    if (rem < 0) {
118
36.7k
        cycles--;
119
36.7k
        rem += 400;
120
36.7k
    }
121
75.5k
    if (!rem) {
122
11.4k
        *is_leap = 1;
123
11.4k
        centuries = 0;
124
11.4k
        leaps = 0;
125
64.1k
    } else {
126
64.1k
        if (rem >= 200) {
127
18.0k
            if (rem >= 300) centuries = 3, rem -= 300;
128
7.59k
            else centuries = 2, rem -= 200;
129
46.0k
        } else {
130
46.0k
            if (rem >= 100) centuries = 1, rem -= 100;
131
36.9k
            else centuries = 0;
132
46.0k
        }
133
64.1k
        if (!rem) {
134
1.98k
            *is_leap = 0;
135
1.98k
            leaps = 0;
136
62.1k
        } else {
137
62.1k
            leaps = rem / 4;
138
62.1k
            rem %= 4;
139
62.1k
            *is_leap = !rem;
140
62.1k
        }
141
64.1k
    }
142
143
75.5k
    leaps += 97*cycles + 24*centuries - *is_leap;
144
145
75.5k
    return (year-100) * 31536000LL + leaps * 86400LL + 946684800 + 86400;
146
84.4k
}
147
148
long long
149
84.4k
musl_tm_to_secs(const struct musl_tm *tm) {
150
84.4k
    int is_leap;
151
84.4k
    long long year = tm->tm_year;
152
84.4k
    int month = tm->tm_mon;
153
84.4k
    if (month >= 12 || month < 0) {
154
62.9k
        int adj = month / 12;
155
62.9k
        month %= 12;
156
62.9k
        if (month < 0) {
157
25.2k
            adj--;
158
25.2k
            month += 12;
159
25.2k
        }
160
62.9k
        year += adj;
161
62.9k
    }
162
84.4k
    long long t = musl_year_to_secs(year, &is_leap);
163
84.4k
    t += musl_month_to_secs(month, is_leap);
164
84.4k
    t += 86400LL * (tm->tm_mday-1);
165
84.4k
    t += 3600LL * tm->tm_hour;
166
84.4k
    t += 60LL * tm->tm_min;
167
84.4k
    t += tm->tm_sec;
168
84.4k
    return t;
169
84.4k
}