tst-strptime.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  17. #include <locale.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <time.h>
  22. static const struct
  23. {
  24. const char *locale;
  25. const char *input;
  26. const char *format;
  27. int wday;
  28. int yday;
  29. int mon;
  30. int mday;
  31. } day_tests[] =
  32. {
  33. { "C", "2000-01-01", "%Y-%m-%d", 6, 0, 0, 1 },
  34. { "C", "03/03/00", "%D", 5, 62, 2, 3 },
  35. { "C", "9/9/99", "%x", 4, 251, 8, 9 },
  36. { "C", "19990502123412", "%Y%m%d%H%M%S", 0, 121, 4, 2 },
  37. { "C", "2001 20 Mon", "%Y %U %a", 1, 140, 4, 21 },
  38. { "C", "2001 21 Mon", "%Y %W %a", 1, 140, 4, 21 },
  39. { "ja_JP.EUC-JP", "2000-01-01 08:12:21 AM", "%Y-%m-%d %I:%M:%S %p",
  40. 6, 0, 0, 1 },
  41. { "en_US.ISO-8859-1", "2000-01-01 08:12:21 PM", "%Y-%m-%d %I:%M:%S %p",
  42. 6, 0, 0, 1 },
  43. { "ja_JP.EUC-JP", "2001 20 \xb7\xee", "%Y %U %a", 1, 140, 4, 21 },
  44. { "ja_JP.EUC-JP", "2001 21 \xb7\xee", "%Y %W %a", 1, 140, 4, 21 },
  45. };
  46. static const struct
  47. {
  48. const char *input;
  49. const char *format;
  50. const char *output;
  51. int wday;
  52. int yday;
  53. } tm_tests [] =
  54. {
  55. {"17410105012000", "%H%M%S%d%m%Y", "2000-01-05 17:41:01", 3, 4}
  56. };
  57. static int
  58. test_tm (void)
  59. {
  60. struct tm tm;
  61. size_t i;
  62. int result = 0;
  63. char buf[100];
  64. for (i = 0; i < sizeof (tm_tests) / sizeof (tm_tests[0]); ++i)
  65. {
  66. memset (&tm, '\0', sizeof (tm));
  67. char *ret = strptime (tm_tests[i].input, tm_tests[i].format, &tm);
  68. if (ret == NULL)
  69. {
  70. printf ("strptime returned NULL for `%s'\n", tm_tests[i].input);
  71. result = 1;
  72. continue;
  73. }
  74. else if (*ret != '\0')
  75. {
  76. printf ("not all of `%s' read\n", tm_tests[i].input);
  77. result = 1;
  78. }
  79. strftime (buf, sizeof (buf), "%F %T", &tm);
  80. printf ("strptime (\"%s\", \"%s\", ...)\n"
  81. "\tshould be: %s, wday = %d, yday = %3d\n"
  82. "\t is: %s, wday = %d, yday = %3d\n",
  83. tm_tests[i].input, tm_tests[i].format,
  84. tm_tests[i].output,
  85. tm_tests[i].wday, tm_tests[i].yday,
  86. buf, tm.tm_wday, tm.tm_yday);
  87. if (strcmp (buf, tm_tests[i].output) != 0)
  88. {
  89. printf ("Time and date are not correct.\n");
  90. result = 1;
  91. }
  92. if (tm.tm_wday != tm_tests[i].wday)
  93. {
  94. printf ("weekday for `%s' incorrect: %d instead of %d\n",
  95. tm_tests[i].input, tm.tm_wday, tm_tests[i].wday);
  96. result = 1;
  97. }
  98. if (tm.tm_yday != tm_tests[i].yday)
  99. {
  100. printf ("yearday for `%s' incorrect: %d instead of %d\n",
  101. tm_tests[i].input, tm.tm_yday, tm_tests[i].yday);
  102. result = 1;
  103. }
  104. }
  105. return result;
  106. }
  107. int
  108. main (int argc, char *argv[])
  109. {
  110. struct tm tm;
  111. size_t i;
  112. int result = 0;
  113. for (i = 0; i < sizeof (day_tests) / sizeof (day_tests[0]); ++i)
  114. {
  115. memset (&tm, '\0', sizeof (tm));
  116. if (setlocale (LC_ALL, day_tests[i].locale) == NULL)
  117. {
  118. printf ("cannot set locale %s: %m\n", day_tests[i].locale);
  119. exit (EXIT_FAILURE);
  120. }
  121. char *ret = strptime (day_tests[i].input, day_tests[i].format, &tm);
  122. if (ret == NULL)
  123. {
  124. printf ("strptime returned NULL for `%s'\n", day_tests[i].input);
  125. result = 1;
  126. continue;
  127. }
  128. else if (*ret != '\0')
  129. {
  130. printf ("not all of `%s' read\n", day_tests[i].input);
  131. result = 1;
  132. }
  133. printf ("strptime (\"%s\", \"%s\", ...)\n"
  134. "\tshould be: wday = %d, yday = %3d, mon = %2d, mday = %2d\n"
  135. "\t is: wday = %d, yday = %3d, mon = %2d, mday = %2d\n",
  136. day_tests[i].input, day_tests[i].format,
  137. day_tests[i].wday, day_tests[i].yday,
  138. day_tests[i].mon, day_tests[i].mday,
  139. tm.tm_wday, tm.tm_yday, tm.tm_mon, tm.tm_mday);
  140. if (tm.tm_wday != day_tests[i].wday)
  141. {
  142. printf ("weekday for `%s' incorrect: %d instead of %d\n",
  143. day_tests[i].input, tm.tm_wday, day_tests[i].wday);
  144. result = 1;
  145. }
  146. if (tm.tm_yday != day_tests[i].yday)
  147. {
  148. printf ("yearday for `%s' incorrect: %d instead of %d\n",
  149. day_tests[i].input, tm.tm_yday, day_tests[i].yday);
  150. result = 1;
  151. }
  152. if (tm.tm_mon != day_tests[i].mon)
  153. {
  154. printf ("month for `%s' incorrect: %d instead of %d\n",
  155. day_tests[i].input, tm.tm_mon, day_tests[i].mon);
  156. result = 1;
  157. }
  158. if (tm.tm_mday != day_tests[i].mday)
  159. {
  160. printf ("monthday for `%s' incorrect: %d instead of %d\n",
  161. day_tests[i].input, tm.tm_mday, day_tests[i].mday);
  162. result = 1;
  163. }
  164. }
  165. setlocale (LC_ALL, "C");
  166. result |= test_tm ();
  167. return result;
  168. }