diff options
| author | 2025-04-13 09:23:43 +0200 | |
|---|---|---|
| committer | 2025-04-13 09:23:43 +0200 | |
| commit | 9a238a937e31b36e7ee35109c09764019ee3af1f (patch) | |
| tree | ffd5f14573655de7c1bfe5e4d77178fbccdb3a5b /xs_time.h | |
| parent | Updated TODO. (diff) | |
| download | snac2-9a238a937e31b36e7ee35109c09764019ee3af1f.tar.gz snac2-9a238a937e31b36e7ee35109c09764019ee3af1f.tar.xz snac2-9a238a937e31b36e7ee35109c09764019ee3af1f.zip | |
Added some preliminary support for time zones (for scheduled posts).
Diffstat (limited to 'xs_time.h')
| -rw-r--r-- | xs_time.h | 71 |
1 files changed, 71 insertions, 0 deletions
| @@ -15,6 +15,8 @@ time_t xs_parse_time(const char *str, const char *fmt, int local); | |||
| 15 | #define xs_parse_localtime(str, fmt) xs_parse_time(str, fmt, 1) | 15 | #define xs_parse_localtime(str, fmt) xs_parse_time(str, fmt, 1) |
| 16 | #define xs_parse_utctime(str, fmt) xs_parse_time(str, fmt, 0) | 16 | #define xs_parse_utctime(str, fmt) xs_parse_time(str, fmt, 0) |
| 17 | xs_str *xs_str_time_diff(time_t time_diff); | 17 | xs_str *xs_str_time_diff(time_t time_diff); |
| 18 | xs_list *xs_tz_list(void); | ||
| 19 | int xs_tz_offset(const char *tz); | ||
| 18 | 20 | ||
| 19 | #ifdef XS_IMPLEMENTATION | 21 | #ifdef XS_IMPLEMENTATION |
| 20 | 22 | ||
| @@ -106,6 +108,75 @@ time_t xs_parse_iso_date(const char *iso_date, int local) | |||
| 106 | } | 108 | } |
| 107 | 109 | ||
| 108 | 110 | ||
| 111 | /** timezones **/ | ||
| 112 | |||
| 113 | /* intentionally dead simple */ | ||
| 114 | |||
| 115 | struct { | ||
| 116 | const char *tz; /* timezone name */ | ||
| 117 | float h_offset; /* hour offset */ | ||
| 118 | } xs_tz[] = { | ||
| 119 | { "UTC", 0 }, | ||
| 120 | { "GMT", 0 }, | ||
| 121 | { "GMT+1", -1 }, | ||
| 122 | { "GMT+2", -2 }, | ||
| 123 | { "GMT+3", -3 }, | ||
| 124 | { "GMT+4", -4 }, | ||
| 125 | { "GMT+5", -5 }, | ||
| 126 | { "GMT+6", -6 }, | ||
| 127 | { "GMT+7", -7 }, | ||
| 128 | { "GMT+8", -8 }, | ||
| 129 | { "GMT+9", -9 }, | ||
| 130 | { "GMT+10", -10 }, | ||
| 131 | { "GMT+11", -11 }, | ||
| 132 | { "GMT+12", -12 }, | ||
| 133 | { "GMT-1", 1 }, | ||
| 134 | { "GMT-2", 2 }, | ||
| 135 | { "GMT-3", 3 }, | ||
| 136 | { "GMT-4", 4 }, | ||
| 137 | { "GMT-5", 5 }, | ||
| 138 | { "GMT-6", 6 }, | ||
| 139 | { "GMT-7", 7 }, | ||
| 140 | { "GMT-8", 8 }, | ||
| 141 | { "GMT-9", 9 }, | ||
| 142 | { "GMT-10", 10 }, | ||
| 143 | { "GMT-11", 11 }, | ||
| 144 | { "GMT-12", 12 }, | ||
| 145 | { "GMT-13", 13 }, | ||
| 146 | { "GMT-14", 14 }, | ||
| 147 | { "GMT-15", 15 }, | ||
| 148 | { "CET", -1 }, | ||
| 149 | { "CST", -6 }, | ||
| 150 | { "MST", -7 }, | ||
| 151 | { "PST", -8 }, | ||
| 152 | { NULL, 0 } | ||
| 153 | }; | ||
| 154 | |||
| 155 | |||
| 156 | xs_list *xs_tz_list(void) | ||
| 157 | /* returns the list of supported timezones */ | ||
| 158 | { | ||
| 159 | xs_list *l = xs_list_new(); | ||
| 160 | |||
| 161 | for (int n = 0; xs_tz[n].tz != NULL; n++) | ||
| 162 | l = xs_list_append(l, xs_tz[n].tz); | ||
| 163 | |||
| 164 | return l; | ||
| 165 | } | ||
| 166 | |||
| 167 | |||
| 168 | int xs_tz_offset(const char *tz) | ||
| 169 | /* returns the offset in seconds from the specified Time Zone to UTC */ | ||
| 170 | { | ||
| 171 | for (int n = 0; xs_tz[n].tz != NULL; n++) { | ||
| 172 | if (strcmp(xs_tz[n].tz, tz) == 0) | ||
| 173 | return xs_tz[n].h_offset * 3600; | ||
| 174 | } | ||
| 175 | |||
| 176 | return 0; | ||
| 177 | } | ||
| 178 | |||
| 179 | |||
| 109 | #endif /* XS_IMPLEMENTATION */ | 180 | #endif /* XS_IMPLEMENTATION */ |
| 110 | 181 | ||
| 111 | #endif /* _XS_TIME_H */ | 182 | #endif /* _XS_TIME_H */ |