Blame time/bug-getdate1.c

Packit 6c4009
/* BZ #5451 */
Packit 6c4009
#include <time.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
Packit 6c4009
#include <support/temp_file.h>
Packit 6c4009
Packit 6c4009
static char *templ_filename;
Packit 6c4009
Packit 6c4009
// Writes template given as parameter to file,
Packit 6c4009
// specified as the argument
Packit 6c4009
static void
Packit 6c4009
output_to_template_file (const char *str)
Packit 6c4009
{
Packit 6c4009
  FILE *fd = fopen (templ_filename, "w");
Packit 6c4009
  if (fd == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("Can not open file for writing\n");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  fprintf (fd, "%s\n", str);
Packit 6c4009
  fclose (fd);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
// Calls getdate() function with specified parameter,
Packit 6c4009
// specified as the argument, also checks the contents of
Packit 6c4009
// file with template and prints the result
Packit 6c4009
static int
Packit 6c4009
process_getdate_on (const char *str)
Packit 6c4009
{
Packit 6c4009
  struct tm *res;
Packit 6c4009
  char templ[1000];
Packit 6c4009
  FILE *fd = fopen (templ_filename, "r");
Packit 6c4009
Packit 6c4009
  if (fd == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("Can not open file for reading\n");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (fgets (templ, 1000, fd) == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("Can not read file\n");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
  fclose (fd);
Packit 6c4009
Packit 6c4009
  res = getdate (str);
Packit 6c4009
  if (res == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("Failed on getdate(\"%s\"), template is: %s", str, templ);
Packit 6c4009
      printf ("Error number: %d\n\n", getdate_err);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  printf ("Success on getdate(\"%s\"), template is: %s\n", str, templ);
Packit 6c4009
  printf ("Result is\n");
Packit 6c4009
  printf ("Seconds: %d\n", res->tm_sec);
Packit 6c4009
  printf ("Minutes: %d\n", res->tm_min);
Packit 6c4009
  printf ("Hour: %d\n", res->tm_hour);
Packit 6c4009
  printf ("Day of month: %d\n", res->tm_mday);
Packit 6c4009
  printf ("Month of year: %d\n", res->tm_mon);
Packit 6c4009
  printf ("Years since 1900: %d\n", res->tm_year);
Packit 6c4009
  printf ("Day of week: %d\n", res->tm_wday);
Packit 6c4009
  printf ("Day of year: %d\n", res->tm_yday);
Packit 6c4009
  printf ("Daylight Savings flag: %d\n\n", res->tm_isdst);
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (int argc, char *argv[])
Packit 6c4009
{
Packit 6c4009
Packit 6c4009
  templ_filename = argv[1];
Packit 6c4009
Packit 6c4009
  setenv ("DATEMSK", templ_filename, 1);
Packit 6c4009
Packit 6c4009
  /*
Packit 6c4009
   * The following 4 testcases reproduce the problem:
Packit 6c4009
   * 1. Templates "%S" and "%M" are not processed,
Packit 6c4009
   *    when used without "%H" template
Packit 6c4009
   */
Packit 6c4009
  int res = 0;
Packit 6c4009
  output_to_template_file ("%M");
Packit 6c4009
  res |= process_getdate_on ("1");
Packit 6c4009
Packit 6c4009
  output_to_template_file ("%M %H");
Packit 6c4009
  res |= process_getdate_on ("1 2");
Packit 6c4009
Packit 6c4009
  output_to_template_file ("%S");
Packit 6c4009
  res |= process_getdate_on ("1");
Packit 6c4009
Packit 6c4009
  output_to_template_file ("%S %H");
Packit 6c4009
  res |= process_getdate_on ("1 2");
Packit 6c4009
Packit 6c4009
  /*
Packit 6c4009
   * The following 9 testcases reproduce the problem:
Packit 6c4009
   * 2. Templates "%Y", "%y", "%d", "%C", "%C %y"
Packit 6c4009
   *    are not processed separately
Packit 6c4009
   */
Packit 6c4009
  output_to_template_file ("%Y");
Packit 6c4009
  process_getdate_on ("2001");
Packit 6c4009
Packit 6c4009
  output_to_template_file ("%Y %m");
Packit 6c4009
  res |= process_getdate_on ("2001 3");
Packit 6c4009
Packit 6c4009
  output_to_template_file ("%y");
Packit 6c4009
  res |= process_getdate_on ("70");
Packit 6c4009
Packit 6c4009
  output_to_template_file ("%y %m");
Packit 6c4009
  res |= process_getdate_on ("70 3");
Packit 6c4009
Packit 6c4009
  output_to_template_file ("%d");
Packit 6c4009
  res |= process_getdate_on ("06");
Packit 6c4009
Packit 6c4009
  output_to_template_file ("%d %m");
Packit 6c4009
  res |= process_getdate_on ("25 3");
Packit 6c4009
Packit 6c4009
  output_to_template_file ("%C");
Packit 6c4009
  res |= process_getdate_on ("20");
Packit 6c4009
Packit 6c4009
  output_to_template_file ("%C %y %m");
Packit 6c4009
  res |= process_getdate_on ("20 3 2");
Packit 6c4009
Packit 6c4009
  output_to_template_file ("%C %y");
Packit 6c4009
  res |= process_getdate_on ("20 5");
Packit 6c4009
Packit 6c4009
  /*
Packit 6c4009
   * The following testcase reproduces the problem:
Packit 6c4009
   * 3. When template is "%Y %m", day of month is not set
Packit 6c4009
   *    to 1 as standard requires
Packit 6c4009
   */
Packit 6c4009
  output_to_template_file ("%Y %m");
Packit 6c4009
  res |= process_getdate_on ("2008 3");
Packit 6c4009
Packit 6c4009
  return res;
Packit 6c4009
}
Packit 6c4009
#define TEST_FUNCTION_ARGV do_test
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
do_prepare (int argc, char **argv)
Packit 6c4009
{
Packit 6c4009
  if (argc < 2)
Packit 6c4009
    {
Packit 6c4009
      puts ("Command line: progname template_filename_full_path");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
  add_temp_file (argv[1]);
Packit 6c4009
}
Packit 6c4009
#define PREPARE do_prepare
Packit 6c4009
Packit 6c4009
#include <support/test-driver.c>