1/* BZ #5451 */
2#include <time.h>
3#include <stdio.h>
4#include <stdlib.h>
5
6#include <support/temp_file.h>
7
8static char *templ_filename;
9
10// Writes template given as parameter to file,
11// specified as the argument
12static void
13output_to_template_file (const char *str)
14{
15 FILE *fd = fopen (templ_filename, "w");
16 if (fd == NULL)
17 {
18 printf (format: "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
29static int
30process_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 (format: "Can not open file for reading\n");
39 exit (1);
40 }
41
42 if (fgets (s: templ, n: 1000, stream: fd) == NULL)
43 {
44 printf (format: "Can not read file\n");
45 exit (1);
46 }
47 fclose (fd);
48
49 res = getdate (string: str);
50 if (res == NULL)
51 {
52 printf (format: "Failed on getdate(\"%s\"), template is: %s", str, templ);
53 printf (format: "Error number: %d\n\n", getdate_err);
54 return 1;
55 }
56 printf (format: "Success on getdate(\"%s\"), template is: %s\n", str, templ);
57 printf (format: "Result is\n");
58 printf (format: "Seconds: %d\n", res->tm_sec);
59 printf (format: "Minutes: %d\n", res->tm_min);
60 printf (format: "Hour: %d\n", res->tm_hour);
61 printf (format: "Day of month: %d\n", res->tm_mday);
62 printf (format: "Month of year: %d\n", res->tm_mon);
63 printf (format: "Years since 1900: %d\n", res->tm_year);
64 printf (format: "Day of week: %d\n", res->tm_wday);
65 printf (format: "Day of year: %d\n", res->tm_yday);
66 printf (format: "Daylight Savings flag: %d\n\n", res->tm_isdst);
67 return 0;
68}
69
70static int
71do_test (int argc, char *argv[])
72{
73
74 templ_filename = argv[1];
75
76 setenv (name: "DATEMSK", value: templ_filename, replace: 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 (str: "%M");
85 res |= process_getdate_on (str: "1");
86
87 output_to_template_file (str: "%M %H");
88 res |= process_getdate_on (str: "1 2");
89
90 output_to_template_file (str: "%S");
91 res |= process_getdate_on (str: "1");
92
93 output_to_template_file (str: "%S %H");
94 res |= process_getdate_on (str: "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 (str: "%Y");
102 process_getdate_on (str: "2001");
103
104 output_to_template_file (str: "%Y %m");
105 res |= process_getdate_on (str: "2001 3");
106
107 output_to_template_file (str: "%y");
108 res |= process_getdate_on (str: "70");
109
110 output_to_template_file (str: "%y %m");
111 res |= process_getdate_on (str: "70 3");
112
113 output_to_template_file (str: "%d");
114 res |= process_getdate_on (str: "06");
115
116 output_to_template_file (str: "%d %m");
117 res |= process_getdate_on (str: "25 3");
118
119 output_to_template_file (str: "%C");
120 res |= process_getdate_on (str: "20");
121
122 output_to_template_file (str: "%C %y %m");
123 res |= process_getdate_on (str: "20 3 2");
124
125 output_to_template_file (str: "%C %y");
126 res |= process_getdate_on (str: "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 (str: "%Y %m");
134 res |= process_getdate_on (str: "2008 3");
135
136 return res;
137}
138#define TEST_FUNCTION_ARGV do_test
139
140static void
141do_prepare (int argc, char **argv)
142{
143 if (argc < 2)
144 {
145 puts (s: "Command line: progname template_filename_full_path");
146 exit (1);
147 }
148 add_temp_file (name: argv[1]);
149}
150#define PREPARE do_prepare
151
152#include <support/test-driver.c>
153

source code of glibc/time/bug-getdate1.c