summary refs log tree commit diff
path: root/time/bug-getdate1.c
blob: 7da88f4548d926eb61f377cdb0e30a1c11427658 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* BZ #5451 */
#include <time.h>
#include <stdio.h>
#include <stdlib.h>

static char *templ_filename;

// Writes template given as parameter to file,
// specified as the argument
static void
output_to_template_file (const char *str)
{
  FILE *fd = fopen (templ_filename, "w");
  if (fd == NULL)
    {
      printf ("Can not open file for writing\n");
      exit (1);
    }

  fprintf (fd, "%s\n", str);
  fclose (fd);
}

// Calls getdate() function with specified parameter,
// specified as the argument, also checks the contents of
// file with template and prints the result
static int
process_getdate_on (const char *str)
{
  struct tm *res;
  char templ[1000];
  FILE *fd = fopen (templ_filename, "r");

  if (fd == NULL)
    {
      printf ("Can not open file for reading\n");
      exit (1);
    }

  if (fgets (templ, 1000, fd) == NULL)
    {
      printf ("Can not read file\n");
      exit (1);
    }
  fclose (fd);

  res = getdate (str);
  if (res == NULL)
    {
      printf ("Failed on getdate(\"%s\"), template is: %s", str, templ);
      printf ("Error number: %d\n\n", getdate_err);
      return 1;
    }
  printf ("Success on getdate(\"%s\"), template is: %s\n", str, templ);
  printf ("Result is\n");
  printf ("Seconds: %d\n", res->tm_sec);
  printf ("Minutes: %d\n", res->tm_min);
  printf ("Hour: %d\n", res->tm_hour);
  printf ("Day of month: %d\n", res->tm_mday);
  printf ("Month of year: %d\n", res->tm_mon);
  printf ("Years since 1900: %d\n", res->tm_year);
  printf ("Day of week: %d\n", res->tm_wday);
  printf ("Day of year: %d\n", res->tm_yday);
  printf ("Daylight Savings flag: %d\n\n", res->tm_isdst);
  return 0;
}

static int
do_test (int argc, char *argv[])
{

  templ_filename = argv[1];

  setenv ("DATEMSK", templ_filename, 1);

  /*
   * The following 4 testcases reproduce the problem:
   * 1. Templates "%S" and "%M" are not processed,
   *    when used without "%H" template
   */
  int res = 0;
  output_to_template_file ("%M");
  res |= process_getdate_on ("1");

  output_to_template_file ("%M %H");
  res |= process_getdate_on ("1 2");

  output_to_template_file ("%S");
  res |= process_getdate_on ("1");

  output_to_template_file ("%S %H");
  res |= process_getdate_on ("1 2");

  /*
   * The following 9 testcases reproduce the problem:
   * 2. Templates "%Y", "%y", "%d", "%C", "%C %y"
   *    are not processed separately
   */
  output_to_template_file ("%Y");
  process_getdate_on ("2001");

  output_to_template_file ("%Y %m");
  res |= process_getdate_on ("2001 3");

  output_to_template_file ("%y");
  res |= process_getdate_on ("70");

  output_to_template_file ("%y %m");
  res |= process_getdate_on ("70 3");

  output_to_template_file ("%d");
  res |= process_getdate_on ("06");

  output_to_template_file ("%d %m");
  res |= process_getdate_on ("25 3");

  output_to_template_file ("%C");
  res |= process_getdate_on ("98");

  output_to_template_file ("%C %y %m");
  res |= process_getdate_on ("98 3 2");

  output_to_template_file ("%C %y");
  res |= process_getdate_on ("21 5");

  /*
   * The following testcase reproduces the problem:
   * 3. When template is "%Y %m", day of month is not set
   *    to 1 as standard requires
   */
  output_to_template_file ("%Y %m");
  res |= process_getdate_on ("2008 3");

  return res;
}

#define PREPARE(argc, argv) \
  if (argc < 2)								\
    {									\
      puts ("Command line: progname template_filename_full_path");	\
      exit (1);								\
    }									\
  add_temp_file (argv[1])

#define TEST_FUNCTION do_test (argc, argv)
#include "../test-skeleton.c"