test_time.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* Copyright (C) 1991, 1992, 1994, 1997 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <http://www.gnu.org/licenses/>. */
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <time.h>
  17. int
  18. main (int argc, char **argv)
  19. {
  20. time_t t;
  21. register struct tm *tp;
  22. struct tm tbuf;
  23. int lose = 0;
  24. --argc;
  25. ++argv;
  26. do
  27. {
  28. char buf[BUFSIZ];
  29. if (argc > 0)
  30. {
  31. static char tzenvbuf[BUFSIZ];
  32. sprintf(tzenvbuf, "TZ=%s", *argv);
  33. if (putenv(tzenvbuf))
  34. {
  35. puts("putenv failed.");
  36. lose = 1;
  37. }
  38. else
  39. puts (tzenvbuf);
  40. }
  41. tzset();
  42. tbuf.tm_year = 72;
  43. tbuf.tm_mon = 0;
  44. tbuf.tm_mday = 31;
  45. tbuf.tm_hour = 6;
  46. tbuf.tm_min = 14;
  47. tbuf.tm_sec = 50;
  48. tbuf.tm_isdst = -1;
  49. doit:;
  50. t = mktime(&tbuf);
  51. if (t == (time_t) -1)
  52. {
  53. puts("mktime() failed?");
  54. lose = 1;
  55. }
  56. tp = localtime(&t);
  57. if (tp == NULL)
  58. {
  59. puts("localtime() failed.");
  60. lose = 1;
  61. }
  62. else if (strftime(buf, sizeof(buf), "%a %b %d %X %Z %Y", tp) == 0)
  63. {
  64. puts("strftime() failed.");
  65. lose = 1;
  66. }
  67. else
  68. puts(buf);
  69. if (tbuf.tm_year == 101)
  70. {
  71. tbuf.tm_year = 97;
  72. tbuf.tm_mon = 0;
  73. goto doit;
  74. }
  75. ++argv;
  76. } while (--argc > 0);
  77. {
  78. #define SIZE 256
  79. char buffer[SIZE];
  80. time_t curtime;
  81. struct tm *loctime;
  82. curtime = time (NULL);
  83. loctime = localtime (&curtime);
  84. fputs (asctime (loctime), stdout);
  85. strftime (buffer, SIZE, "Today is %A, %B %d.\n", loctime);
  86. fputs (buffer, stdout);
  87. strftime (buffer, SIZE, "The time is %I:%M %p.\n", loctime);
  88. fputs (buffer, stdout);
  89. loctime->tm_year = 72;
  90. loctime->tm_mon = 8;
  91. loctime->tm_mday = 12;
  92. loctime->tm_hour = 20;
  93. loctime->tm_min = 49;
  94. loctime->tm_sec = 05;
  95. curtime = mktime (loctime);
  96. strftime (buffer, SIZE, "%D %T was %w the %jth.\n", loctime);
  97. fputs (buffer, stdout);
  98. }
  99. return (lose ? EXIT_FAILURE : EXIT_SUCCESS);
  100. }