commit 9a238a9

default  ·  2025-04-13 07:23:43 +0000 UTC
parent 8d86cab
Added some preliminary support for time zones (for scheduled posts).
2 files changed,  +78, -3
M html.c
M html.c
+7, -3
 1@@ -4359,19 +4359,23 @@ int html_post_handler(const xs_dict *req, const char *q_path,
 2             }
 3 
 4             if (xs_is_string(post_date) && *post_date) {
 5-                xs *local_pubdate = xs_fmt("%sT%s", post_date,
 6+                xs *post_pubdate = xs_fmt("%sT%s", post_date,
 7                     xs_is_string(post_time) && *post_time ? post_time : "00:00:00");
 8 
 9-                time_t t = xs_parse_iso_date(local_pubdate, 1);
10+                time_t t = xs_parse_iso_date(post_pubdate, 0);
11 
12                 if (t != 0) {
13+                    const char *tz = xs_dict_get_def(snac.config, "tz", "UTC");
14+
15+                    t -= xs_tz_offset(tz);
16+
17                     xs *iso_date = xs_str_iso_date(t);
18                     msg = xs_dict_set(msg, "published", iso_date);
19 
20                     snac_debug(&snac, 1, xs_fmt("Published date: [%s]", iso_date));
21                 }
22                 else
23-                    snac_log(&snac, xs_fmt("Invalid post date: [%s]", local_pubdate));
24+                    snac_log(&snac, xs_fmt("Invalid post date: [%s]", post_pubdate));
25             }
26 
27             /* is the published date from the future? */
+71, -0
 1@@ -15,6 +15,8 @@ time_t xs_parse_time(const char *str, const char *fmt, int local);
 2 #define xs_parse_localtime(str, fmt) xs_parse_time(str, fmt, 1)
 3 #define xs_parse_utctime(str, fmt) xs_parse_time(str, fmt, 0)
 4 xs_str *xs_str_time_diff(time_t time_diff);
 5+xs_list *xs_tz_list(void);
 6+int xs_tz_offset(const char *tz);
 7 
 8 #ifdef XS_IMPLEMENTATION
 9 
10@@ -106,6 +108,75 @@ time_t xs_parse_iso_date(const char *iso_date, int local)
11 }
12 
13 
14+/** timezones **/
15+
16+/* intentionally dead simple */
17+
18+struct {
19+    const char *tz; /* timezone name */
20+    float h_offset; /* hour offset */
21+} xs_tz[] = {
22+    { "UTC",      0 },
23+    { "GMT",      0 },
24+    { "GMT+1",    -1 },
25+    { "GMT+2",    -2 },
26+    { "GMT+3",    -3 },
27+    { "GMT+4",    -4 },
28+    { "GMT+5",    -5 },
29+    { "GMT+6",    -6 },
30+    { "GMT+7",    -7 },
31+    { "GMT+8",    -8 },
32+    { "GMT+9",    -9 },
33+    { "GMT+10",   -10 },
34+    { "GMT+11",   -11 },
35+    { "GMT+12",   -12 },
36+    { "GMT-1",    1 },
37+    { "GMT-2",    2 },
38+    { "GMT-3",    3 },
39+    { "GMT-4",    4 },
40+    { "GMT-5",    5 },
41+    { "GMT-6",    6 },
42+    { "GMT-7",    7 },
43+    { "GMT-8",    8 },
44+    { "GMT-9",    9 },
45+    { "GMT-10",   10 },
46+    { "GMT-11",   11 },
47+    { "GMT-12",   12 },
48+    { "GMT-13",   13 },
49+    { "GMT-14",   14 },
50+    { "GMT-15",   15 },
51+    { "CET",      -1 },
52+    { "CST",      -6 },
53+    { "MST",      -7 },
54+    { "PST",      -8 },
55+    { NULL,       0 }
56+};
57+
58+
59+xs_list *xs_tz_list(void)
60+/* returns the list of supported timezones */
61+{
62+    xs_list *l = xs_list_new();
63+
64+    for (int n = 0; xs_tz[n].tz != NULL; n++)
65+        l = xs_list_append(l, xs_tz[n].tz);
66+
67+    return l;
68+}
69+
70+
71+int xs_tz_offset(const char *tz)
72+/* returns the offset in seconds from the specified Time Zone to UTC */
73+{
74+    for (int n = 0; xs_tz[n].tz != NULL; n++) {
75+        if (strcmp(xs_tz[n].tz, tz) == 0)
76+            return xs_tz[n].h_offset * 3600;
77+    }
78+
79+    return 0;
80+}
81+
82+
83 #endif /* XS_IMPLEMENTATION */
84 
85 #endif /* _XS_TIME_H */