LexLeo
0.0.0-dev+f8e5087-dirty
Technical documentation
Loading...
Searching...
No Matches
logger_default_utc_timestamp.c
Go to the documentation of this file.
1
/* SPDX-License-Identifier: GPL-3.0-or-later
2
* Copyright (C) 2026 Sylvain Labopin
3
*/
4
15
#include "
internal/logger_default_utc_timestamp.h
"
16
17
#include "
osal/time/osal_time_types.h
"
18
35
static
bool
logger_default_is_leap_year
(int32_t year)
36
{
37
return
((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
38
}
39
49
static
int32_t
logger_default_days_in_year
(int32_t year)
50
{
51
return
logger_default_is_leap_year
(year) ? 366 : 365;
52
}
53
66
static
int32_t
logger_default_days_in_month
(int32_t year, int32_t month)
67
{
68
static
const
int32_t DAYS_PER_MONTH[12] = {
69
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
70
};
71
72
int32_t days = DAYS_PER_MONTH[month - 1];
73
74
if
(month == 2 &&
logger_default_is_leap_year
(year)) {
75
days = 29;
76
}
77
78
return
days;
79
}
80
102
bool
logger_default_epoch_time_to_date
(
103
logger_default_utc_timestamp_t
*out,
104
const
osal_time_t
*time)
105
{
106
if
(!out || !time) {
107
return
false
;
108
}
109
110
int64_t epoch_seconds = time->
epoch_seconds
;
111
112
int64_t days = epoch_seconds / 86400;
113
int64_t rem = epoch_seconds % 86400;
114
115
if
(rem < 0) {
116
rem += 86400;
117
days -= 1;
118
}
119
120
out->
hour
= (int32_t)(rem / 3600);
121
rem %= 3600;
122
out->
minute
= (int32_t)(rem / 60);
123
out->
second
= (int32_t)(rem % 60);
124
125
int32_t year = 1970;
126
127
if
(days >= 0) {
128
while
(days >=
logger_default_days_in_year
(year)) {
129
days -=
logger_default_days_in_year
(year);
130
year++;
131
}
132
}
else
{
133
do
{
134
year--;
135
days +=
logger_default_days_in_year
(year);
136
}
while
(days < 0);
137
}
138
139
out->
year
= year;
140
141
int32_t month = 1;
142
while
(days >=
logger_default_days_in_month
(year, month)) {
143
days -=
logger_default_days_in_month
(year, month);
144
month++;
145
}
146
147
out->
month
= month;
148
out->
day
= (int32_t)days + 1;
149
150
return
true
;
151
}
logger_default_days_in_month
static int32_t logger_default_days_in_month(int32_t year, int32_t month)
Return the number of days in a Gregorian calendar month.
Definition
logger_default_utc_timestamp.c:66
logger_default_days_in_year
static int32_t logger_default_days_in_year(int32_t year)
Return the number of days in a Gregorian calendar year.
Definition
logger_default_utc_timestamp.c:49
logger_default_is_leap_year
static bool logger_default_is_leap_year(int32_t year)
Return whether a Gregorian calendar year is leap.
Definition
logger_default_utc_timestamp.c:35
logger_default_epoch_time_to_date
bool logger_default_epoch_time_to_date(logger_default_utc_timestamp_t *out, const osal_time_t *time)
Convert an epoch-time value to a decomposed UTC timestamp.
Definition
logger_default_utc_timestamp.c:102
logger_default_utc_timestamp.h
Private UTC timestamp conversion helpers for the logger_default adapter.
osal_time_types.h
logger_default_utc_timestamp_t
Private UTC timestamp representation used by logger_default.
Definition
logger_default_utc_timestamp.h:31
logger_default_utc_timestamp_t::minute
int32_t minute
Definition
logger_default_utc_timestamp.h:45
logger_default_utc_timestamp_t::second
int32_t second
Definition
logger_default_utc_timestamp.h:48
logger_default_utc_timestamp_t::year
int32_t year
Definition
logger_default_utc_timestamp.h:33
logger_default_utc_timestamp_t::hour
int32_t hour
Definition
logger_default_utc_timestamp.h:42
logger_default_utc_timestamp_t::day
int32_t day
Definition
logger_default_utc_timestamp.h:39
logger_default_utc_timestamp_t::month
int32_t month
Definition
logger_default_utc_timestamp.h:36
osal_time_t
Definition
osal_time_types.h:12
osal_time_t::epoch_seconds
int64_t epoch_seconds
Definition
osal_time_types.h:13
src
foundation
logger
adapters
logger_default
src
internal
logger_default_utc_timestamp.c
Generated by
1.9.8