ESPHome  2024.12.2
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 uint8_t days_in_month(uint8_t month, uint16_t year);
13 
15 struct 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 
56  std::string strftime(const std::string &format);
57 
59  bool is_valid() const { return this->year >= 2019 && this->fields_in_range(); }
60 
62  bool fields_in_range() const {
63  return this->second < 61 && this->minute < 60 && this->hour < 24 && this->day_of_week > 0 &&
64  this->day_of_week < 8 && this->day_of_month > 0 && this->day_of_month < 32 && this->day_of_year > 0 &&
65  this->day_of_year < 367 && this->month > 0 && this->month < 13;
66  }
67 
73  static bool strptime(const std::string &time_to_parse, ESPTime &esp_time);
74 
76  static ESPTime from_c_tm(struct tm *c_tm, time_t c_time);
77 
83  static ESPTime from_epoch_local(time_t epoch) {
84  struct tm *c_tm = ::localtime(&epoch);
85  return ESPTime::from_c_tm(c_tm, epoch);
86  }
92  static ESPTime from_epoch_utc(time_t epoch) {
93  struct tm *c_tm = ::gmtime(&epoch);
94  return ESPTime::from_c_tm(c_tm, epoch);
95  }
96 
98  void recalc_timestamp_utc(bool use_day_of_year = true);
99 
101  void recalc_timestamp_local();
102 
104  struct tm to_c_tm();
105 
106  static int32_t timezone_offset();
107 
109  void increment_second();
111  void increment_day();
112  bool operator<(ESPTime other);
113  bool operator<=(ESPTime other);
114  bool operator==(ESPTime other);
115  bool operator>=(ESPTime other);
116  bool operator>(ESPTime other);
117 };
118 } // 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:92
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
void recalc_timestamp_local()
Recalculate the timestamp field from the other fields of this ESPTime instance assuming local fields...
Definition: time.cpp:191
static int32_t timezone_offset()
Definition: time.cpp:204
A more user-friendly version of struct tm from time.h.
Definition: time.h:15
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_dst
daylight saving time flag
Definition: time.h:35
void increment_day()
Increment this clock instance by one day.
Definition: time.cpp:146
uint16_t day_of_year
day of the year [1-366]
Definition: time.h:29
bool operator<=(ESPTime other)
Definition: time.cpp:229
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:8
static ESPTime from_epoch_local(time_t epoch)
Convert an UTC epoch timestamp to a local time ESPTime instance.
Definition: time.h:83
bool operator>(ESPTime other)
Definition: time.cpp:232
bool increment_time_value(T &current, uint16_t begin, uint16_t end)
Definition: time.cpp:234
uint8_t second
seconds after the minute [0-60]
Definition: time.h:19
time_t timestamp
unix epoch time (seconds since UTC Midnight January 1, 1970)
Definition: time.h:37
uint8_t minute
minutes after the hour [0-59]
Definition: time.h:21
bool operator==(ESPTime other)
Definition: time.cpp:230
bool is_valid() const
Check if this ESPTime is valid (all fields in range and year is greater than 2018) ...
Definition: time.h:59
uint8_t day_of_week
day of the week; sunday=1 [1-7]
Definition: time.h:25
uint16_t year
year
Definition: time.h:33
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:164
uint8_t month
month; january=1 [1-12]
Definition: time.h:31
uint8_t end[39]
Definition: sun_gtil2.cpp:31
uint8_t hour
hours since midnight [0-23]
Definition: time.h:23
bool operator<(ESPTime other)
Definition: time.cpp:228
uint8_t day_of_month
day of the month [1-31]
Definition: time.h:27
uint8_t month
Definition: date_entity.h:123
bool operator>=(ESPTime other)
Definition: time.cpp:231
bool fields_in_range() const
Check if all time fields of this ESPTime are in range.
Definition: time.h:62
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