Coverage Report

Created: 2026-05-16 06:54

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