(root)/
glibc-2.38/
time/
bug-getdate1.c
       1  /* BZ #5451 */
       2  #include <time.h>
       3  #include <stdio.h>
       4  #include <stdlib.h>
       5  
       6  #include <support/temp_file.h>
       7  
       8  static char *templ_filename;
       9  
      10  // Writes template given as parameter to file,
      11  // specified as the argument
      12  static void
      13  output_to_template_file (const char *str)
      14  {
      15    FILE *fd = fopen (templ_filename, "w");
      16    if (fd == NULL)
      17      {
      18        printf ("Can not open file for writing\n");
      19        exit (1);
      20      }
      21  
      22    fprintf (fd, "%s\n", str);
      23    fclose (fd);
      24  }
      25  
      26  // Calls getdate() function with specified parameter,
      27  // specified as the argument, also checks the contents of
      28  // file with template and prints the result
      29  static int
      30  process_getdate_on (const char *str)
      31  {
      32    struct tm *res;
      33    char templ[1000];
      34    FILE *fd = fopen (templ_filename, "r");
      35  
      36    if (fd == NULL)
      37      {
      38        printf ("Can not open file for reading\n");
      39        exit (1);
      40      }
      41  
      42    if (fgets (templ, 1000, fd) == NULL)
      43      {
      44        printf ("Can not read file\n");
      45        exit (1);
      46      }
      47    fclose (fd);
      48  
      49    res = getdate (str);
      50    if (res == NULL)
      51      {
      52        printf ("Failed on getdate(\"%s\"), template is: %s", str, templ);
      53        printf ("Error number: %d\n\n", getdate_err);
      54        return 1;
      55      }
      56    printf ("Success on getdate(\"%s\"), template is: %s\n", str, templ);
      57    printf ("Result is\n");
      58    printf ("Seconds: %d\n", res->tm_sec);
      59    printf ("Minutes: %d\n", res->tm_min);
      60    printf ("Hour: %d\n", res->tm_hour);
      61    printf ("Day of month: %d\n", res->tm_mday);
      62    printf ("Month of year: %d\n", res->tm_mon);
      63    printf ("Years since 1900: %d\n", res->tm_year);
      64    printf ("Day of week: %d\n", res->tm_wday);
      65    printf ("Day of year: %d\n", res->tm_yday);
      66    printf ("Daylight Savings flag: %d\n\n", res->tm_isdst);
      67    return 0;
      68  }
      69  
      70  static int
      71  do_test (int argc, char *argv[])
      72  {
      73  
      74    templ_filename = argv[1];
      75  
      76    setenv ("DATEMSK", templ_filename, 1);
      77  
      78    /*
      79     * The following 4 testcases reproduce the problem:
      80     * 1. Templates "%S" and "%M" are not processed,
      81     *    when used without "%H" template
      82     */
      83    int res = 0;
      84    output_to_template_file ("%M");
      85    res |= process_getdate_on ("1");
      86  
      87    output_to_template_file ("%M %H");
      88    res |= process_getdate_on ("1 2");
      89  
      90    output_to_template_file ("%S");
      91    res |= process_getdate_on ("1");
      92  
      93    output_to_template_file ("%S %H");
      94    res |= process_getdate_on ("1 2");
      95  
      96    /*
      97     * The following 9 testcases reproduce the problem:
      98     * 2. Templates "%Y", "%y", "%d", "%C", "%C %y"
      99     *    are not processed separately
     100     */
     101    output_to_template_file ("%Y");
     102    process_getdate_on ("2001");
     103  
     104    output_to_template_file ("%Y %m");
     105    res |= process_getdate_on ("2001 3");
     106  
     107    output_to_template_file ("%y");
     108    res |= process_getdate_on ("70");
     109  
     110    output_to_template_file ("%y %m");
     111    res |= process_getdate_on ("70 3");
     112  
     113    output_to_template_file ("%d");
     114    res |= process_getdate_on ("06");
     115  
     116    output_to_template_file ("%d %m");
     117    res |= process_getdate_on ("25 3");
     118  
     119    output_to_template_file ("%C");
     120    res |= process_getdate_on ("20");
     121  
     122    output_to_template_file ("%C %y %m");
     123    res |= process_getdate_on ("20 3 2");
     124  
     125    output_to_template_file ("%C %y");
     126    res |= process_getdate_on ("20 5");
     127  
     128    /*
     129     * The following testcase reproduces the problem:
     130     * 3. When template is "%Y %m", day of month is not set
     131     *    to 1 as standard requires
     132     */
     133    output_to_template_file ("%Y %m");
     134    res |= process_getdate_on ("2008 3");
     135  
     136    return res;
     137  }
     138  #define TEST_FUNCTION_ARGV do_test
     139  
     140  static void
     141  do_prepare (int argc, char **argv)
     142  {
     143    if (argc < 2)
     144      {
     145        puts ("Command line: progname template_filename_full_path");
     146        exit (1);
     147      }
     148    add_temp_file (argv[1]);
     149  }
     150  #define PREPARE do_prepare
     151  
     152  #include <support/test-driver.c>