set-tz.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <time.h>
  19. #include <sys/time.h>
  20. #include <sys/timex.h>
  21. #include <string.h>
  22. #include <signal.h>
  23. #include <unistd.h>
  24. #ifdef KTEST
  25. #include "../kselftest.h"
  26. #else
  27. static inline int ksft_exit_pass(void)
  28. {
  29. exit(0);
  30. }
  31. static inline int ksft_exit_fail(void)
  32. {
  33. exit(1);
  34. }
  35. #endif
  36. int set_tz(int min, int dst)
  37. {
  38. struct timezone tz;
  39. tz.tz_minuteswest = min;
  40. tz.tz_dsttime = dst;
  41. return settimeofday(0, &tz);
  42. }
  43. int get_tz_min(void)
  44. {
  45. struct timezone tz;
  46. struct timeval tv;
  47. memset(&tz, 0, sizeof(tz));
  48. gettimeofday(&tv, &tz);
  49. return tz.tz_minuteswest;
  50. }
  51. int get_tz_dst(void)
  52. {
  53. struct timezone tz;
  54. struct timeval tv;
  55. memset(&tz, 0, sizeof(tz));
  56. gettimeofday(&tv, &tz);
  57. return tz.tz_dsttime;
  58. }
  59. int main(int argc, char **argv)
  60. {
  61. int i, ret;
  62. int min, dst;
  63. min = get_tz_min();
  64. dst = get_tz_dst();
  65. printf("tz_minuteswest started at %i, dst at %i\n", min, dst);
  66. printf("Checking tz_minuteswest can be properly set: ");
  67. for (i = -15*60; i < 15*60; i += 30) {
  68. ret = set_tz(i, dst);
  69. ret = get_tz_min();
  70. if (ret != i) {
  71. printf("[FAILED] expected: %i got %i\n", i, ret);
  72. goto err;
  73. }
  74. }
  75. printf("[OK]\n");
  76. printf("Checking invalid tz_minuteswest values are caught: ");
  77. if (!set_tz(-15*60-1, dst)) {
  78. printf("[FAILED] %i didn't return failure!\n", -15*60-1);
  79. goto err;
  80. }
  81. if (!set_tz(15*60+1, dst)) {
  82. printf("[FAILED] %i didn't return failure!\n", 15*60+1);
  83. goto err;
  84. }
  85. if (!set_tz(-24*60, dst)) {
  86. printf("[FAILED] %i didn't return failure!\n", -24*60);
  87. goto err;
  88. }
  89. if (!set_tz(24*60, dst)) {
  90. printf("[FAILED] %i didn't return failure!\n", 24*60);
  91. goto err;
  92. }
  93. printf("[OK]\n");
  94. set_tz(min, dst);
  95. return ksft_exit_pass();
  96. err:
  97. set_tz(min, dst);
  98. return ksft_exit_fail();
  99. }