ESPHome  2023.11.6
time.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cstdlib>
4 #include <ctime>
5 #include <string>
6 
7 namespace esphome {
8 
9 template<typename T> bool increment_time_value(T &current, uint16_t begin, uint16_t end);
10 
12 struct ESPTime {
16  uint8_t second;
18  uint8_t minute;
20  uint8_t hour;
22  uint8_t day_of_week;
24  uint8_t day_of_month;
26  uint16_t day_of_year;
28  uint8_t month;
30  uint16_t year;
32  bool is_dst;
34  time_t timestamp;
35 
41  size_t strftime(char *buffer, size_t buffer_len, const char *format);
42 
53  std::string strftime(const std::string &format);
54 
56  bool is_valid() const { return this->year >= 2019 && this->fields_in_range(); }
57 
59  bool fields_in_range() const {
60  return this->second < 61 && this->minute < 60 && this->hour < 24 && this->day_of_week > 0 &&
61  this->day_of_week < 8 && this->day_of_month > 0 && this->day_of_month < 32 && this->day_of_year > 0 &&
62  this->day_of_year < 367 && this->month > 0 && this->month < 13;
63  }
64 
66  static ESPTime from_c_tm(struct tm *c_tm, time_t c_time);
67 
73  static ESPTime from_epoch_local(time_t epoch) {
74  struct tm *c_tm = ::localtime(&epoch);
75  return ESPTime::from_c_tm(c_tm, epoch);
76  }
82  static ESPTime from_epoch_utc(time_t epoch) {
83  struct tm *c_tm = ::gmtime(&epoch);
84  return ESPTime::from_c_tm(c_tm, epoch);
85  }
86 
88  void recalc_timestamp_utc(bool use_day_of_year = true);
89 
91  struct tm to_c_tm();
92 
93  static int32_t timezone_offset();
94 
96  void increment_second();
98  void increment_day();
99  bool operator<(ESPTime other);
100  bool operator<=(ESPTime other);
101  bool operator==(ESPTime other);
102  bool operator>=(ESPTime other);
103  bool operator>(ESPTime other);
104 };
105 } // namespace esphome
static ESPTime from_epoch_utc(time_t epoch)
Convert an UTC epoch timestamp to a UTC time ESPTime instance.
Definition: time.h:82
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:12
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:32
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:26
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:73
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:16
time_t timestamp
unix epoch time (seconds since UTC Midnight January 1, 1970)
Definition: time.h:34
uint8_t minute
minutes after the hour [0-59]
Definition: time.h:18
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:56
uint8_t day_of_week
day of the week; sunday=1 [1-7]
Definition: time.h:22
uint16_t year
year
Definition: time.h:30
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:108
uint8_t month
month; january=1 [1-12]
Definition: time.h:28
uint8_t hour
hours since midnight [0-23]
Definition: time.h:20
bool operator<(ESPTime other)
Definition: time.cpp:161
uint8_t day_of_month
day of the month [1-31]
Definition: time.h:24
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:59