Back to index

glibc  2.9
tst-getdate.c
Go to the documentation of this file.
00001 /* Test for getdate.
00002    Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Andreas Jaeger <aj@suse.de>, 2000.
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 <stdio.h>
00022 #include <stdlib.h>
00023 #include <string.h>
00024 #include <time.h>
00025 
00026 static const struct
00027 {
00028   const char *str;
00029   const char *tz;
00030   int err;
00031   struct tm tm;
00032 } tests [] =
00033 {
00034   {"21:01:10 1999-1-31", "Universal", 0, {10, 1, 21, 31, 0, 99, 0, 0, 0}},
00035   {"21:01:10 1999-2-28", "Universal", 0, {10, 1, 21, 28, 1, 99, 0, 0, 0}},
00036   {"16:30:46 2000-2-29", "Universal", 0, {46, 30,16, 29, 1, 100, 0, 0, 0}},
00037   {"01-08-2000 05:06:07", "Europe/Berlin", 0, {7, 6, 5, 1, 7, 100, 0, 0, 0}}
00038 };
00039 
00040 static void
00041 report_date_error (int err)
00042 {
00043   switch(err)
00044     {
00045     case 1:
00046       printf ("The environment variable DATEMSK is not defined or null.\n");
00047       break;
00048     case 2:
00049       printf ("The template file denoted by the DATEMSK environment variable cannot be opened.\n");
00050       break;
00051     case 3:
00052       printf ("Information about the template file cannot retrieved.\n");
00053       break;
00054     case 4:
00055       printf ("The template file is not a regular file.\n");
00056       break;
00057     case 5:
00058       printf ("An I/O error occurred while reading the template file.\n");
00059       break;
00060     case 6:
00061       printf ("Not enough memory available to execute the function.\n");
00062       break;
00063     case 7:
00064       printf ("The template file contains no matching template.\n");
00065       break;
00066     case 8:
00067       printf ("The input date is invalid, but would match a template otherwise.\n");
00068       break;
00069     default:
00070       printf("Unknown error code.\n");
00071       break;
00072     }
00073 }
00074 
00075 
00076 int
00077 main (void)
00078 {
00079   int errors = 0;
00080   size_t i;
00081   struct tm *tm;
00082 
00083 
00084   for (i = 0; i < sizeof (tests) / sizeof (tests[0]); ++i)
00085     {
00086       setenv ("TZ", tests[i].tz, 1);
00087 
00088       tm = getdate (tests[i].str);
00089 
00090       if (getdate_err != tests[i].err)
00091        {
00092          printf ("Failure for getdate (\"%s\"):\n", tests[i].str);
00093          printf ("getdate_err should be %d but returned: %d which means:\n",
00094                 tests[i].err, getdate_err);
00095          report_date_error (getdate_err);
00096          ++errors;
00097        }
00098       else if (tests[i].tm.tm_mon != tm->tm_mon
00099               || tests[i].tm.tm_year != tm->tm_year
00100               || tests[i].tm.tm_mday != tm->tm_mday
00101               || tests[i].tm.tm_hour != tm->tm_hour
00102               || tests[i].tm.tm_min != tm->tm_min
00103               || tests[i].tm.tm_sec != tm->tm_sec)
00104        {
00105          printf ("Failure for getdate (\"%s\"):\n", tests[i].str);
00106          printf ("struct tm is:  %d-%d-%d %d:%d:%d\n",
00107                 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
00108                 tm->tm_hour, tm->tm_min, tm->tm_sec);
00109          printf ("but should be: %d-%d-%d %d:%d:%d\n",
00110                 tests[i].tm.tm_year + 1900, tests[i].tm.tm_mon + 1,
00111                 tests[i].tm.tm_mday,
00112                 tests[i].tm.tm_hour, tests[i].tm.tm_min, tests[i].tm.tm_sec);
00113          ++errors;
00114        }
00115     }
00116 
00117   if (!errors)
00118     printf ("No errors found.\n");
00119   return errors != 0;
00120 }