ESPHome 2026.1.4
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 <ctime>
6#include <span>
7#include <string>
8
9namespace esphome {
10
11template<typename T> bool increment_time_value(T &current, uint16_t begin, uint16_t end);
12
13uint8_t days_in_month(uint8_t month, uint16_t year);
14
16struct ESPTime {
18 static constexpr size_t STRFTIME_BUFFER_SIZE = 128;
19
23 uint8_t second;
25 uint8_t minute;
27 uint8_t hour;
29 uint8_t day_of_week;
31 uint8_t day_of_month;
33 uint16_t day_of_year;
35 uint8_t month;
37 uint16_t year;
39 bool is_dst;
41 time_t timestamp;
42
48 size_t strftime(char *buffer, size_t buffer_len, const char *format);
49
56 size_t strftime_to(std::span<char, STRFTIME_BUFFER_SIZE> buffer, const char *format);
57
67 std::string strftime(const std::string &format);
68
70 std::string strftime(const char *format);
71
73 bool is_valid() const { return this->year >= 2019 && this->fields_in_range(); }
74
76 bool fields_in_range() const {
77 return this->second < 61 && this->minute < 60 && this->hour < 24 && this->day_of_week > 0 &&
78 this->day_of_week < 8 && this->day_of_month > 0 && this->day_of_month < 32 && this->day_of_year > 0 &&
79 this->day_of_year < 367 && this->month > 0 && this->month < 13;
80 }
81
87 static bool strptime(const std::string &time_to_parse, ESPTime &esp_time);
88
90 static ESPTime from_c_tm(struct tm *c_tm, time_t c_time);
91
97 static ESPTime from_epoch_local(time_t epoch) {
98 struct tm *c_tm = ::localtime(&epoch);
99 if (c_tm == nullptr) {
100 return ESPTime{}; // Return an invalid ESPTime
101 }
102 return ESPTime::from_c_tm(c_tm, epoch);
103 }
109 static ESPTime from_epoch_utc(time_t epoch) {
110 struct tm *c_tm = ::gmtime(&epoch);
111 if (c_tm == nullptr) {
112 return ESPTime{}; // Return an invalid ESPTime
113 }
114 return ESPTime::from_c_tm(c_tm, epoch);
115 }
116
118 void recalc_timestamp_utc(bool use_day_of_year = true);
119
122
124 struct tm to_c_tm();
125
126 static int32_t timezone_offset();
127
129 void increment_second();
131 void increment_day();
132 bool operator<(const ESPTime &other) const;
133 bool operator<=(const ESPTime &other) const;
134 bool operator==(const ESPTime &other) const;
135 bool operator>=(const ESPTime &other) const;
136 bool operator>(const ESPTime &other) const;
137};
138} // 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
bool increment_time_value(T &current, uint16_t begin, uint16_t end)
Definition time.cpp:224
A more user-friendly version of struct tm from time.h.
Definition time.h:16
void increment_second()
Increment this clock instance by one second.
Definition time.cpp:121
static int32_t timezone_offset()
Definition time.cpp:208
uint8_t minute
minutes after the hour [0-59]
Definition time.h:25
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:167
static ESPTime from_epoch_local(time_t epoch)
Convert an UTC epoch timestamp to a local time ESPTime instance.
Definition time.h:97
bool operator<(const ESPTime &other) const
Definition time.cpp:218
bool is_dst
daylight saving time flag
Definition time.h:39
uint8_t second
seconds after the minute [0-60]
Definition time.h:23
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
bool operator>(const ESPTime &other) const
Definition time.cpp:222
uint8_t hour
hours since midnight [0-23]
Definition time.h:27
time_t timestamp
unix epoch time (seconds since UTC Midnight January 1, 1970)
Definition time.h:41
void increment_day()
Increment this clock instance by one day.
Definition time.cpp:149
uint16_t day_of_year
day of the year [1-366]
Definition time.h:33
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.cpp:70
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
bool is_valid() const
Check if this ESPTime is valid (all fields in range and year is greater than 2018)
Definition time.h:73
bool operator<=(const ESPTime &other) const
Definition time.cpp:219
bool operator==(const ESPTime &other) const
Definition time.cpp:220
void recalc_timestamp_local()
Recalculate the timestamp field from the other fields of this ESPTime instance assuming local fields.
Definition time.cpp:194
static ESPTime from_epoch_utc(time_t epoch)
Convert an UTC epoch timestamp to a UTC time ESPTime instance.
Definition time.h:109
static constexpr size_t STRFTIME_BUFFER_SIZE
Buffer size required for strftime output.
Definition time.h:18
uint8_t day_of_month
day of the month [1-31]
Definition time.h:31
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:37
bool fields_in_range() const
Check if all time fields of this ESPTime are in range.
Definition time.h:76
uint8_t month
month; january=1 [1-12]
Definition time.h:35
bool operator>=(const ESPTime &other) const
Definition time.cpp:221
uint8_t day_of_week
day of the week; sunday=1 [1-7]
Definition time.h:29
uint8_t end[39]
Definition sun_gtil2.cpp:17