Coverage Report

Created: 2026-08-31 07:11

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