Commit 025c888a 025c888ab07458c7b2a1e73d6b607cc9ce26cca6 by Sergey Poznyakoff

Implement locale-independent strftime function.

* include/mailutils/util.h (mu_c_streamftime): New prototype.
* libmailutils/base/date.c (mu_c_streamftime): New function.
* libmailutils/tests/.gitignore: Update.
* libmailutils/tests/strftime.at: New test script.
* libmailutils/tests/strftime.c: New test program.
* libmailutils/tests/Makefile.am (noinst_PROGRAMS): Add strftime.
(TESTSUITE_AT): Add strftime.at.
* libmailutils/tests/testsuite.at: Include strftime.at.
1 parent 217ae7a2
...@@ -71,6 +71,9 @@ time_t mu_tm2time (struct tm *timeptr, mu_timezone *tz); ...@@ -71,6 +71,9 @@ time_t mu_tm2time (struct tm *timeptr, mu_timezone *tz);
71 size_t mu_strftime (char *s, size_t max, const char *format, 71 size_t mu_strftime (char *s, size_t max, const char *format,
72 const struct tm *tm); 72 const struct tm *tm);
73 73
74 int mu_c_streamftime (mu_stream_t str, const char *fmt, struct tm *tm,
75 struct mu_timezone *tz);
76
74 /* ----------------------- */ 77 /* ----------------------- */
75 /* File & path names. */ 78 /* File & path names. */
76 /* ----------------------- */ 79 /* ----------------------- */
......
...@@ -16,6 +16,7 @@ imapio ...@@ -16,6 +16,7 @@ imapio
16 listop 16 listop
17 mailcap 17 mailcap
18 prop 18 prop
19 strftime
19 url-comp 20 url-comp
20 url-parse 21 url-parse
21 wicket 22 wicket
......
...@@ -51,6 +51,7 @@ noinst_PROGRAMS = \ ...@@ -51,6 +51,7 @@ noinst_PROGRAMS = \
51 listop\ 51 listop\
52 mailcap\ 52 mailcap\
53 prop\ 53 prop\
54 strftime\
54 tempfile\ 55 tempfile\
55 url-comp\ 56 url-comp\
56 url-parse\ 57 url-parse\
...@@ -81,6 +82,7 @@ TESTSUITE_AT = \ ...@@ -81,6 +82,7 @@ TESTSUITE_AT = \
81 list.at\ 82 list.at\
82 mailcap.at\ 83 mailcap.at\
83 prop.at\ 84 prop.at\
85 strftime.at\
84 testsuite.at\ 86 testsuite.at\
85 url.at\ 87 url.at\
86 url-comp.at\ 88 url-comp.at\
......
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001, 2007, 2009, 2010, 2011 Free Software
3 Foundation, Inc.
4
5 GNU Mailutils is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3, or (at your option)
8 any later version.
9
10 GNU Mailutils is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <mailutils/error.h>
25 #include <mailutils/errno.h>
26 #include <mailutils/util.h>
27 #include <mailutils/stream.h>
28 #include <mailutils/cctype.h>
29 #include <mailutils/cstr.h>
30 #include <mailutils/stdstream.h>
31
32 void
33 usage ()
34 {
35 mu_stream_printf (mu_strout, "usage: %s [-format=FMT] [-tz=TZ]\n",
36 mu_program_name);
37 exit (0);
38 }
39
40 int
41 main (int argc, char **argv)
42 {
43 int rc, i;
44 char *format = "%c";
45 char *buf = NULL;
46 size_t size = 0;
47 size_t n;
48 struct mu_timezone tz, *tzp = NULL;
49
50 mu_set_program_name (argv[0]);
51 mu_stdstream_setup (MU_STDSTREAM_RESET_NONE);
52
53 memset (&tz, 0, sizeof tz);
54 for (i = 1; i < argc; i++)
55 {
56 char *opt = argv[i];
57
58 if (strncmp (opt, "-format=", 8) == 0)
59 format = opt + 8;
60 else if (strncmp (opt, "-tz=", 4) == 0)
61 {
62 int sign;
63 int n = atoi (opt + 4);
64 if (n < 0)
65 {
66 sign = -1;
67 n = - n;
68 }
69 else
70 sign = 1;
71 tz.utc_offset = sign * ((n / 100 * 60) + n % 100) * 60;
72 tzp = &tz;
73 }
74 else if (strcmp (opt, "-h") == 0)
75 usage ();
76 else
77 {
78 mu_error ("%s: unrecognized argument", opt);
79 exit (1);
80 }
81 }
82
83 while ((rc = mu_stream_getline (mu_strin, &buf, &size, &n)) == 0 && n > 0)
84 {
85 char *p;
86 struct tm *tm;
87 time_t t;
88
89 mu_rtrim_class (buf, MU_CTYPE_ENDLN);
90
91 if (*buf == ';')
92 {
93 mu_printf ("%s\n", buf);
94 continue;
95 }
96 t = strtoul (buf, &p, 10);
97 if (*p)
98 {
99 mu_error ("bad input line near %s", p);
100 continue;
101 }
102
103 tm = gmtime (&t);
104 mu_c_streamftime (mu_strout, format, tm, tzp);
105 mu_printf ("\n");
106 }
107
108 if (rc)
109 {
110 mu_error ("%s", mu_strerror (rc));
111 return 1;
112 }
113 return 0;
114 }
...@@ -83,4 +83,6 @@ m4_include([debugspec.at]) ...@@ -83,4 +83,6 @@ m4_include([debugspec.at])
83 AT_BANNER([IMAP IO]) 83 AT_BANNER([IMAP IO])
84 m4_include([imapio.at]) 84 m4_include([imapio.at])
85 85
86 m4_include([strftime.at])
87
86 m4_include([fsaf.at]) 88 m4_include([fsaf.at])
...\ No newline at end of file ...\ No newline at end of file
......