| 1 | /* |
| 2 | * tz.h - IANA timezone resolution for plugin_time. |
| 3 | * |
| 4 | * The connector cannot read the host's tz database: a Windows node has none, |
| 5 | * macOS's is case-insensitive, and a slim container may have a different tzdata |
| 6 | * version, all of which would make the same tool call answer differently per |
| 7 | * node. So the table is embedded (src/tzdata.h, generated by |
| 8 | * tools/gen_tzdata.py) and every platform resolves identically: |
| 9 | * |
| 10 | * tz_find() exact, case-sensitive IANA key -> zone record (or NULL) |
| 11 | * tz_offset() UTC instant -> offset east of UTC, DST flag, abbreviation |
| 12 | * tz_resolve_wall() a wall-clock local time -> the UTC instants it can mean |
| 13 | * (two of them on a DST fall-back, none in a spring gap) |
| 14 | * tz_local_name() the node's own timezone name, best effort |
| 15 | * |
| 16 | * Times are int64 seconds since the Unix epoch; nothing here reads the system |
| 17 | * clock, so every function is deterministic and testable. |
| 18 | * |
| 19 | * MIT License. Copyright (c) 2026 SCORPIOX INC. |
| 20 | */ |
| 21 | #ifndef SX_PLUGIN_TIME_TZ_H |
| 22 | #define SX_PLUGIN_TIME_TZ_H |
| 23 | |
| 24 | #include <stddef.h> |
| 25 | #include <stdint.h> |
| 26 | |
| 27 | /* A wall-clock resolution can legitimately have two answers (a repeated hour |
| 28 | * at a DST fall-back) or zero (an hour skipped at spring-forward). */ |
| 29 | #define TZ_MAX_CANDIDATES 4 |
| 30 | |
| 31 | typedef struct { |
| 32 | int64_t utc; |
| 33 | int offset; /* east of UTC, seconds */ |
| 34 | int is_dst; |
| 35 | } tz_cand_t; |
| 36 | |
| 37 | /* The zone record, opaque outside tz.c. */ |
| 38 | typedef struct tz_zone tz_zone; |
| 39 | |
| 40 | /* The zone's IANA key (valid while the table is loaded). */ |
| 41 | const char *tz_zone_name(const tz_zone *z); |
| 42 | |
| 43 | /* Exact IANA key lookup. Case sensitive on every platform on purpose: see the |
| 44 | * header comment in tools/gen_tzdata.py. Returns NULL for an unknown key. */ |
| 45 | const tz_zone *tz_find(const char *name); |
| 46 | |
| 47 | /* Windows timezone id ("New Zealand Standard Time") -> IANA key, via the |
| 48 | * CLDR map embedded in the table. Returns NULL when the id is unknown. */ |
| 49 | const tz_zone *tz_find_windows(const char *win_id); |
| 50 | |
| 51 | /* Number of embedded zones and their keys (sorted), for the picker UI. */ |
| 52 | int tz_count(void); |
| 53 | const char *tz_name_at(int index); |
| 54 | |
| 55 | /* Offset east of UTC in force at `utc`. `abbr` (may be NULL) receives the |
| 56 | * zone abbreviation, truncated to `abbrsz` including the terminator. */ |
| 57 | int tz_offset(const tz_zone *z, int64_t utc, int *is_dst, |
| 58 | char *abbr, size_t abbrsz); |
| 59 | |
| 60 | /* Every UTC instant that has the given wall-clock time in `z`, earliest |
| 61 | * first. `wall` is the local time read as if it were UTC (i.e. it already |
| 62 | * includes the date and the hh:mm:ss). Returns the count: |
| 63 | * 0 = the wall time does not exist (skipped by a forward clock change) |
| 64 | * 1 = unambiguous |
| 65 | * 2 = the hour repeated (a backward clock change); [0] is the first |
| 66 | * occurrence, [1] the second - which is what Python's fold=0/1 gives. |
| 67 | */ |
| 68 | int tz_resolve_wall(const tz_zone *z, int64_t wall, tz_cand_t *out, int outmax); |
| 69 | |
| 70 | /* The node's own IANA zone name, best effort: $TZ, then the platform's own |
| 71 | * configuration, then an offset match against the embedded table. Never |
| 72 | * returns NULL; falls back to "UTC". The result is always a name tz_find() |
| 73 | * accepts. */ |
| 74 | const char *tz_local_name(void); |
| 75 | |
| 76 | /* Civil-date helpers (proleptic Gregorian, no leap seconds). */ |
| 77 | int64_t tz_days_from_civil(int y, int m, int d); |
| 78 | void tz_civil_from_days(int64_t days, int *y, int *m, int *d); |
| 79 | int tz_weekday(int64_t days); /* 0 = Sunday */ |
| 80 | |
| 81 | #endif /* SX_PLUGIN_TIME_TZ_H */ |