^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* Set tz value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * by: John Stultz <john.stultz@linaro.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * (C) Copyright Linaro 2016
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Licensed under the GPLv2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * This program is free software: you can redistribute it and/or modify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * it under the terms of the GNU General Public License as published by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * the Free Software Foundation, either version 2 of the License, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * (at your option) any later version.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * This program is distributed in the hope that it will be useful,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * but WITHOUT ANY WARRANTY; without even the implied warranty of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * GNU General Public License for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <sys/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <sys/timex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include "../kselftest.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) int set_tz(int min, int dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct timezone tz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) tz.tz_minuteswest = min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) tz.tz_dsttime = dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) return settimeofday(0, &tz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) int get_tz_min(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct timezone tz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct timeval tv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) memset(&tz, 0, sizeof(tz));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) gettimeofday(&tv, &tz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) return tz.tz_minuteswest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) int get_tz_dst(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct timezone tz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct timeval tv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) memset(&tz, 0, sizeof(tz));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) gettimeofday(&tv, &tz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) return tz.tz_dsttime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) int main(int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) int min, dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) min = get_tz_min();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) dst = get_tz_dst();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) printf("tz_minuteswest started at %i, dst at %i\n", min, dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) printf("Checking tz_minuteswest can be properly set: ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) fflush(stdout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) for (i = -15*60; i < 15*60; i += 30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) ret = set_tz(i, dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) ret = get_tz_min();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if (ret != i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) printf("[FAILED] expected: %i got %i\n", i, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) printf("[OK]\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) printf("Checking invalid tz_minuteswest values are caught: ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) fflush(stdout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if (!set_tz(-15*60-1, dst)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) printf("[FAILED] %i didn't return failure!\n", -15*60-1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) if (!set_tz(15*60+1, dst)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) printf("[FAILED] %i didn't return failure!\n", 15*60+1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (!set_tz(-24*60, dst)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) printf("[FAILED] %i didn't return failure!\n", -24*60);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if (!set_tz(24*60, dst)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) printf("[FAILED] %i didn't return failure!\n", 24*60);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) printf("[OK]\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) set_tz(min, dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) return ksft_exit_pass();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) set_tz(min, dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) return ksft_exit_fail();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }