test_time.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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, write to the Free
  13. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  14. 02111-1307 USA. */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <time.h>
  18. int
  19. main (int argc, char **argv)
  20. {
  21. time_t t;
  22. register struct tm *tp;
  23. struct tm tbuf;
  24. int lose = 0;
  25. --argc;
  26. ++argv;
  27. do
  28. {
  29. char buf[BUFSIZ];
  30. if (argc > 0)
  31. {
  32. static char tzenvbuf[BUFSIZ];
  33. sprintf(tzenvbuf, "TZ=%s", *argv);
  34. if (putenv(tzenvbuf))
  35. {
  36. puts("putenv failed.");
  37. lose = 1;
  38. }
  39. else
  40. puts (tzenvbuf);
  41. }
  42. tzset();
  43. tbuf.tm_year = 72;
  44. tbuf.tm_mon = 0;
  45. tbuf.tm_mday = 31;
  46. tbuf.tm_hour = 6;
  47. tbuf.tm_min = 14;
  48. tbuf.tm_sec = 50;
  49. tbuf.tm_isdst = -1;
  50. doit:;
  51. t = mktime(&tbuf);
  52. if (t == (time_t) -1)
  53. {
  54. puts("mktime() failed?");
  55. lose = 1;
  56. }
  57. tp = localtime(&t);
  58. if (tp == NULL)
  59. {
  60. puts("localtime() failed.");
  61. lose = 1;
  62. }
  63. else if (strftime(buf, sizeof(buf), "%a %b %d %X %Z %Y", tp) == 0)
  64. {
  65. puts("strftime() failed.");
  66. lose = 1;
  67. }
  68. else
  69. puts(buf);
  70. if (tbuf.tm_year == 101)
  71. {
  72. tbuf.tm_year = 97;
  73. tbuf.tm_mon = 0;
  74. goto doit;
  75. }
  76. ++argv;
  77. } while (--argc > 0);
  78. {
  79. #define SIZE 256
  80. char buffer[SIZE];
  81. time_t curtime;
  82. struct tm *loctime;
  83. curtime = time (NULL);
  84. loctime = localtime (&curtime);
  85. fputs (asctime (loctime), stdout);
  86. strftime (buffer, SIZE, "Today is %A, %B %d.\n", loctime);
  87. fputs (buffer, stdout);
  88. strftime (buffer, SIZE, "The time is %I:%M %p.\n", loctime);
  89. fputs (buffer, stdout);
  90. loctime->tm_year = 72;
  91. loctime->tm_mon = 8;
  92. loctime->tm_mday = 12;
  93. loctime->tm_hour = 20;
  94. loctime->tm_min = 49;
  95. loctime->tm_sec = 05;
  96. curtime = mktime (loctime);
  97. strftime (buffer, SIZE, "%D %T was %w the %jth.\n", loctime);
  98. fputs (buffer, stdout);
  99. }
  100. return (lose ? EXIT_FAILURE : EXIT_SUCCESS);
  101. }