Back to home page

OSCL-LXR

 
 

    


0001 /* Set tz value
0002  *              by: John Stultz <john.stultz@linaro.org>
0003  *              (C) Copyright Linaro 2016
0004  *              Licensed under the GPLv2
0005  *
0006  *   This program is free software: you can redistribute it and/or modify
0007  *   it under the terms of the GNU General Public License as published by
0008  *   the Free Software Foundation, either version 2 of the License, or
0009  *   (at your option) any later version.
0010  *
0011  *   This program is distributed in the hope that it will be useful,
0012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014  *   GNU General Public License for more details.
0015  */
0016 
0017 
0018 #include <stdio.h>
0019 #include <stdlib.h>
0020 #include <time.h>
0021 #include <sys/time.h>
0022 #include <sys/timex.h>
0023 #include <string.h>
0024 #include <signal.h>
0025 #include <unistd.h>
0026 #include "../kselftest.h"
0027 
0028 int set_tz(int min, int dst)
0029 {
0030     struct timezone tz;
0031 
0032     tz.tz_minuteswest = min;
0033     tz.tz_dsttime = dst;
0034 
0035     return settimeofday(0, &tz);
0036 }
0037 
0038 int get_tz_min(void)
0039 {
0040     struct timezone tz;
0041     struct timeval tv;
0042 
0043     memset(&tz, 0, sizeof(tz));
0044     gettimeofday(&tv, &tz);
0045     return tz.tz_minuteswest;
0046 }
0047 
0048 int get_tz_dst(void)
0049 {
0050     struct timezone tz;
0051     struct timeval tv;
0052 
0053     memset(&tz, 0, sizeof(tz));
0054     gettimeofday(&tv, &tz);
0055     return tz.tz_dsttime;
0056 }
0057 
0058 int main(int argc, char **argv)
0059 {
0060     int i, ret;
0061     int min, dst;
0062 
0063     min = get_tz_min();
0064     dst = get_tz_dst();
0065     printf("tz_minuteswest started at %i, dst at %i\n", min, dst);
0066 
0067     printf("Checking tz_minuteswest can be properly set: ");
0068     fflush(stdout);
0069     for (i = -15*60; i < 15*60; i += 30) {
0070         ret = set_tz(i, dst);
0071         ret = get_tz_min();
0072         if (ret != i) {
0073             printf("[FAILED] expected: %i got %i\n", i, ret);
0074             goto err;
0075         }
0076     }
0077     printf("[OK]\n");
0078 
0079     printf("Checking invalid tz_minuteswest values are caught: ");
0080     fflush(stdout);
0081 
0082     if (!set_tz(-15*60-1, dst)) {
0083         printf("[FAILED] %i didn't return failure!\n", -15*60-1);
0084         goto err;
0085     }
0086 
0087     if (!set_tz(15*60+1, dst)) {
0088         printf("[FAILED] %i didn't return failure!\n", 15*60+1);
0089         goto err;
0090     }
0091 
0092     if (!set_tz(-24*60, dst)) {
0093         printf("[FAILED] %i didn't return failure!\n", -24*60);
0094         goto err;
0095     }
0096 
0097     if (!set_tz(24*60, dst)) {
0098         printf("[FAILED] %i didn't return failure!\n", 24*60);
0099         goto err;
0100     }
0101 
0102     printf("[OK]\n");
0103 
0104     set_tz(min, dst);
0105     return ksft_exit_pass();
0106 
0107 err:
0108     set_tz(min, dst);
0109     return ksft_exit_fail();
0110 }