Coverage Report

Created: 2026-07-16 06:23

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
28.1k
#define LEAPOCH (946684800LL + 86400*(31+29))
9
10
78.4k
#define DAYS_PER_400Y (365*400 + 97)
11
56.3k
#define DAYS_PER_100Y (365*100 + 24)
12
56.3k
#define DAYS_PER_4Y   (365*4   + 1)
13
14
int
15
28.1k
musl_secs_to_tm(long long t, struct musl_tm *tm) {
16
28.1k
    long long days, secs, years;
17
28.1k
    int remdays, remsecs, remyears;
18
28.1k
    int qc_cycles, c_cycles, q_cycles;
19
28.1k
    int months;
20
28.1k
    int wday, yday, leap;
21
28.1k
    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
28.1k
    if (t < INT_MIN * 31622400LL || t > INT_MAX * 31622400LL)
25
0
        return -1;
26
27
28.1k
    secs = t - LEAPOCH;
28
28.1k
    days = secs / 86400LL;
29
28.1k
    remsecs = (int)(secs % 86400);
30
28.1k
    if (remsecs < 0) {
31
18.3k
        remsecs += 86400;
32
18.3k
        --days;
33
18.3k
    }
34
35
28.1k
    wday = (int)((3+days)%7);
36
28.1k
    if (wday < 0) wday += 7;
37
38
28.1k
    qc_cycles = (int)(days / DAYS_PER_400Y);
39
28.1k
    remdays = (int)(days % DAYS_PER_400Y);
40
28.1k
    if (remdays < 0) {
41
22.1k
        remdays += DAYS_PER_400Y;
42
22.1k
        --qc_cycles;
43
22.1k
    }
44
45
28.1k
    c_cycles = remdays / DAYS_PER_100Y;
46
28.1k
    if (c_cycles == 4) --c_cycles;
47
28.1k
    remdays -= c_cycles * DAYS_PER_100Y;
48
49
28.1k
    q_cycles = remdays / DAYS_PER_4Y;
50
28.1k
    if (q_cycles == 25) --q_cycles;
51
28.1k
    remdays -= q_cycles * DAYS_PER_4Y;
52
53
28.1k
    remyears = remdays / 365;
54
28.1k
    if (remyears == 4) --remyears;
55
28.1k
    remdays -= remyears * 365;
56
57
28.1k
    leap = !remyears && (q_cycles || !c_cycles);
58
28.1k
    yday = remdays + 31 + 28 + leap;
59
28.1k
    if (yday >= 365+leap) yday -= 365+leap;
60
61
28.1k
    years = remyears + 4*q_cycles + 100*c_cycles + 400LL*qc_cycles;
62
63
240k
    for (months=0; days_in_month[months] <= remdays; months++)
64
212k
        remdays -= days_in_month[months];
65
66
28.1k
    if (months >= 10) {
67
14.0k
        months -= 12;
68
14.0k
        years++;
69
14.0k
    }
70
71
28.1k
    if (years+100 > INT_MAX || years+100 < INT_MIN)
72
0
        return -1;
73
74
28.1k
    tm->tm_year = (int)(years + 100);
75
28.1k
    tm->tm_mon = months + 2;
76
28.1k
    tm->tm_mday = remdays + 1;
77
28.1k
    tm->tm_wday = wday;
78
28.1k
    tm->tm_yday = yday;
79
80
28.1k
    tm->tm_hour = remsecs / 3600;
81
28.1k
    tm->tm_min = remsecs / 60 % 60;
82
28.1k
    tm->tm_sec = remsecs % 60;
83
84
28.1k
    return 0;
85
28.1k
}
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
53.9k
musl_month_to_secs(int month, int is_leap) {
94
53.9k
    int t = secs_through_month[month];
95
53.9k
    if (is_leap && month >= 2)
96
17.0k
        t+=86400;
97
53.9k
    return t;
98
53.9k
}
99
100
static long long
101
53.9k
musl_year_to_secs(const long long year, int *is_leap) {
102
53.9k
    if (year-2ULL <= 136) {
103
4.96k
        int y = (int)year;
104
4.96k
        int leaps = (y-68)>>2;
105
4.96k
        if (!((y-68)&3)) {
106
2.60k
            leaps--;
107
2.60k
            if (is_leap) *is_leap = 1;
108
2.60k
        } else if (is_leap) *is_leap = 0;
109
4.96k
        return 31536000*(y-70) + 86400*leaps;
110
4.96k
    }
111
112
48.9k
    int cycles, centuries, leaps, rem, dummy;
113
114
48.9k
    if (!is_leap) is_leap = &dummy;
115
48.9k
    cycles = (int)((year-100) / 400);
116
48.9k
    rem = (int)((year-100) % 400);
117
48.9k
    if (rem < 0) {
118
29.6k
        cycles--;
119
29.6k
        rem += 400;
120
29.6k
    }
121
48.9k
    if (!rem) {
122
7.22k
        *is_leap = 1;
123
7.22k
        centuries = 0;
124
7.22k
        leaps = 0;
125
41.7k
    } else {
126
41.7k
        if (rem >= 200) {
127
14.2k
            if (rem >= 300) centuries = 3, rem -= 300;
128
4.65k
            else centuries = 2, rem -= 200;
129
27.4k
        } else {
130
27.4k
            if (rem >= 100) centuries = 1, rem -= 100;
131
22.1k
            else centuries = 0;
132
27.4k
        }
133
41.7k
        if (!rem) {
134
1.84k
            *is_leap = 0;
135
1.84k
            leaps = 0;
136
39.9k
        } else {
137
39.9k
            leaps = rem / 4;
138
39.9k
            rem %= 4;
139
39.9k
            *is_leap = !rem;
140
39.9k
        }
141
41.7k
    }
142
143
48.9k
    leaps += 97*cycles + 24*centuries - *is_leap;
144
145
48.9k
    return (year-100) * 31536000LL + leaps * 86400LL + 946684800 + 86400;
146
53.9k
}
147
148
long long
149
53.9k
musl_tm_to_secs(const struct musl_tm *tm) {
150
53.9k
    int is_leap;
151
53.9k
    long long year = tm->tm_year;
152
53.9k
    int month = tm->tm_mon;
153
53.9k
    if (month >= 12 || month < 0) {
154
33.7k
        int adj = month / 12;
155
33.7k
        month %= 12;
156
33.7k
        if (month < 0) {
157
12.4k
            adj--;
158
12.4k
            month += 12;
159
12.4k
        }
160
33.7k
        year += adj;
161
33.7k
    }
162
53.9k
    long long t = musl_year_to_secs(year, &is_leap);
163
53.9k
    t += musl_month_to_secs(month, is_leap);
164
53.9k
    t += 86400LL * (tm->tm_mday-1);
165
53.9k
    t += 3600LL * tm->tm_hour;
166
53.9k
    t += 60LL * tm->tm_min;
167
53.9k
    t += tm->tm_sec;
168
53.9k
    return t;
169
53.9k
}