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