tst-strptime.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /* Test for strptime.
  2. Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <locale.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <time.h>
  21. static const struct
  22. {
  23. const char *locale;
  24. const char *input;
  25. const char *format;
  26. int wday;
  27. int yday;
  28. int mon;
  29. int mday;
  30. } day_tests[] =
  31. {
  32. { "C", "2000-01-01", "%Y-%m-%d", 6, 0, 0, 1 },
  33. { "C", "03/03/00", "%D", 5, 62, 2, 3 },
  34. { "C", "9/9/99", "%x", 4, 251, 8, 9 },
  35. { "C", "19990502123412", "%Y%m%d%H%M%S", 0, 121, 4, 2 },
  36. { "C", "2001 20 Mon", "%Y %U %a", 1, 140, 4, 21 },
  37. { "C", "2001 21 Mon", "%Y %W %a", 1, 140, 4, 21 },
  38. { "ja_JP.EUC-JP", "2000-01-01 08:12:21 AM", "%Y-%m-%d %I:%M:%S %p",
  39. 6, 0, 0, 1 },
  40. { "en_US.ISO-8859-1", "2000-01-01 08:12:21 PM", "%Y-%m-%d %I:%M:%S %p",
  41. 6, 0, 0, 1 },
  42. { "ja_JP.EUC-JP", "2001 20 \xb7\xee", "%Y %U %a", 1, 140, 4, 21 },
  43. { "ja_JP.EUC-JP", "2001 21 \xb7\xee", "%Y %W %a", 1, 140, 4, 21 },
  44. };
  45. static const struct
  46. {
  47. const char *input;
  48. const char *format;
  49. const char *output;
  50. int wday;
  51. int yday;
  52. } tm_tests [] =
  53. {
  54. {"17410105012000", "%H%M%S%d%m%Y", "2000-01-05 17:41:01", 3, 4}
  55. };
  56. static int
  57. test_tm (void)
  58. {
  59. struct tm tm;
  60. size_t i;
  61. int result = 0;
  62. char buf[100];
  63. for (i = 0; i < sizeof (tm_tests) / sizeof (tm_tests[0]); ++i)
  64. {
  65. memset (&tm, '\0', sizeof (tm));
  66. char *ret = strptime (tm_tests[i].input, tm_tests[i].format, &tm);
  67. if (ret == NULL)
  68. {
  69. printf ("strptime returned NULL for `%s'\n", tm_tests[i].input);
  70. result = 1;
  71. continue;
  72. }
  73. else if (*ret != '\0')
  74. {
  75. printf ("not all of `%s' read\n", tm_tests[i].input);
  76. result = 1;
  77. }
  78. strftime (buf, sizeof (buf), "%F %T", &tm);
  79. printf ("strptime (\"%s\", \"%s\", ...)\n"
  80. "\tshould be: %s, wday = %d, yday = %3d\n"
  81. "\t is: %s, wday = %d, yday = %3d\n",
  82. tm_tests[i].input, tm_tests[i].format,
  83. tm_tests[i].output,
  84. tm_tests[i].wday, tm_tests[i].yday,
  85. buf, tm.tm_wday, tm.tm_yday);
  86. if (strcmp (buf, tm_tests[i].output) != 0)
  87. {
  88. printf ("Time and date are not correct.\n");
  89. result = 1;
  90. }
  91. if (tm.tm_wday != tm_tests[i].wday)
  92. {
  93. printf ("weekday for `%s' incorrect: %d instead of %d\n",
  94. tm_tests[i].input, tm.tm_wday, tm_tests[i].wday);
  95. result = 1;
  96. }
  97. if (tm.tm_yday != tm_tests[i].yday)
  98. {
  99. printf ("yearday for `%s' incorrect: %d instead of %d\n",
  100. tm_tests[i].input, tm.tm_yday, tm_tests[i].yday);
  101. result = 1;
  102. }
  103. }
  104. return result;
  105. }
  106. int
  107. main (int argc, char *argv[])
  108. {
  109. struct tm tm;
  110. size_t i;
  111. int result = 0;
  112. for (i = 0; i < sizeof (day_tests) / sizeof (day_tests[0]); ++i)
  113. {
  114. memset (&tm, '\0', sizeof (tm));
  115. if (setlocale (LC_ALL, day_tests[i].locale) == NULL)
  116. {
  117. printf ("cannot set locale %s: %m\n", day_tests[i].locale);
  118. exit (EXIT_FAILURE);
  119. }
  120. char *ret = strptime (day_tests[i].input, day_tests[i].format, &tm);
  121. if (ret == NULL)
  122. {
  123. printf ("strptime returned NULL for `%s'\n", day_tests[i].input);
  124. result = 1;
  125. continue;
  126. }
  127. else if (*ret != '\0')
  128. {
  129. printf ("not all of `%s' read\n", day_tests[i].input);
  130. result = 1;
  131. }
  132. printf ("strptime (\"%s\", \"%s\", ...)\n"
  133. "\tshould be: wday = %d, yday = %3d, mon = %2d, mday = %2d\n"
  134. "\t is: wday = %d, yday = %3d, mon = %2d, mday = %2d\n",
  135. day_tests[i].input, day_tests[i].format,
  136. day_tests[i].wday, day_tests[i].yday,
  137. day_tests[i].mon, day_tests[i].mday,
  138. tm.tm_wday, tm.tm_yday, tm.tm_mon, tm.tm_mday);
  139. if (tm.tm_wday != day_tests[i].wday)
  140. {
  141. printf ("weekday for `%s' incorrect: %d instead of %d\n",
  142. day_tests[i].input, tm.tm_wday, day_tests[i].wday);
  143. result = 1;
  144. }
  145. if (tm.tm_yday != day_tests[i].yday)
  146. {
  147. printf ("yearday for `%s' incorrect: %d instead of %d\n",
  148. day_tests[i].input, tm.tm_yday, day_tests[i].yday);
  149. result = 1;
  150. }
  151. if (tm.tm_mon != day_tests[i].mon)
  152. {
  153. printf ("month for `%s' incorrect: %d instead of %d\n",
  154. day_tests[i].input, tm.tm_mon, day_tests[i].mon);
  155. result = 1;
  156. }
  157. if (tm.tm_mday != day_tests[i].mday)
  158. {
  159. printf ("monthday for `%s' incorrect: %d instead of %d\n",
  160. day_tests[i].input, tm.tm_mday, day_tests[i].mday);
  161. result = 1;
  162. }
  163. }
  164. setlocale (LC_ALL, "C");
  165. result |= test_tm ();
  166. return result;
  167. }