Back to index

glibc  2.9
tst-strptime.c
Go to the documentation of this file.
00001 /* Test for strptime.
00002    Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <locale.h>
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <string.h>
00025 #include <time.h>
00026 
00027 
00028 static const struct
00029 {
00030   const char *locale;
00031   const char *input;
00032   const char *format;
00033   int wday;
00034   int yday;
00035   int mon;
00036   int mday;
00037 } day_tests[] =
00038 {
00039   { "C", "2000-01-01", "%Y-%m-%d", 6, 0, 0, 1 },
00040   { "C", "03/03/00", "%D", 5, 62, 2, 3 },
00041   { "C", "9/9/99", "%x", 4, 251, 8, 9 },
00042   { "C", "19990502123412", "%Y%m%d%H%M%S", 0, 121, 4, 2 },
00043   { "C", "2001 20 Mon", "%Y %U %a", 1, 140, 4, 21 },
00044   { "C", "2001 21 Mon", "%Y %W %a", 1, 140, 4, 21 },
00045   { "ja_JP.EUC-JP", "2000-01-01 08:12:21 AM", "%Y-%m-%d %I:%M:%S %p",
00046     6, 0, 0, 1 },
00047   { "en_US.ISO-8859-1", "2000-01-01 08:12:21 PM", "%Y-%m-%d %I:%M:%S %p",
00048     6, 0, 0, 1 },
00049   { "ja_JP.EUC-JP", "2001 20 \xb7\xee", "%Y %U %a", 1, 140, 4, 21 },
00050   { "ja_JP.EUC-JP", "2001 21 \xb7\xee", "%Y %W %a", 1, 140, 4, 21 },
00051 };
00052 
00053 
00054 static const struct
00055 {
00056   const char *input;
00057   const char *format;
00058   const char *output;
00059   int wday;
00060   int yday;
00061 } tm_tests [] =
00062 {
00063   {"17410105012000", "%H%M%S%d%m%Y", "2000-01-05 17:41:01", 3, 4}
00064 };
00065 
00066 
00067 
00068 static int
00069 test_tm (void)
00070 {
00071   struct tm tm;
00072   size_t i;
00073   int result = 0;
00074   char buf[100];
00075 
00076   for (i = 0; i < sizeof (tm_tests) / sizeof (tm_tests[0]); ++i)
00077     {
00078       memset (&tm, '\0', sizeof (tm));
00079 
00080       char *ret = strptime (tm_tests[i].input, tm_tests[i].format, &tm);
00081       if (ret == NULL)
00082        {
00083          printf ("strptime returned NULL for `%s'\n", tm_tests[i].input);
00084          result = 1;
00085          continue;
00086        }
00087       else if (*ret != '\0')
00088        {
00089          printf ("not all of `%s' read\n", tm_tests[i].input);
00090          result = 1;
00091        }
00092       strftime (buf, sizeof (buf), "%F %T", &tm);
00093       printf ("strptime (\"%s\", \"%s\", ...)\n"
00094              "\tshould be: %s, wday = %d, yday = %3d\n"
00095              "\t       is: %s, wday = %d, yday = %3d\n",
00096              tm_tests[i].input, tm_tests[i].format,
00097              tm_tests[i].output,
00098              tm_tests[i].wday, tm_tests[i].yday,
00099              buf, tm.tm_wday, tm.tm_yday);
00100 
00101       if (strcmp (buf, tm_tests[i].output) != 0)
00102        {
00103          printf ("Time and date are not correct.\n");
00104          result = 1;
00105        }
00106       if (tm.tm_wday != tm_tests[i].wday)
00107        {
00108          printf ("weekday for `%s' incorrect: %d instead of %d\n",
00109                 tm_tests[i].input, tm.tm_wday, tm_tests[i].wday);
00110          result = 1;
00111        }
00112       if (tm.tm_yday != tm_tests[i].yday)
00113        {
00114          printf ("yearday for `%s' incorrect: %d instead of %d\n",
00115                 tm_tests[i].input, tm.tm_yday, tm_tests[i].yday);
00116          result = 1;
00117        }
00118     }
00119 
00120   return result;
00121 }
00122 
00123 
00124 int
00125 main (int argc, char *argv[])
00126 {
00127   struct tm tm;
00128   size_t i;
00129   int result = 0;
00130 
00131   for (i = 0; i < sizeof (day_tests) / sizeof (day_tests[0]); ++i)
00132     {
00133       memset (&tm, '\0', sizeof (tm));
00134 
00135       if (setlocale (LC_ALL, day_tests[i].locale) == NULL)
00136        {
00137          printf ("cannot set locale %s: %m\n", day_tests[i].locale);
00138          exit (EXIT_FAILURE);
00139        }
00140 
00141       char *ret = strptime (day_tests[i].input, day_tests[i].format, &tm);
00142       if (ret == NULL)
00143        {
00144          printf ("strptime returned NULL for `%s'\n", day_tests[i].input);
00145          result = 1;
00146          continue;
00147        }
00148       else if (*ret != '\0')
00149        {
00150          printf ("not all of `%s' read\n", day_tests[i].input);
00151          result = 1;
00152        }
00153 
00154       printf ("strptime (\"%s\", \"%s\", ...)\n"
00155              "\tshould be: wday = %d, yday = %3d, mon = %2d, mday = %2d\n"
00156              "\t       is: wday = %d, yday = %3d, mon = %2d, mday = %2d\n",
00157              day_tests[i].input, day_tests[i].format,
00158              day_tests[i].wday, day_tests[i].yday,
00159              day_tests[i].mon, day_tests[i].mday,
00160              tm.tm_wday, tm.tm_yday, tm.tm_mon, tm.tm_mday);
00161 
00162       if (tm.tm_wday != day_tests[i].wday)
00163        {
00164          printf ("weekday for `%s' incorrect: %d instead of %d\n",
00165                 day_tests[i].input, tm.tm_wday, day_tests[i].wday);
00166          result = 1;
00167        }
00168       if (tm.tm_yday != day_tests[i].yday)
00169        {
00170          printf ("yearday for `%s' incorrect: %d instead of %d\n",
00171                 day_tests[i].input, tm.tm_yday, day_tests[i].yday);
00172          result = 1;
00173        }
00174       if (tm.tm_mon != day_tests[i].mon)
00175        {
00176          printf ("month for `%s' incorrect: %d instead of %d\n",
00177                 day_tests[i].input, tm.tm_mon, day_tests[i].mon);
00178          result = 1;
00179        }
00180       if (tm.tm_mday != day_tests[i].mday)
00181        {
00182          printf ("monthday for `%s' incorrect: %d instead of %d\n",
00183                 day_tests[i].input, tm.tm_mday, day_tests[i].mday);
00184          result = 1;
00185        }
00186     }
00187 
00188   setlocale (LC_ALL, "C");
00189 
00190   result |= test_tm ();
00191 
00192   return result;
00193 }