ESPHome 2026.2.3
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
10namespace esphome {
11
12template<typename T> bool increment_time_value(T &current, uint16_t begin, uint16_t end);
13
14uint8_t days_in_month(uint8_t month, uint16_t year);
15
17struct ESPTime {
19 static constexpr size_t STRFTIME_BUFFER_SIZE = 128;
20
24 uint8_t second;
26 uint8_t minute;
28 uint8_t hour;
30 uint8_t day_of_week;
32 uint8_t day_of_month;
34 uint16_t day_of_year;
36 uint8_t month;
38 uint16_t year;
40 bool is_dst;
42 time_t timestamp;
43
49 size_t strftime(char *buffer, size_t buffer_len, const char *format);
50
57 size_t strftime_to(std::span<char, STRFTIME_BUFFER_SIZE> buffer, const char *format);
58
68 std::string strftime(const std::string &format);
69
71 std::string strftime(const char *format);
72
74 bool is_valid() const { return this->year >= 2019 && this->fields_in_range(); }
75
77 bool fields_in_range() const {
78 return this->second < 61 && this->minute < 60 && this->hour < 24 && this->day_of_week > 0 &&
79 this->day_of_week < 8 && this->day_of_month > 0 && this->day_of_month < 32 && this->day_of_year > 0 &&
80 this->day_of_year < 367 && this->month > 0 && this->month < 13;
81 }
82
89 static bool strptime(const char *time_to_parse, size_t len, ESPTime &esp_time);
91 static bool strptime(const char *time_to_parse, ESPTime &esp_time) {
92 return strptime(time_to_parse, strlen(time_to_parse), esp_time);
93 }
95 static bool strptime(const std::string &time_to_parse, ESPTime &esp_time) {
96 return strptime(time_to_parse.c_str(), time_to_parse.size(), esp_time);
97 }
98
100 static ESPTime from_c_tm(struct tm *c_tm, time_t c_time);
101
107 static ESPTime from_epoch_local(time_t epoch) {
108 struct tm *c_tm = ::localtime(&epoch);
109 if (c_tm == nullptr) {
110 return ESPTime{}; // Return an invalid ESPTime
111 }
112 return ESPTime::from_c_tm(c_tm, epoch);
113 }
119 static ESPTime from_epoch_utc(time_t epoch) {
120 struct tm *c_tm = ::gmtime(&epoch);
121 if (c_tm == nullptr) {
122 return ESPTime{}; // Return an invalid ESPTime
123 }
124 return ESPTime::from_c_tm(c_tm, epoch);
125 }
126
128 void recalc_timestamp_utc(bool use_day_of_year = true);
129
132
134 struct tm to_c_tm();
135
136 static int32_t timezone_offset();
137
139 void increment_second();
141 void increment_day();
142 bool operator<(const ESPTime &other) const;
143 bool operator<=(const ESPTime &other) const;
144 bool operator==(const ESPTime &other) const;
145 bool operator>=(const ESPTime &other) const;
146 bool operator>(const ESPTime &other) const;
147};
148} // namespace esphome
uint8_t month
Definition date_entity.h:1
uint16_t year
Definition date_entity.h:0
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:9
std::string size_t len
Definition helpers.h:692
bool increment_time_value(T &current, uint16_t begin, uint16_t end)
Definition time.cpp:225
A more user-friendly version of struct tm from time.h.
Definition time.h:17
void increment_second()
Increment this clock instance by one second.
Definition time.cpp:122
static int32_t timezone_offset()
Definition time.cpp:209
uint8_t minute
minutes after the hour [0-59]
Definition time.h:26
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:168
static ESPTime from_epoch_local(time_t epoch)
Convert an UTC epoch timestamp to a local time ESPTime instance.
Definition time.h:107
bool operator<(const ESPTime &other) const
Definition time.cpp:219
bool is_dst
daylight saving time flag
Definition time.h:40
uint8_t second
seconds after the minute [0-60]
Definition time.h:24
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:16
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:21
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:95
bool operator>(const ESPTime &other) const
Definition time.cpp:223
uint8_t hour
hours since midnight [0-23]
Definition time.h:28
time_t timestamp
unix epoch time (seconds since UTC Midnight January 1, 1970)
Definition time.h:42
void increment_day()
Increment this clock instance by one day.
Definition time.cpp:150
uint16_t day_of_year
day of the year [1-366]
Definition time.h:34
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:33
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:70
bool is_valid() const
Check if this ESPTime is valid (all fields in range and year is greater than 2018)
Definition time.h:74
bool operator<=(const ESPTime &other) const
Definition time.cpp:220
bool operator==(const ESPTime &other) const
Definition time.cpp:221
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:91
void recalc_timestamp_local()
Recalculate the timestamp field from the other fields of this ESPTime instance assuming local fields.
Definition time.cpp:195
static ESPTime from_epoch_utc(time_t epoch)
Convert an UTC epoch timestamp to a UTC time ESPTime instance.
Definition time.h:119
static constexpr size_t STRFTIME_BUFFER_SIZE
Buffer size required for strftime output.
Definition time.h:19
uint8_t day_of_month
day of the month [1-31]
Definition time.h:32
struct tm to_c_tm()
Convert this ESPTime instance back to a tm struct.
Definition time.cpp:48
uint16_t year
year
Definition time.h:38
bool fields_in_range() const
Check if all time fields of this ESPTime are in range.
Definition time.h:77
uint8_t month
month; january=1 [1-12]
Definition time.h:36
bool operator>=(const ESPTime &other) const
Definition time.cpp:222
uint8_t day_of_week
day of the week; sunday=1 [1-7]
Definition time.h:30
uint8_t end[39]
Definition sun_gtil2.cpp:17