--- /dev/null
+/* Test that valid POSIX timezone strings are correctly parsed by tzset(3). */\r
+#include <stdio.h>\r
+#include <stdlib.h>\r
+\r
+// BEGIN test vectors\r
+#include <time.h>\r
+#include <limits.h>\r
+\r
+#define IN_SECONDS(h, m, s) ((h) * 3600 + (m) * 60 + (s))\r
+#define NO_TIME INT_MIN\r
+\r
+struct tz_test {\r
+ const char* tzstr;\r
+ int offset_seconds;\r
+ int dst_offset_seconds;\r
+};\r
+\r
+extern struct tm winter_tm;\r
+extern struct tm summer_tm;\r
+extern const time_t winter_time;\r
+extern const time_t summer_time;\r
+extern struct tz_test test_timezones[];\r
+\r
+// winter time is March, 21st 2022 at 8:15pm and 20 seconds\r
+struct tm winter_tm = {\r
+ .tm_sec = 20,\r
+ .tm_min = 15,\r
+ .tm_hour = 20,\r
+ .tm_mday = 21,\r
+ .tm_mon = 3 - 1,\r
+ .tm_year = 2022 - 1900,\r
+ .tm_isdst = 0\r
+};\r
+\r
+// summer time is July, 15th 2022 at 10:50am and 40 seconds\r
+struct tm summer_tm = {\r
+ .tm_sec = 40,\r
+ .tm_min = 50,\r
+ .tm_hour = 10,\r
+ .tm_mday = 15,\r
+ .tm_mon = 7 - 1,\r
+ .tm_year = 2022 - 1900,\r
+ .tm_isdst = 1\r
+};\r
+\r
+// UTC unix time for the winter time\r
+const time_t winter_time = 1647893720;\r
+const time_t summer_time = 1657882240;\r
+\r
+struct tz_test test_timezones[] = {\r
+ /*\r
+ * creating test vectors based on the POSIX spec (https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html#tag_08_03)\r
+ */\r
+ // normal std names\r
+ {"MAR1", IN_SECONDS(1, 0, 0), NO_TIME},\r
+ {"MAR-1", -IN_SECONDS(1, 0, 0), NO_TIME},\r
+ {"MAR+2", IN_SECONDS(2, 0, 0), NO_TIME},\r
+ {"MAR7", IN_SECONDS(7, 0, 0), NO_TIME},\r
+ {"MAR-7", -IN_SECONDS(7, 0, 0), NO_TIME},\r
+ {"MARS5", IN_SECONDS(5, 0, 0), NO_TIME},\r
+ {"MARSM5", IN_SECONDS(5, 0, 0), NO_TIME},\r
+ {"MARSMOON5", IN_SECONDS(5, 0, 0), NO_TIME}, // assuming TZNAME_MAX >= 8\r
+ {"MARS5:23:42", IN_SECONDS(5, 23, 42), NO_TIME},\r
+ {"SUN-7:14:24", -IN_SECONDS(7, 14, 24), NO_TIME},\r
+ // with DST\r
+ {"MAR5SMAR", IN_SECONDS(5, 0, 0), IN_SECONDS(4, 0, 0)}, // only DST name\r
+ {"MAR5SMAR2", IN_SECONDS(5, 0, 0), IN_SECONDS(2, 0, 0)}, // DST name with offset\r
+ {"MAR3SMAR-3", IN_SECONDS(3, 0, 0), -IN_SECONDS(3, 0, 0)},\r
+ {"MARSWINTER4MARSUMMER", IN_SECONDS(4, 0, 0), IN_SECONDS(3, 0, 0)},\r
+ {"MARSWINTER4MARSUMMER3", IN_SECONDS(4, 0, 0), IN_SECONDS(3, 0, 0)},\r
+ // with DST IN_SECONDSs\r
+ {"WMARS3SMARS,J80", IN_SECONDS(3, 0, 0), IN_SECONDS(2, 0, 0)},\r
+ {"WMARS3SMARS,J80,J134", IN_SECONDS(3, 0, 0), IN_SECONDS(2, 0, 0)},\r
+ {"WMARS3SMARS,79", IN_SECONDS(3, 0, 0), IN_SECONDS(2, 0, 0)},\r
+ {"WMARS3SMARS,76,134", IN_SECONDS(3, 0, 0), IN_SECONDS(2, 0, 0)},\r
+ {"WMARS3SMARS,76/02,134/03", IN_SECONDS(3, 0, 0), IN_SECONDS(2, 0, 0)},\r
+ {"WMARS3SMARS,76/02:15:45,134/03:40:20", IN_SECONDS(3, 0, 0), IN_SECONDS(2, 0, 0)},\r
+ {"WMARS3SMARS,M3.4.1/02:15:45,M8.3.1/03:40:20", IN_SECONDS(3, 0, 0), IN_SECONDS(2, 0, 0)},\r
+\r
+ // special std names\r
+ {"<UNK>-1", -IN_SECONDS(1, 0, 0), NO_TIME},\r
+ {"<UNKNOWN>-2", -IN_SECONDS(2, 0, 0), NO_TIME}, // require TZNAME_MAX >= 7 + 1\r
+ {"<003>3", IN_SECONDS(3, 0, 0), NO_TIME},\r
+ {"<+04>4", IN_SECONDS(4, 0, 0), NO_TIME},\r
+ {"<-05>-5", -IN_SECONDS(5, 0, 0), NO_TIME},\r
+ {"<A-5>6", IN_SECONDS(6, 0, 0), NO_TIME},\r
+ {"<+A5>-7", -IN_SECONDS(7, 0, 0), NO_TIME},\r
+ {"<0123456>8", IN_SECONDS(8, 0, 0), NO_TIME},\r
+ {"<0A1B2C3>9", IN_SECONDS(9, 0, 0), NO_TIME},\r
+ {"<RD-04>-4<RD+005>5", -IN_SECONDS(4, 0, 0), IN_SECONDS(5, 0, 0)},\r
+ {"<WINT+03>3<SUM+02>", IN_SECONDS(3, 0, 0), IN_SECONDS(2, 0, 0)},\r
+ {"<WINT+03>3<SUM+02>2", IN_SECONDS(3, 0, 0), IN_SECONDS(2, 0, 0)},\r
+ {"<WINT+03>3:15<SUM+02>2:30:15", IN_SECONDS(3, 15, 0), IN_SECONDS(2, 30, 15)},\r
+ {"<H3M15>3:15<H2M30S15>2:30:15", IN_SECONDS(3, 15, 0), IN_SECONDS(2, 30, 15)}, // requires TZNAME_MAX >= 8 + 1\r
+ {"<+H6M20S12>6:20:12<-H4M40S14>-4:40:14", IN_SECONDS(6, 20, 12), -IN_SECONDS(4, 40, 14)}, // requires TZNAME_MAX >= 9 + 1\r
+ {"<+0123456789ABCDEF>3:33:33", IN_SECONDS(3, 33, 33), NO_TIME}, // truncates the name (17 + 1)\r
+\r
+ /* \r
+ * real-world test vectors.\r
+ * IN_SECONDSzones extracted from the tzdb (https://github.com/eggert/tz#2019e).\r
+ * The IN_SECONDSzone strings can also be obtained from https://raw.githubusercontent.com/nayarsystems/posix_tz_db/master/zones.csv.\r
+ */\r
+ { /* Etc/GMT-14 */ "<+14>-14", -IN_SECONDS(14, 0, 0), NO_TIME},\r
+ { /* Etc/GMT+12 */ "<-12>12", IN_SECONDS(12, 0, 0), NO_TIME},\r
+ { /* Africa/Casablanca */ "<+01>-1", -IN_SECONDS(1, 0, 0), NO_TIME},\r
+ { /* America/Araguaina */ "<-03>3", IN_SECONDS(3, 0, 0), NO_TIME},\r
+ { /* America/Asuncion */ "<-04>4<-03>,M10.1.0/0,M3.4.0/0", IN_SECONDS(4, 0, 0), IN_SECONDS(3, 0, 0)},\r
+ { /* America/Los_Angeles */ "PST8PDT,M3.2.0,M11.1.0", IN_SECONDS(8, 0, 0), IN_SECONDS(7, 0, 0)},\r
+ { /* America/New_York */ "EST5EDT,M3.2.0,M11.1.0", IN_SECONDS(5, 0, 0), IN_SECONDS(4, 0, 0)},\r
+ { /* America/Scoresbysund */ "<-01>1<+00>,M3.5.0/0,M10.5.0/1", IN_SECONDS(1, 0, 0), IN_SECONDS(0, 0, 0)},\r
+ { /* Asia/Colombo */ "<+0530>-5:30", -IN_SECONDS(5, 30, 0), NO_TIME},\r
+ { /* Europe/Berlin */ "CET-1CEST,M3.5.0,M10.5.0/3", -IN_SECONDS(1, 0, 0), -IN_SECONDS(2, 0, 0)},\r
+\r
+ // END of list\r
+ {NULL, NO_TIME, NO_TIME}\r
+};\r
+\r
+// helper macros\r
+#define FOR_TIMEZONES(iter_name) for (struct tz_test* iter_name = test_timezones; iter_name->tzstr != NULL; ++iter_name)\r
+\r
+// END test vectors\r
+\r
+static int failed = 0;\r
+\r
+#define TEST_ASSERT_EQUAL_INT_MESSAGE(...) assert_equal(__VA_ARGS__)\r
+void assert_equal(int lhs, int rhs, const char* msg)\r
+{\r
+ if (lhs != rhs)\r
+ {\r
+ printf("Assertion failed! Expected %d to equal %d. %s\n", lhs, rhs, msg);\r
+ ++failed;\r
+ }\r
+}\r
+\r
+void test_TimezoneStrings(void)\r
+{\r
+ char buffer[128];\r
+\r
+ FOR_TIMEZONES(ptr)\r
+ {\r
+ setenv("TZ", ptr->tzstr, 1);\r
+ tzset();\r
+\r
+ snprintf(buffer, 128, "winter time, timezone = \"%s\"", ptr->tzstr);\r
+\r
+ struct tm winter_tm_copy = winter_tm; // copy\r
+ TEST_ASSERT_EQUAL_INT_MESSAGE(winter_time + ptr->offset_seconds, mktime(&winter_tm_copy), buffer);\r
+\r
+ if (ptr->dst_offset_seconds != NO_TIME)\r
+ {\r
+ snprintf(buffer, 128, "summer time, timezone = \"%s\"", ptr->tzstr);\r
+\r
+ struct tm summer_tm_copy = summer_tm; // copy\r
+ TEST_ASSERT_EQUAL_INT_MESSAGE(summer_time + ptr->dst_offset_seconds, mktime(&summer_tm_copy), buffer);\r
+ }\r
+ }\r
+}\r
+\r
+int main()\r
+{\r
+ test_TimezoneStrings();\r
+ exit (failed);\r
+}
\ No newline at end of file