ESPHome 2026.5.0-dev
Loading...
Searching...
No Matches
time.h
Go to the documentation of this file.
1#pragma once
2
4
5#include <cstdint>
6#include <cstdlib>
7#include <cstring>
8#include <ctime>
9#include <span>
10#include <string>
11
12#ifdef USE_TIME_TIMEZONE
14#endif
15
16namespace esphome {
17
18template<typename T> bool increment_time_value(T &current, uint16_t begin, uint16_t end);
19
20uint8_t days_in_month(uint8_t month, uint16_t year);
21
23struct ESPTime {
25 static constexpr size_t STRFTIME_BUFFER_SIZE = 128;
26
30 uint8_t second;
32 uint8_t minute;
34 uint8_t hour;
36 uint8_t day_of_week;
38 uint8_t day_of_month;
40 uint16_t day_of_year;
42 uint8_t month;
44 uint16_t year;
46 bool is_dst;
48 time_t timestamp;
49
55 size_t strftime(char *buffer, size_t buffer_len, const char *format);
56
63 size_t strftime_to(std::span<char, STRFTIME_BUFFER_SIZE> buffer, const char *format);
64
74 std::string strftime(const std::string &format);
75
77 std::string strftime(const char *format);
78
82 bool is_valid(bool check_day_of_week = true, bool check_day_of_year = true) const {
83 return this->year >= 2019 && this->fields_in_range(check_day_of_week, check_day_of_year);
84 }
85
89 bool fields_in_range(bool check_day_of_week = true, bool check_day_of_year = true) const {
90 bool valid = this->second < 61 && this->minute < 60 && this->hour < 24 && this->month > 0 && this->month < 13 &&
91 this->day_of_month > 0 && this->day_of_month <= days_in_month(this->month, this->year);
92 if (check_day_of_week) {
93 valid = valid && this->day_of_week > 0 && this->day_of_week < 8;
94 }
95 if (check_day_of_year) {
96 valid = valid && this->day_of_year > 0 && this->day_of_year < 367;
97 }
98 return valid;
99 }
100
107 static bool strptime(const char *time_to_parse, size_t len, ESPTime &esp_time);
109 static bool strptime(const char *time_to_parse, ESPTime &esp_time) {
110 return strptime(time_to_parse, strlen(time_to_parse), esp_time);
111 }
113 static bool strptime(const std::string &time_to_parse, ESPTime &esp_time) {
114 return strptime(time_to_parse.c_str(), time_to_parse.size(), esp_time);
115 }
116
118 static ESPTime from_c_tm(struct tm *c_tm, time_t c_time);
119
125 static ESPTime from_epoch_local(time_t epoch) {
126#ifdef USE_TIME_TIMEZONE
127 struct tm local_tm;
128 if (time::epoch_to_local_tm(epoch, time::get_global_tz(), &local_tm)) {
129 return ESPTime::from_c_tm(&local_tm, epoch);
130 }
131 // Fallback to UTC if conversion failed
132 return ESPTime::from_epoch_utc(epoch);
133#else
134 // No timezone support - return UTC (no TZ configured, localtime would return UTC anyway)
135 return ESPTime::from_epoch_utc(epoch);
136#endif
137 }
143 static ESPTime from_epoch_utc(time_t epoch) {
144 struct tm *c_tm = ::gmtime(&epoch);
145 if (c_tm == nullptr) {
146 return ESPTime{}; // Return an invalid ESPTime
147 }
148 return ESPTime::from_c_tm(c_tm, epoch);
149 }
150
152 void recalc_timestamp_utc(bool use_day_of_year = true);
153
156
158 struct tm to_c_tm();
159
160 static int32_t timezone_offset();
161
163 void increment_second();
165 void increment_day();
166 bool operator<(const ESPTime &other) const;
167 bool operator<=(const ESPTime &other) const;
168 bool operator==(const ESPTime &other) const;
169 bool operator>=(const ESPTime &other) const;
170 bool operator>(const ESPTime &other) const;
171};
172} // namespace esphome
uint8_t month
Definition date_entity.h:1
uint16_t year
Definition date_entity.h:0
const ParsedTimezone & get_global_tz()
Get the global timezone.
Definition posix_tz.cpp:17
bool epoch_to_local_tm(time_t utc_epoch, const ParsedTimezone &tz, struct tm *out_tm)
Convert a UTC epoch to local time using the parsed timezone.
Definition posix_tz.cpp:465
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
const char int const __FlashStringHelper * format
Definition log.h:74
uint8_t days_in_month(uint8_t month, uint16_t year)
Definition time.cpp:11
std::string size_t len
Definition helpers.h:1045
bool increment_time_value(T &current, uint16_t begin, uint16_t end)
Definition time.cpp:375
bool valid
A more user-friendly version of struct tm from time.h.
Definition time.h:23
void increment_second()
Increment this clock instance by one second.
Definition time.cpp:236
static int32_t timezone_offset()
Definition time.cpp:353
uint8_t minute
minutes after the hour [0-59]
Definition time.h:32
void recalc_timestamp_utc(bool use_day_of_year=true)
Recalculate the timestamp field from the other fields of this ESPTime instance (must be UTC).
Definition time.cpp:282
static ESPTime from_epoch_local(time_t epoch)
Convert an UTC epoch timestamp to a local time ESPTime instance.
Definition time.h:125
bool operator<(const ESPTime &other) const
Definition time.cpp:369
bool is_dst
daylight saving time flag
Definition time.h:46
uint8_t second
seconds after the minute [0-60]
Definition time.h:30
size_t strftime(char *buffer, size_t buffer_len, const char *format)
Convert this ESPTime struct to a null-terminated c string buffer as specified by the format argument.
Definition time.cpp:18
bool fields_in_range(bool check_day_of_week=true, bool check_day_of_year=true) const
Check if time fields are in range.
Definition time.h:89
size_t strftime_to(std::span< char, STRFTIME_BUFFER_SIZE > buffer, const char *format)
Format time into a fixed-size buffer, returns length written.
Definition time.cpp:71
static bool strptime(const std::string &time_to_parse, ESPTime &esp_time)
Convert a string to ESPTime struct as specified by the format argument.
Definition time.h:113
bool operator>(const ESPTime &other) const
Definition time.cpp:373
uint8_t hour
hours since midnight [0-23]
Definition time.h:34
time_t timestamp
unix epoch time (seconds since UTC Midnight January 1, 1970)
Definition time.h:48
void increment_day()
Increment this clock instance by one day.
Definition time.cpp:264
uint16_t day_of_year
day of the year [1-366]
Definition time.h:40
static ESPTime from_c_tm(struct tm *c_tm, time_t c_time)
Convert a C tm struct instance with a C unix epoch timestamp to an ESPTime instance.
Definition time.cpp:82
static bool strptime(const char *time_to_parse, size_t len, ESPTime &esp_time)
Convert a string to ESPTime struct as specified by the format argument.
Definition time.cpp:139
bool is_valid(bool check_day_of_week=true, bool check_day_of_year=true) const
Check if this ESPTime is valid (year >= 2019 and the requested fields are in range).
Definition time.h:82
bool operator<=(const ESPTime &other) const
Definition time.cpp:370
bool operator==(const ESPTime &other) const
Definition time.cpp:371
static bool strptime(const char *time_to_parse, ESPTime &esp_time)
Convert a string to ESPTime struct as specified by the format argument.
Definition time.h:109
void recalc_timestamp_local()
Recalculate the timestamp field from the other fields of this ESPTime instance assuming local fields.
Definition time.cpp:309
static ESPTime from_epoch_utc(time_t epoch)
Convert an UTC epoch timestamp to a UTC time ESPTime instance.
Definition time.h:143
static constexpr size_t STRFTIME_BUFFER_SIZE
Buffer size required for strftime output.
Definition time.h:25
uint8_t day_of_month
day of the month [1-31]
Definition time.h:38
struct tm to_c_tm()
Convert this ESPTime instance back to a tm struct.
Definition time.cpp:97
uint16_t year
year
Definition time.h:44
uint8_t month
month; january=1 [1-12]
Definition time.h:42
bool operator>=(const ESPTime &other) const
Definition time.cpp:372
uint8_t day_of_week
day of the week; sunday=1 [1-7]
Definition time.h:36
uint8_t end[39]
Definition sun_gtil2.cpp:17