ESPHome 2025.12.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 <ctime>
6#include <string>
7
8namespace esphome {
9
10template<typename T> bool increment_time_value(T &current, uint16_t begin, uint16_t end);
11
12uint8_t days_in_month(uint8_t month, uint16_t year);
13
15struct ESPTime {
19 uint8_t second;
21 uint8_t minute;
23 uint8_t hour;
25 uint8_t day_of_week;
27 uint8_t day_of_month;
29 uint16_t day_of_year;
31 uint8_t month;
33 uint16_t year;
35 bool is_dst;
37 time_t timestamp;
38
44 size_t strftime(char *buffer, size_t buffer_len, const char *format);
45
55 std::string strftime(const std::string &format);
56
58 std::string strftime(const char *format);
59
61 bool is_valid() const { return this->year >= 2019 && this->fields_in_range(); }
62
64 bool fields_in_range() const {
65 return this->second < 61 && this->minute < 60 && this->hour < 24 && this->day_of_week > 0 &&
66 this->day_of_week < 8 && this->day_of_month > 0 && this->day_of_month < 32 && this->day_of_year > 0 &&
67 this->day_of_year < 367 && this->month > 0 && this->month < 13;
68 }
69
75 static bool strptime(const std::string &time_to_parse, ESPTime &esp_time);
76
78 static ESPTime from_c_tm(struct tm *c_tm, time_t c_time);
79
85 static ESPTime from_epoch_local(time_t epoch) {
86 struct tm *c_tm = ::localtime(&epoch);
87 if (c_tm == nullptr) {
88 return ESPTime{}; // Return an invalid ESPTime
89 }
90 return ESPTime::from_c_tm(c_tm, epoch);
91 }
97 static ESPTime from_epoch_utc(time_t epoch) {
98 struct tm *c_tm = ::gmtime(&epoch);
99 if (c_tm == nullptr) {
100 return ESPTime{}; // Return an invalid ESPTime
101 }
102 return ESPTime::from_c_tm(c_tm, epoch);
103 }
104
106 void recalc_timestamp_utc(bool use_day_of_year = true);
107
110
112 struct tm to_c_tm();
113
114 static int32_t timezone_offset();
115
117 void increment_second();
119 void increment_day();
120 bool operator<(const ESPTime &other) const;
121 bool operator<=(const ESPTime &other) const;
122 bool operator==(const ESPTime &other) const;
123 bool operator>=(const ESPTime &other) const;
124 bool operator>(const ESPTime &other) const;
125};
126} // 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:8
bool increment_time_value(T &current, uint16_t begin, uint16_t end)
Definition time.cpp:215
A more user-friendly version of struct tm from time.h.
Definition time.h:15
void increment_second()
Increment this clock instance by one second.
Definition time.cpp:112
static int32_t timezone_offset()
Definition time.cpp:199
uint8_t minute
minutes after the hour [0-59]
Definition time.h:21
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:158
static ESPTime from_epoch_local(time_t epoch)
Convert an UTC epoch timestamp to a local time ESPTime instance.
Definition time.h:85
bool operator<(const ESPTime &other) const
Definition time.cpp:209
bool is_dst
daylight saving time flag
Definition time.h:35
uint8_t second
seconds after the minute [0-60]
Definition time.h:19
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 operator>(const ESPTime &other) const
Definition time.cpp:213
uint8_t hour
hours since midnight [0-23]
Definition time.h:23
time_t timestamp
unix epoch time (seconds since UTC Midnight January 1, 1970)
Definition time.h:37
void increment_day()
Increment this clock instance by one day.
Definition time.cpp:140
uint16_t day_of_year
day of the year [1-366]
Definition time.h:29
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:61
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:20
bool is_valid() const
Check if this ESPTime is valid (all fields in range and year is greater than 2018)
Definition time.h:61
bool operator<=(const ESPTime &other) const
Definition time.cpp:210
bool operator==(const ESPTime &other) const
Definition time.cpp:211
void recalc_timestamp_local()
Recalculate the timestamp field from the other fields of this ESPTime instance assuming local fields.
Definition time.cpp:185
static ESPTime from_epoch_utc(time_t epoch)
Convert an UTC epoch timestamp to a UTC time ESPTime instance.
Definition time.h:97
uint8_t day_of_month
day of the month [1-31]
Definition time.h:27
struct tm to_c_tm()
Convert this ESPTime instance back to a tm struct.
Definition time.cpp:35
uint16_t year
year
Definition time.h:33
bool fields_in_range() const
Check if all time fields of this ESPTime are in range.
Definition time.h:64
uint8_t month
month; january=1 [1-12]
Definition time.h:31
bool operator>=(const ESPTime &other) const
Definition time.cpp:212
uint8_t day_of_week
day of the week; sunday=1 [1-7]
Definition time.h:25
uint8_t end[39]
Definition sun_gtil2.cpp:17