1 /* mdate.c -- Show the sysmet date and time in all locales.
2 Copyright (C) 2003, 2004
3 National Institute of Advanced Industrial Science and Technology (AIST)
4 Registration Number H15PRO112
6 This file is part of the m17n library.
8 The m17n library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public License
10 as published by the Free Software Foundation; either version 2.1 of
11 the License, or (at your option) any later version.
13 The m17n library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public
19 License along with the m17n library; if not, write to the Free
20 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
24 @page mdate display date and time
26 @section mdate-synopsis SYNOPSIS
30 @section mdate-description DESCRIPTION
32 Display the system date and time in many locales on a window.
34 The following OPTIONs are available.
58 #include <m17n-misc.h>
62 /* Return a plist of all locales currently avairable on the system.
63 The keys and values of the plist are locale name symbols and
64 pointers to MLocale respectively. */
67 list_system_locales ()
73 /* Run the command "locale -a" to get a list of locales. */
74 if (! (p = popen ("locale -a", "r")))
76 fprintf (stderr, "Can't run `locale -a'.\n");
81 /* Read from the pipe one line by one. */
82 while (fgets (buf, 1024, p))
85 int len = strlen (buf);
87 if (buf[len - 1] == '\n')
89 locale = mlocale_set (LC_TIME, buf);
91 /* If the locale is surely usable, it is not duplicated, and we
92 know the corresponding coding system, add it to the list. */
94 && ! mplist_get (plist, mlocale_get_prop (locale, Mname))
95 && mlocale_get_prop (locale, Mcoding) != Mnil)
96 mplist_add (plist, mlocale_get_prop (locale, Mname), locale);
103 /* Print the usage of this program (the name is PROG), and exit with
107 help_exit (char *prog, int exit_code)
115 printf ("Usage: %s [ OPTION ...]\n", prog);
116 printf ("Display the system date and time in many locales on a window.\n");
117 printf ("The following OPTIONs are available.\n");
118 printf (" %-13s %s", "--version", "Print version number.\n");
119 printf (" %-13s %s", "-h, --help", "Print this message.\n");
124 /* Format MSG by FMT and print the result to the stderr, and exit. */
126 #define FATAL_ERROR(fmt, arg) \
128 fprintf (stderr, fmt, arg); \
134 main (int argc, char **argv)
136 time_t current_time_t = time (NULL);
137 struct tm *current_time_tm = localtime (¤t_time_t);
138 /* List of all locales. */
139 MPlist *locale_list, *plist;
140 /* Text to be shown. */
144 for (i = 1; i < argc; i++)
146 if (! strcmp (argv[i], "--help")
147 || ! strcmp (argv[i], "-h")
148 || ! strcmp (argv[i], "-?"))
149 help_exit (argv[0], 0);
150 else if (! strcmp (argv[i], "--version"))
152 printf ("mdate (m17n library) %s\n", VERSION);
153 printf ("Copyright (C) 2003 AIST, JAPAN\n");
157 help_exit (argv[0], 1);
161 /* Initialize the m17n library. */
163 if (merror_code != MERROR_NONE)
164 FATAL_ERROR ("%s\n", "Fail to initialize the m17n library.");
166 /* Get a local list in LOCALE_LIST, and generate an M-text that
167 contains date string in each locale. */
168 locale_list = list_system_locales ();
171 while (mplist_key (plist) != Mnil)
173 char *name = msymbol_name (mplist_key (plist));
176 MLocale *locale = mplist_value (plist);
177 MSymbol coding = mlocale_get_prop (locale, Mcoding);
178 /* One line text for this locale. The format is:
179 "LOCALE-NAME: DATE-AND-TIME-STRING". */
182 sprintf (fmtbuf, "%16s: ", name);
183 len = strlen (fmtbuf);
184 thisline = mconv_decode_buffer (coding, (unsigned char *) fmtbuf, len);
187 mlocale_set (LC_TIME, name);
188 if (mtext_ftime (thisline, "%c", current_time_tm, NULL) > 0)
190 mtext_cat_char (thisline, '\n');
191 mtext_cat (mt, thisline);
193 m17n_object_unref (thisline);
195 plist = mplist_next (plist);
198 /* Show the generated M-text by another example program "mview". */
200 FILE *p = popen ("mview", "w");
203 FATAL_ERROR ("%s\n", "Can't run the program mview!");
204 mconv_encode_stream (Mcoding_utf_8, mt, p);
209 m17n_object_unref (locale_list);
210 m17n_object_unref (mt);
215 #endif /* not FOR_DOXYGEN */