ESPHome 2026.3.0-dev
Loading...
Searching...
No Matches
posix_tz.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef USE_TIME_TIMEZONE
4
5#include <cstdint>
6#include <ctime>
7
8namespace esphome::time {
9
11enum class DSTRuleType : uint8_t {
12 NONE = 0,
16};
17
19struct DSTRule {
20 int32_t time_seconds;
21 uint16_t day;
23 uint8_t month;
24 uint8_t week;
25 uint8_t day_of_week;
26};
27
34
36 bool has_dst() const { return this->dst_start.type != DSTRuleType::NONE; }
37};
38
59bool parse_posix_tz(const char *tz_string, ParsedTimezone &result);
60
67bool epoch_to_local_tm(time_t utc_epoch, const ParsedTimezone &tz, struct tm *out_tm);
68
72void set_global_tz(const ParsedTimezone &tz);
73
75const ParsedTimezone &get_global_tz();
76
81bool is_in_dst(time_t utc_epoch, const ParsedTimezone &tz);
82
83// Internal helper functions exposed for testing.
84// Remove before 2026.9.0: skip_tz_name, parse_offset, parse_dst_rule are only
85// used by parse_posix_tz() which is bridge code for backward compatibility.
86// The remaining helpers (epoch_to_tm_utc, day_of_week, days_in_month, etc.)
87// are used by the conversion functions and will stay.
88
89namespace internal {
90
94bool skip_tz_name(const char *&p);
95
99int32_t parse_offset(const char *&p);
100
105bool parse_dst_rule(const char *&p, DSTRule &rule);
106
111void julian_to_month_day(int julian_day, int &month, int &day);
112
118void day_of_year_to_month_day(int day_of_year, int year, int &month, int &day);
119
122int day_of_week(int year, int month, int day);
123
125int days_in_month(int year, int month);
126
128bool is_leap_year(int year);
129
131void epoch_to_tm_utc(time_t epoch, struct tm *out_tm);
132
139
140} // namespace internal
141
142} // namespace esphome::time
143
144#endif // USE_TIME_TIMEZONE
time_t const DSTRule int32_t base_offset_seconds
Definition posix_tz.cpp:293
int day_of_week(int year, int month, int day)
Calculate day of week for any date (0 = Sunday) Uses a simplified algorithm that works for years 1970...
int days_in_month(int year, int month)
Get the number of days in a month.
Definition posix_tz.cpp:59
int32_t parse_offset(const char *&p)
Parse an offset in format [-]hh[:mm[:ss]].
void struct tm * out_tm
Definition posix_tz.cpp:87
time_t const DSTRule & rule
Definition posix_tz.cpp:293
bool parse_dst_rule(const char *&p, DSTRule &rule)
Parse a DST rule in format Mm.w.d[/time], Jn[/time], or n[/time].
Definition posix_tz.cpp:220
bool skip_tz_name(const char *&p)
Skip a timezone name (letters or <...> quoted format)
Definition posix_tz.cpp:124
void julian_to_month_day(int julian_day, int &month, int &day)
Convert Julian day (J format, 1-365 not counting Feb 29) to month/day.
void day_of_year_to_month_day(int day_of_year, int year, int &month, int &day)
Convert day of year (plain format, 0-365 counting Feb 29) to month/day.
bool is_leap_year(int year)
Check if a year is a leap year.
Definition posix_tz.cpp:31
time_t calculate_dst_transition(int year, const DSTRule &rule, int32_t base_offset_seconds)
Calculate the epoch timestamp for a DST transition in a given year.
void epoch_to_tm_utc(time_t epoch, struct tm *out_tm)
Convert epoch to year/month/day/hour/min/sec (UTC)
void set_global_tz(const ParsedTimezone &tz)
Set the global timezone used by epoch_to_local_tm() when called without a timezone.
Definition posix_tz.cpp:14
bool is_in_dst(time_t utc_epoch, const ParsedTimezone &tz)
Check if a given UTC epoch falls within DST for the parsed timezone.
const ParsedTimezone & get_global_tz()
Get the global timezone.
Definition posix_tz.cpp:16
bool parse_posix_tz(const char *tz_string, ParsedTimezone &result)
Parse a POSIX TZ string into a ParsedTimezone struct.
Definition posix_tz.cpp:374
DSTRuleType
Type of DST transition rule.
Definition posix_tz.h:11
@ JULIAN_NO_LEAP
J format: Jn (day 1-365, Feb 29 not counted)
@ NONE
No DST rule (used to indicate no DST)
@ DAY_OF_YEAR
Plain number: n (day 0-365, Feb 29 counted in leap years)
@ MONTH_WEEK_DAY
M format: Mm.w.d (e.g., M3.2.0 = 2nd Sunday of March)
bool const ParsedTimezone & tz
Definition posix_tz.cpp:346
bool epoch_to_local_tm(time_t utc_epoch, const ParsedTimezone &tz, struct tm *out_tm)
Convert a UTC epoch to local time using the parsed timezone.
Definition posix_tz.cpp:445
Rule for DST transition (packed for 32-bit: 12 bytes)
Definition posix_tz.h:19
uint16_t day
Day of year (for JULIAN_NO_LEAP and DAY_OF_YEAR)
Definition posix_tz.h:21
DSTRuleType type
Type of rule.
Definition posix_tz.h:22
uint8_t week
Week 1-5, 5 = last (for MONTH_WEEK_DAY)
Definition posix_tz.h:24
int32_t time_seconds
Seconds after midnight (default 7200 = 2:00 AM)
Definition posix_tz.h:20
uint8_t day_of_week
Day 0-6, 0 = Sunday (for MONTH_WEEK_DAY)
Definition posix_tz.h:25
uint8_t month
Month 1-12 (for MONTH_WEEK_DAY)
Definition posix_tz.h:23
Parsed POSIX timezone information (packed for 32-bit: 32 bytes)
Definition posix_tz.h:29
bool has_dst() const
Check if this timezone has DST rules.
Definition posix_tz.h:36
DSTRule dst_end
When DST ends.
Definition posix_tz.h:33
DSTRule dst_start
When DST starts.
Definition posix_tz.h:32
int32_t dst_offset_seconds
DST offset from UTC in seconds.
Definition posix_tz.h:31
int32_t std_offset_seconds
Standard time offset from UTC in seconds (positive = west)
Definition posix_tz.h:30