Back to index

glibc  2.9
bug-getdate1.c
Go to the documentation of this file.
00001 /* BZ #5451 */
00002 #include <time.h>
00003 #include <stdio.h>
00004 #include <stdlib.h>
00005 
00006 static char *templ_filename;
00007 
00008 // Writes template given as parameter to file,
00009 // specified as the argument
00010 static void
00011 output_to_template_file (const char *str)
00012 {
00013   FILE *fd = fopen (templ_filename, "w");
00014   if (fd == NULL)
00015     {
00016       printf ("Can not open file for writing\n");
00017       exit (1);
00018     }
00019 
00020   fprintf (fd, "%s\n", str);
00021   fclose (fd);
00022 }
00023 
00024 // Calls getdate() function with specified parameter,
00025 // specified as the argument, also checks the contents of
00026 // file with template and prints the result
00027 static int
00028 process_getdate_on (const char *str)
00029 {
00030   struct tm *res;
00031   char templ[1000];
00032   FILE *fd = fopen (templ_filename, "r");
00033 
00034   if (fd == NULL)
00035     {
00036       printf ("Can not open file for reading\n");
00037       exit (1);
00038     }
00039 
00040   if (fgets (templ, 1000, fd) == NULL)
00041     {
00042       printf ("Can not read file\n");
00043       exit (1);
00044     }
00045   fclose (fd);
00046 
00047   res = getdate (str);
00048   if (res == NULL)
00049     {
00050       printf ("Failed on getdate(\"%s\"), template is: %s", str, templ);
00051       printf ("Error number: %d\n\n", getdate_err);
00052       return 1;
00053     }
00054   printf ("Success on getdate(\"%s\"), template is: %s\n", str, templ);
00055   printf ("Result is\n");
00056   printf ("Seconds: %d\n", res->tm_sec);
00057   printf ("Minutes: %d\n", res->tm_min);
00058   printf ("Hour: %d\n", res->tm_hour);
00059   printf ("Day of month: %d\n", res->tm_mday);
00060   printf ("Month of year: %d\n", res->tm_mon);
00061   printf ("Years since 1900: %d\n", res->tm_year);
00062   printf ("Day of week: %d\n", res->tm_wday);
00063   printf ("Day of year: %d\n", res->tm_yday);
00064   printf ("Daylight Savings flag: %d\n\n", res->tm_isdst);
00065   return 0;
00066 }
00067 
00068 static int
00069 do_test (int argc, char *argv[])
00070 {
00071 
00072   templ_filename = argv[1];
00073 
00074   setenv ("DATEMSK", templ_filename, 1);
00075 
00076   /*
00077    * The following 4 testcases reproduce the problem:
00078    * 1. Templates "%S" and "%M" are not processed,
00079    *    when used without "%H" template
00080    */
00081   int res = 0;
00082   output_to_template_file ("%M");
00083   res |= process_getdate_on ("1");
00084 
00085   output_to_template_file ("%M %H");
00086   res |= process_getdate_on ("1 2");
00087 
00088   output_to_template_file ("%S");
00089   res |= process_getdate_on ("1");
00090 
00091   output_to_template_file ("%S %H");
00092   res |= process_getdate_on ("1 2");
00093 
00094   /*
00095    * The following 9 testcases reproduce the problem:
00096    * 2. Templates "%Y", "%y", "%d", "%C", "%C %y"
00097    *    are not processed separately
00098    */
00099   output_to_template_file ("%Y");
00100   process_getdate_on ("2001");
00101 
00102   output_to_template_file ("%Y %m");
00103   res |= process_getdate_on ("2001 3");
00104 
00105   output_to_template_file ("%y");
00106   res |= process_getdate_on ("70");
00107 
00108   output_to_template_file ("%y %m");
00109   res |= process_getdate_on ("70 3");
00110 
00111   output_to_template_file ("%d");
00112   res |= process_getdate_on ("06");
00113 
00114   output_to_template_file ("%d %m");
00115   res |= process_getdate_on ("25 3");
00116 
00117   output_to_template_file ("%C");
00118   res |= process_getdate_on ("20");
00119 
00120   output_to_template_file ("%C %y %m");
00121   res |= process_getdate_on ("20 3 2");
00122 
00123   output_to_template_file ("%C %y");
00124   res |= process_getdate_on ("20 5");
00125 
00126   /*
00127    * The following testcase reproduces the problem:
00128    * 3. When template is "%Y %m", day of month is not set
00129    *    to 1 as standard requires
00130    */
00131   output_to_template_file ("%Y %m");
00132   res |= process_getdate_on ("2008 3");
00133 
00134   return res;
00135 }
00136 
00137 #define PREPARE(argc, argv) \
00138   if (argc < 2)                                                       \
00139     {                                                          \
00140       puts ("Command line: progname template_filename_full_path");    \
00141       exit (1);                                                       \
00142     }                                                          \
00143   add_temp_file (argv[1])
00144 
00145 #define TEST_FUNCTION do_test (argc, argv)
00146 #include "../test-skeleton.c"