#include <err.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#include <string.h>
#include <sys/sysmacros.h>
typedef struct st_test {
const char *st_tz;
long st_time;
const char *st_exp;
} st_test_t;
const st_test_t st_tests[] = { {
.st_tz = "America/Vancouver",
.st_time = 1798790400,
.st_exp = "MST"
}, {
.st_tz = "America/Vancouver",
.st_time = 1777569072,
.st_exp = "PDT"
}, {
.st_tz = "Europe/Kyiv",
.st_time = 500000000,
.st_exp = "MSK"
}, {
.st_tz = "Europe/Kyiv",
.st_time = 700000000,
.st_exp = "EET"
} };
static bool
st_test_one(const st_test_t *test)
{
struct tm *tm;
char buf[32];
(void) setenv("TZ", test->st_tz, 1);
tm = localtime(&test->st_time);
if (tm == NULL) {
warn("TEST FAILED: %s (%ld): failed to convert to struct tm",
test->st_tz, test->st_time);
return (false);
}
if (strftime(buf, sizeof (buf), "%Z", tm) == 0) {
warnx("TEST FAILED: %s (%ld): strftime wrote no data",
test->st_tz, test->st_time);
return (false);
}
if (strcmp(buf, test->st_exp) != 0) {
warnx("TEST FAILED: %s (%ld): tz mismatch: found %s, expected "
"%s", test->st_tz, test->st_time, buf, test->st_exp);
return (false);
}
return (true);
}
int
main(void)
{
int ret = EXIT_SUCCESS;
for (size_t i = 0; i < ARRAY_SIZE(st_tests); i++) {
if (!st_test_one(&st_tests[i]))
ret = EXIT_FAILURE;
}
if (ret == EXIT_SUCCESS) {
(void) printf("All tests passed successfully!\n");
}
return (ret);
}