ESPHome  2024.10.3
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 
8 namespace esphome {
9 
10 template<typename T> bool increment_time_value(T &current, uint16_t begin, uint16_t end);
11 
12 bool is_leap_year(uint32_t year);
13 
14 uint8_t days_in_month(uint8_t month, uint16_t year);
15 
17 struct ESPTime {
21  uint8_t second;
23  uint8_t minute;
25  uint8_t hour;
27  uint8_t day_of_week;
29  uint8_t day_of_month;
31  uint16_t day_of_year;
33  uint8_t month;
35  uint16_t year;
37  bool is_dst;
39  time_t timestamp;
40 
46  size_t strftime(char *buffer, size_t buffer_len, const char *format);
47 
58  std::string strftime(const std::string &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  return ESPTime::from_c_tm(c_tm, epoch);
88  }
94  static ESPTime from_epoch_utc(time_t epoch) {
95  struct tm *c_tm = ::gmtime(&epoch);
96  return ESPTime::from_c_tm(c_tm, epoch);
97  }
98 
100  void recalc_timestamp_utc(bool use_day_of_year = true);
101 
103  void recalc_timestamp_local(bool use_day_of_year = true);
104 
106  struct tm to_c_tm();
107 
108  static int32_t timezone_offset();
109 
111  void increment_second();
113  void increment_day();
114  bool operator<(ESPTime other);
115  bool operator<=(ESPTime other);
116  bool operator==(ESPTime other);
117  bool operator>=(ESPTime other);
118  bool operator>(ESPTime other);
119 };
120 } // namespace esphome
uint16_t year
Definition: date_entity.h:122
static ESPTime from_epoch_utc(time_t epoch)
Convert an UTC epoch timestamp to a UTC time ESPTime instance.
Definition: time.h:94
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:18
static int32_t timezone_offset()
Definition: time.cpp:200
A more user-friendly version of struct tm from time.h.
Definition: time.h:17
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:22
bool is_dst
daylight saving time flag
Definition: time.h:37
void increment_day()
Increment this clock instance by one day.
Definition: time.cpp:145
uint16_t day_of_year
day of the year [1-366]
Definition: time.h:31
bool operator<=(ESPTime other)
Definition: time.cpp:225
void increment_second()
Increment this clock instance by one second.
Definition: time.cpp:118
uint8_t days_in_month(uint8_t month, uint16_t year)
Definition: time.cpp:10
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>(ESPTime other)
Definition: time.cpp:228
bool increment_time_value(T &current, uint16_t begin, uint16_t end)
Definition: time.cpp:230
uint8_t second
seconds after the minute [0-60]
Definition: time.h:21
time_t timestamp
unix epoch time (seconds since UTC Midnight January 1, 1970)
Definition: time.h:39
uint8_t minute
minutes after the hour [0-59]
Definition: time.h:23
bool operator==(ESPTime other)
Definition: time.cpp:226
bool is_valid() const
Check if this ESPTime is valid (all fields in range and year is greater than 2018) ...
Definition: time.h:61
uint8_t day_of_week
day of the week; sunday=1 [1-7]
Definition: time.h:27
uint16_t year
year
Definition: time.h:35
bool is_leap_year(uint32_t year)
Definition: time.cpp:8
void recalc_timestamp_local(bool use_day_of_year=true)
Recalculate the timestamp field from the other fields of this ESPTime instance assuming local fields...
Definition: time.cpp:191
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
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:162
uint8_t month
month; january=1 [1-12]
Definition: time.h:33
uint8_t end[39]
Definition: sun_gtil2.cpp:31
uint8_t hour
hours since midnight [0-23]
Definition: time.h:25
bool operator<(ESPTime other)
Definition: time.cpp:224
uint8_t day_of_month
day of the month [1-31]
Definition: time.h:29
uint8_t month
Definition: date_entity.h:123
bool operator>=(ESPTime other)
Definition: time.cpp:227
bool fields_in_range() const
Check if all time fields of this ESPTime are in range.
Definition: time.h:64
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:67