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