ESPHome 2026.3.0-dev
Loading...
Searching...
No Matches
time.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <cstdlib>
5#include <cstring>
6#include <ctime>
7#include <span>
8#include <string>
9
10#ifdef USE_TIME_TIMEZONE
12#endif
13
14namespace esphome {
15
16template<typename T> bool increment_time_value(T &current, uint16_t begin, uint16_t end);
17
18uint8_t days_in_month(uint8_t month, uint16_t year);
19
21struct ESPTime {
23 static constexpr size_t STRFTIME_BUFFER_SIZE = 128;
24
28 uint8_t second;
30 uint8_t minute;
32 uint8_t hour;
34 uint8_t day_of_week;
36 uint8_t day_of_month;
38 uint16_t day_of_year;
40 uint8_t month;
42 uint16_t year;
44 bool is_dst;
46 time_t timestamp;
47
53 size_t strftime(char *buffer, size_t buffer_len, const char *format);
54
61 size_t strftime_to(std::span<char, STRFTIME_BUFFER_SIZE> buffer, const char *format);
62
72 std::string strftime(const std::string &format);
73
75 std::string strftime(const char *format);
76
78 bool is_valid() const { return this->year >= 2019 && this->fields_in_range(); }
79
81 bool fields_in_range() const {
82 return this->second < 61 && this->minute < 60 && this->hour < 24 && this->day_of_week > 0 &&
83 this->day_of_week < 8 && this->day_of_year > 0 && this->day_of_year < 367 && this->month > 0 &&
84 this->month < 13 && this->day_of_month > 0 && this->day_of_month <= days_in_month(this->month, this->year);
85 }
86
93 static bool strptime(const char *time_to_parse, size_t len, ESPTime &esp_time);
95 static bool strptime(const char *time_to_parse, ESPTime &esp_time) {
96 return strptime(time_to_parse, strlen(time_to_parse), esp_time);
97 }
99 static bool strptime(const std::string &time_to_parse, ESPTime &esp_time) {
100 return strptime(time_to_parse.c_str(), time_to_parse.size(), esp_time);
101 }
102
104 static ESPTime from_c_tm(struct tm *c_tm, time_t c_time);
105
111 static ESPTime from_epoch_local(time_t epoch) {
112#ifdef USE_TIME_TIMEZONE
113 struct tm local_tm;
114 if (time::epoch_to_local_tm(epoch, time::get_global_tz(), &local_tm)) {
115 return ESPTime::from_c_tm(&local_tm, epoch);
116 }
117 // Fallback to UTC if conversion failed
118 return ESPTime::from_epoch_utc(epoch);
119#else
120 // No timezone support - return UTC (no TZ configured, localtime would return UTC anyway)
121 return ESPTime::from_epoch_utc(epoch);
122#endif
123 }
129 static ESPTime from_epoch_utc(time_t epoch) {
130 struct tm *c_tm = ::gmtime(&epoch);
131 if (c_tm == nullptr) {
132 return ESPTime{}; // Return an invalid ESPTime
133 }
134 return ESPTime::from_c_tm(c_tm, epoch);
135 }
136
138 void recalc_timestamp_utc(bool use_day_of_year = true);
139
142
144 struct tm to_c_tm();
145
146 static int32_t timezone_offset();
147
149 void increment_second();
151 void increment_day();
152 bool operator<(const ESPTime &other) const;
153 bool operator<=(const ESPTime &other) const;
154 bool operator==(const ESPTime &other) const;
155 bool operator>=(const ESPTime &other) const;
156 bool operator>(const ESPTime &other) const;
157};
158} // 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:16
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:445
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint8_t days_in_month(uint8_t month, uint16_t year)
Definition time.cpp:8
std::string size_t len
Definition helpers.h:817
bool increment_time_value(T &current, uint16_t begin, uint16_t end)
Definition time.cpp:329
A more user-friendly version of struct tm from time.h.
Definition time.h:21
void increment_second()
Increment this clock instance by one second.
Definition time.cpp:186
static int32_t timezone_offset()
Definition time.cpp:307
uint8_t minute
minutes after the hour [0-59]
Definition time.h:30
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:232
static ESPTime from_epoch_local(time_t epoch)
Convert an UTC epoch timestamp to a local time ESPTime instance.
Definition time.h:111
bool operator<(const ESPTime &other) const
Definition time.cpp:323
bool is_dst
daylight saving time flag
Definition time.h:44
uint8_t second
seconds after the minute [0-60]
Definition time.h:28
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:15
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:20
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:99
bool operator>(const ESPTime &other) const
Definition time.cpp:327
uint8_t hour
hours since midnight [0-23]
Definition time.h:32
time_t timestamp
unix epoch time (seconds since UTC Midnight January 1, 1970)
Definition time.h:46
void increment_day()
Increment this clock instance by one day.
Definition time.cpp:214
uint16_t day_of_year
day of the year [1-366]
Definition time.h:38
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:32
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:89
bool is_valid() const
Check if this ESPTime is valid (all fields in range and year is greater than or equal to 2019)
Definition time.h:78
bool operator<=(const ESPTime &other) const
Definition time.cpp:324
bool operator==(const ESPTime &other) const
Definition time.cpp:325
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:95
void recalc_timestamp_local()
Recalculate the timestamp field from the other fields of this ESPTime instance assuming local fields.
Definition time.cpp:259
static ESPTime from_epoch_utc(time_t epoch)
Convert an UTC epoch timestamp to a UTC time ESPTime instance.
Definition time.h:129
static constexpr size_t STRFTIME_BUFFER_SIZE
Buffer size required for strftime output.
Definition time.h:23
uint8_t day_of_month
day of the month [1-31]
Definition time.h:36
struct tm to_c_tm()
Convert this ESPTime instance back to a tm struct.
Definition time.cpp:47
uint16_t year
year
Definition time.h:42
bool fields_in_range() const
Check if all time fields of this ESPTime are in range.
Definition time.h:81
uint8_t month
month; january=1 [1-12]
Definition time.h:40
bool operator>=(const ESPTime &other) const
Definition time.cpp:326
uint8_t day_of_week
day of the week; sunday=1 [1-7]
Definition time.h:34
uint8_t end[39]
Definition sun_gtil2.cpp:17