maint: update copyright
[gnulib.git] / tests / test-strftime.c
1 /* Test that posixtime works as required.
2    Copyright (C) 2011-2014 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 <stdio.h>
24 #include <time.h>
25 #include <string.h>
26
27 #include "macros.h"
28 #define STREQ(a, b) (strcmp (a, b) == 0)
29
30 struct posixtm_test
31 {
32   time_t in;
33   int in_ns;
34   char const *fmt;
35   char const *exp;
36 };
37
38 static struct posixtm_test const T[] =
39   {
40     { 1300000000, 0,            "%F", "2011-03-13" },
41     { 0,          10,           "%T.%N", "00:00:00.000000010" },
42     { 0,          0,            NULL, NULL }
43   };
44
45 int
46 main (void)
47 {
48   int fail = 0;
49   unsigned int i;
50
51   for (i = 0; T[i].fmt; i++)
52     {
53       char buf[1000];
54       time_t t = T[i].in;
55       struct tm *tm = gmtime (&t);
56       size_t n;
57       int utc = 1;
58
59       ASSERT (tm);
60
61       n = nstrftime (buf, sizeof buf, T[i].fmt, tm, utc, 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 /*
80 Local Variables:
81 indent-tabs-mode: nil
82 End:
83 */