set-tz.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* Set tz value
  2. * by: John Stultz <[email protected]>
  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. #include "../kselftest.h"
  25. int set_tz(int min, int dst)
  26. {
  27. struct timezone tz;
  28. tz.tz_minuteswest = min;
  29. tz.tz_dsttime = dst;
  30. return settimeofday(0, &tz);
  31. }
  32. int get_tz_min(void)
  33. {
  34. struct timezone tz;
  35. struct timeval tv;
  36. memset(&tz, 0, sizeof(tz));
  37. gettimeofday(&tv, &tz);
  38. return tz.tz_minuteswest;
  39. }
  40. int get_tz_dst(void)
  41. {
  42. struct timezone tz;
  43. struct timeval tv;
  44. memset(&tz, 0, sizeof(tz));
  45. gettimeofday(&tv, &tz);
  46. return tz.tz_dsttime;
  47. }
  48. int main(int argc, char **argv)
  49. {
  50. int i, ret;
  51. int min, dst;
  52. min = get_tz_min();
  53. dst = get_tz_dst();
  54. printf("tz_minuteswest started at %i, dst at %i\n", min, dst);
  55. printf("Checking tz_minuteswest can be properly set: ");
  56. fflush(stdout);
  57. for (i = -15*60; i < 15*60; i += 30) {
  58. ret = set_tz(i, dst);
  59. ret = get_tz_min();
  60. if (ret != i) {
  61. printf("[FAILED] expected: %i got %i\n", i, ret);
  62. goto err;
  63. }
  64. }
  65. printf("[OK]\n");
  66. printf("Checking invalid tz_minuteswest values are caught: ");
  67. fflush(stdout);
  68. if (!set_tz(-15*60-1, dst)) {
  69. printf("[FAILED] %i didn't return failure!\n", -15*60-1);
  70. goto err;
  71. }
  72. if (!set_tz(15*60+1, dst)) {
  73. printf("[FAILED] %i didn't return failure!\n", 15*60+1);
  74. goto err;
  75. }
  76. if (!set_tz(-24*60, dst)) {
  77. printf("[FAILED] %i didn't return failure!\n", -24*60);
  78. goto err;
  79. }
  80. if (!set_tz(24*60, dst)) {
  81. printf("[FAILED] %i didn't return failure!\n", 24*60);
  82. goto err;
  83. }
  84. printf("[OK]\n");
  85. set_tz(min, dst);
  86. return ksft_exit_pass();
  87. err:
  88. set_tz(min, dst);
  89. return ksft_exit_fail();
  90. }