Bump to 1.14.1
[platform/upstream/augeas.git] / tests / test-strftime.c
1 /* Test that posixtime works as required.
2    Copyright (C) 2011-2016 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 /* Written by Jim Meyering.  */
18
19 #include <config.h>
20
21 #include "strftime.h"
22
23 #include <errno.h>
24 #include <stdio.h>
25 #include <time.h>
26 #include <string.h>
27
28 #include "macros.h"
29 #define STREQ(a, b) (strcmp (a, b) == 0)
30
31 struct posixtm_test
32 {
33   time_t in;
34   int in_ns;
35   char const *fmt;
36   char const *exp;
37 };
38
39 static struct posixtm_test const T[] =
40   {
41     { 1300000000, 0,            "%F", "2011-03-13" },
42     { 0,          10,           "%T.%N", "00:00:00.000000010" },
43     { 0,          0,            NULL, NULL }
44   };
45
46 static int
47 posixtm_test (void)
48 {
49   int fail = 0;
50   unsigned int i;
51
52   for (i = 0; T[i].fmt; i++)
53     {
54       char buf[1000];
55       time_t t = T[i].in;
56       struct tm *tm = gmtime (&t);
57       size_t n;
58
59       ASSERT (tm);
60
61       n = nstrftime (buf, sizeof buf, T[i].fmt, tm, 0, T[i].in_ns);
62       if (n == 0)
63         {
64           fail = 1;
65           printf ("nstrftime failed with format %s\n", T[i].fmt);
66         }
67
68       if (! STREQ (buf, T[i].exp))
69         {
70           fail = 1;
71           printf ("%s: result mismatch: got %s, expected %s\n",
72                   T[i].fmt, buf, T[i].exp);
73         }
74     }
75
76   return fail;
77 }
78
79 struct tzalloc_test
80 {
81   timezone_t tz;
82   char const *setting;
83 };
84
85 static struct tzalloc_test TZ[] =
86   {
87 #define Pacific 0
88     { 0, "PST8PDT,M3.2.0,M11.1.0"      },
89 #define Arizona 1
90     { 0, "MST7"                        },
91 #define UTC 2
92     { 0, 0                             },
93 #define CentEur 3
94     { 0, "CET-1CEST,M3.5.0,M10.5.0/3"  },
95 #define Japan 4
96     { 0, "JST-9"                       },
97 #define NZ 5
98     { 0, "NZST-12NZDT,M9.5.0,M4.1.0/3" },
99     { 0 }
100   };
101
102 struct localtime_rz_test
103 {
104   /* Input parameters.  */
105   struct tzalloc_test *tza;
106   time_t t;
107
108   /* Expected result.  */
109   char const *exp;
110
111   /* Determines if an incorrectly unset tm_isdst
112      results in failure or just a warning.  */
113   int ahistorical;
114 };
115
116 static struct localtime_rz_test LT[] =
117   {
118     { TZ+Pacific,          0, "1969-12-31 16:00:00 -0800 (PST)",  0 },
119     { TZ+Arizona,          0, "1969-12-31 17:00:00 -0700 (MST)",  0 },
120     { TZ+UTC    ,          0, "1970-01-01 00:00:00 +0000 (UTC)",  0 },
121     { TZ+CentEur,          0, "1970-01-01 01:00:00 +0100 (CET)",  0 },
122     { TZ+Japan  ,          0, "1970-01-01 09:00:00 +0900 (JST)",  0 },
123     { TZ+NZ     ,          0, "1970-01-01 13:00:00 +1300 (NZDT)", 1 },
124     { TZ+Pacific,  500000001, "1985-11-04 16:53:21 -0800 (PST)",  0 },
125     { TZ+Arizona,  500000001, "1985-11-04 17:53:21 -0700 (MST)",  0 },
126     { TZ+UTC    ,  500000001, "1985-11-05 00:53:21 +0000 (UTC)",  0 },
127     { TZ+CentEur,  500000001, "1985-11-05 01:53:21 +0100 (CET)",  1 },
128     { TZ+Japan  ,  500000001, "1985-11-05 09:53:21 +0900 (JST)",  0 },
129     { TZ+NZ     ,  500000001, "1985-11-05 13:53:21 +1300 (NZDT)", 0 },
130     { TZ+Pacific, 1000000002, "2001-09-08 18:46:42 -0700 (PDT)",  0 },
131     { TZ+Arizona, 1000000002, "2001-09-08 18:46:42 -0700 (MST)",  0 },
132     { TZ+UTC    , 1000000002, "2001-09-09 01:46:42 +0000 (UTC)",  0 },
133     { TZ+CentEur, 1000000002, "2001-09-09 03:46:42 +0200 (CEST)", 0 },
134     { TZ+Japan  , 1000000002, "2001-09-09 10:46:42 +0900 (JST)",  0 },
135     { TZ+NZ     , 1000000002, "2001-09-09 13:46:42 +1200 (NZST)", 0 },
136     { 0 }
137   };
138
139 static int
140 tzalloc_test (void)
141 {
142   int fail = 0;
143   int i;
144
145   for (i = 0; LT[i].tza; i++)
146     {
147       struct tzalloc_test *tza = LT[i].tza;
148       long lt = LT[i].t;
149       timezone_t tz = tza->tz;
150       char const *setting;
151       static char const format[] = "%Y-%m-%d %H:%M:%S %z (%Z)";
152       char buf[1000];
153       struct tm tm;
154       size_t n;
155
156       if (!tz && tza->setting)
157         {
158           tz = tzalloc (tza->setting);
159           if (!tz)
160             {
161               fail = 1;
162               printf ("%s: tzalloc: %s\n", TZ[i].setting, strerror (errno));
163               continue;
164             }
165           tza->tz = tz;
166         }
167
168       setting = tza->setting ? tza->setting : "UTC0";
169
170       if (!localtime_rz (tz, &LT[i].t, &tm))
171         {
172           fail = 1;
173           printf ("%s: %ld: localtime_rz: %s\n", setting, lt,
174                   strerror (errno));
175           continue;
176         }
177
178       n = nstrftime (buf, sizeof buf, format, &tm, tz, 0);
179       if (n == 0)
180         {
181           fail = 1;
182           printf ("%s: %ld: nstrftime failed\n", setting, lt);
183           continue;
184         }
185
186       if (! (STREQ (buf, LT[i].exp)
187              || (!tz && n == strlen (LT[i].exp)
188                  && memcmp (buf, LT[i].exp, n - sizeof "(GMT)" + 1) == 0
189                  && STREQ (buf + n - sizeof "(GMT)" + 1, "(GMT)"))))
190         {
191           /* Don't fail for unhandled dst in ahistorical entries,
192              as gnulib doesn't currently fix that issue, seen on Darwin 14.  */
193           if (!LT[i].ahistorical || tm.tm_isdst)
194             fail = 1;
195           printf ("%s: expected \"%s\", got \"%s\"\n",
196                   setting, LT[i].exp, buf);
197         }
198     }
199
200   return fail;
201 }
202
203 int
204 main (void)
205 {
206   int fail = 0;
207   fail |= posixtm_test ();
208   fail |= tzalloc_test ();
209   return fail;
210 }
211
212 /*
213 Local Variables:
214 indent-tabs-mode: nil
215 End:
216 */