ESPHome 2026.3.3
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
80 bool is_valid() const { return this->year >= 2019 && this->fields_in_range(); }
81
85 bool fields_in_range(bool check_day_of_week = true, bool check_day_of_year = true) const {
86 bool valid = this->second < 61 && this->minute < 60 && this->hour < 24 && this->month > 0 && this->month < 13 &&
87 this->day_of_month > 0 && this->day_of_month <= days_in_month(this->month, this->year);
88 if (check_day_of_week) {
89 valid = valid && this->day_of_week > 0 && this->day_of_week < 8;
90 }
91 if (check_day_of_year) {
92 valid = valid && this->day_of_year > 0 && this->day_of_year < 367;
93 }
94 return valid;
95 }
96
103 static bool strptime(const char *time_to_parse, size_t len, ESPTime &esp_time);
105 static bool strptime(const char *time_to_parse, ESPTime &esp_time) {
106 return strptime(time_to_parse, strlen(time_to_parse), esp_time);
107 }
109 static bool strptime(const std::string &time_to_parse, ESPTime &esp_time) {
110 return strptime(time_to_parse.c_str(), time_to_parse.size(), esp_time);
111 }
112
114 static ESPTime from_c_tm(struct tm *c_tm, time_t c_time);
115
121 static ESPTime from_epoch_local(time_t epoch) {
122#ifdef USE_TIME_TIMEZONE
123 struct tm local_tm;
124 if (time::epoch_to_local_tm(epoch, time::get_global_tz(), &local_tm)) {
125 return ESPTime::from_c_tm(&local_tm, epoch);
126 }
127 // Fallback to UTC if conversion failed
128 return ESPTime::from_epoch_utc(epoch);
129#else
130 // No timezone support - return UTC (no TZ configured, localtime would return UTC anyway)
131 return ESPTime::from_epoch_utc(epoch);
132#endif
133 }
139 static ESPTime from_epoch_utc(time_t epoch) {
140 struct tm *c_tm = ::gmtime(&epoch);
141 if (c_tm == nullptr) {
142 return ESPTime{}; // Return an invalid ESPTime
143 }
144 return ESPTime::from_c_tm(c_tm, epoch);
145 }
146
148 void recalc_timestamp_utc(bool use_day_of_year = true);
149
152
154 struct tm to_c_tm();
155
156 static int32_t timezone_offset();
157
159 void increment_second();
161 void increment_day();
162 bool operator<(const ESPTime &other) const;
163 bool operator<=(const ESPTime &other) const;
164 bool operator==(const ESPTime &other) const;
165 bool operator>=(const ESPTime &other) const;
166 bool operator>(const ESPTime &other) const;
167};
168} // 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
const char int const __FlashStringHelper * format
Definition log.h:74
uint8_t days_in_month(uint8_t month, uint16_t year)
Definition time.cpp:8
std::string size_t len
Definition helpers.h:892
bool increment_time_value(T &current, uint16_t begin, uint16_t end)
Definition time.cpp:325
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:186
static int32_t timezone_offset()
Definition time.cpp:303
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:232
static ESPTime from_epoch_local(time_t epoch)
Convert an UTC epoch timestamp to a local time ESPTime instance.
Definition time.h:121
bool operator<(const ESPTime &other) const
Definition time.cpp:319
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:15
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:85
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:109
bool operator>(const ESPTime &other) const
Definition time.cpp:323
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:214
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: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:80
bool operator<=(const ESPTime &other) const
Definition time.cpp:320
bool operator==(const ESPTime &other) const
Definition time.cpp:321
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:105
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:139
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:47
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:322
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