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
16
18
35static bool logger_default_is_leap_year(int32_t year)
36{
37 return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
38}
39
49static int32_t logger_default_days_in_year(int32_t year)
50{
51 return logger_default_is_leap_year(year) ? 366 : 365;
52}
53
66static 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
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}
static int32_t logger_default_days_in_month(int32_t year, int32_t month)
Return the number of days in a Gregorian calendar month.
static int32_t logger_default_days_in_year(int32_t year)
Return the number of days in a Gregorian calendar year.
static bool logger_default_is_leap_year(int32_t year)
Return whether a Gregorian calendar year is leap.
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.
Private UTC timestamp conversion helpers for the logger_default adapter.
Private UTC timestamp representation used by logger_default.
int64_t epoch_seconds