ESPHome  2024.2.1
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 
13 struct ESPTime {
17  uint8_t second;
19  uint8_t minute;
21  uint8_t hour;
23  uint8_t day_of_week;
25  uint8_t day_of_month;
27  uint16_t day_of_year;
29  uint8_t month;
31  uint16_t year;
33  bool is_dst;
35  time_t timestamp;
36 
42  size_t strftime(char *buffer, size_t buffer_len, const char *format);
43 
54  std::string strftime(const std::string &format);
55 
57  bool is_valid() const { return this->year >= 2019 && this->fields_in_range(); }
58 
60  bool fields_in_range() const {
61  return this->second < 61 && this->minute < 60 && this->hour < 24 && this->day_of_week > 0 &&
62  this->day_of_week < 8 && this->day_of_month > 0 && this->day_of_month < 32 && this->day_of_year > 0 &&
63  this->day_of_year < 367 && this->month > 0 && this->month < 13;
64  }
65 
67  static ESPTime from_c_tm(struct tm *c_tm, time_t c_time);
68 
74  static ESPTime from_epoch_local(time_t epoch) {
75  struct tm *c_tm = ::localtime(&epoch);
76  return ESPTime::from_c_tm(c_tm, epoch);
77  }
83  static ESPTime from_epoch_utc(time_t epoch) {
84  struct tm *c_tm = ::gmtime(&epoch);
85  return ESPTime::from_c_tm(c_tm, epoch);
86  }
87 
89  void recalc_timestamp_utc(bool use_day_of_year = true);
90 
92  struct tm to_c_tm();
93 
94  static int32_t timezone_offset();
95 
97  void increment_second();
99  void increment_day();
100  bool operator<(ESPTime other);
101  bool operator<=(ESPTime other);
102  bool operator==(ESPTime other);
103  bool operator>=(ESPTime other);
104  bool operator>(ESPTime other);
105 };
106 } // namespace esphome
static ESPTime from_epoch_utc(time_t epoch)
Convert an UTC epoch timestamp to a UTC time ESPTime instance.
Definition: time.h:83
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
static int32_t timezone_offset()
Definition: time.cpp:137
A more user-friendly version of struct tm from time.h.
Definition: time.h:13
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:19
bool is_dst
daylight saving time flag
Definition: time.h:33
void increment_day()
Increment this clock instance by one day.
Definition: time.cpp:91
uint16_t day_of_year
day of the year [1-366]
Definition: time.h:27
bool operator<=(ESPTime other)
Definition: time.cpp:162
void increment_second()
Increment this clock instance by one second.
Definition: time.cpp:64
static ESPTime from_epoch_local(time_t epoch)
Convert an UTC epoch timestamp to a local time ESPTime instance.
Definition: time.h:74
bool operator>(ESPTime other)
Definition: time.cpp:165
bool increment_time_value(T &current, uint16_t begin, uint16_t end)
Definition: time.cpp:167
uint8_t second
seconds after the minute [0-60]
Definition: time.h:17
time_t timestamp
unix epoch time (seconds since UTC Midnight January 1, 1970)
Definition: time.h:35
uint8_t minute
minutes after the hour [0-59]
Definition: time.h:19
bool operator==(ESPTime other)
Definition: time.cpp:163
bool is_valid() const
Check if this ESPTime is valid (all fields in range and year is greater than 2018) ...
Definition: time.h:57
uint8_t day_of_week
day of the week; sunday=1 [1-7]
Definition: time.h:23
uint16_t year
year
Definition: time.h:31
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
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:108
uint8_t month
month; january=1 [1-12]
Definition: time.h:29
uint8_t hour
hours since midnight [0-23]
Definition: time.h:21
bool operator<(ESPTime other)
Definition: time.cpp:161
uint8_t day_of_month
day of the month [1-31]
Definition: time.h:25
bool operator>=(ESPTime other)
Definition: time.cpp:164
bool fields_in_range() const
Check if all time fields of this ESPTime are in range.
Definition: time.h:60