ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
time_utc.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <ctime>
5
6// UTC calendar math for ESPTime and the time component's timezone code, so neither needs libc's gmtime()
7namespace esphome::internal {
8
9static constexpr int32_t SECONDS_PER_DAY = 86400;
10
12static constexpr int MIN_VALID_YEAR = 2019;
13static constexpr time_t MIN_VALID_EPOCH = 1546300800; // 2019-01-01 00:00:00 UTC
14
16bool is_leap_year(int year);
17
19int days_in_month(int year, int month);
20
22inline int days_before_month(int year, int month) {
23 int days = 0;
24 for (int m = 1; m < month; m++)
25 days += days_in_month(year, m);
26 return days;
27}
28
30constexpr int days_to_weekday(int32_t days) {
31 const int wday = (days + 4) % 7; // Jan 1, 1970 was a Thursday
32 return wday < 0 ? wday + 7 : wday;
33}
34
36int32_t days_to_year_start(int year);
37
40inline bool epoch_in_range(time_t epoch) {
41 if constexpr (sizeof(time_t) > sizeof(int32_t)) {
42 return epoch >= -62167219200LL && epoch < 2005949145600LL;
43 }
44 return true;
45}
46
48int epoch_to_year(time_t epoch);
49
51void epoch_to_tm_utc(time_t epoch, struct tm *out_tm);
52
53} // namespace esphome::internal
uint8_t m
Definition bl0906.h:1
uint8_t month
Definition date_entity.h:1
uint16_t year
Definition date_entity.h:0
RP2040 wakeable delay uses file-scope state (alarm callback + flag) — defined in wake_rp2....
Definition datatypes.h:9
int days_before_month(int year, int month)
Days in the year before the first of a month (1-12)
Definition time_utc.h:22
bool is_leap_year(int year)
Check if a year is a leap year.
Definition time.cpp:15
bool epoch_in_range(time_t epoch)
Whether a UTC epoch is in the years 0 to 65535, which the epoch functions below take without checking...
Definition time_utc.h:40
int days_in_month(int year, int month)
Get the number of days in a month (1-12)
Definition time.cpp:45
int32_t days_to_year_start(int year)
Days from the epoch (Jan 1 1970) to Jan 1 of the given year.
Definition time.cpp:29
int epoch_to_year(time_t epoch)
Get the year of a UTC epoch.
Definition time.cpp:63
void epoch_to_tm_utc(time_t epoch, struct tm *out_tm)
Convert epoch to year/month/day/hour/min/sec (UTC)
Definition time.cpp:69
constexpr int days_to_weekday(int32_t days)
Day of the week (0 = Sunday) of a day counted from the epoch, which may be negative.
Definition time_utc.h:30