1 /* mdate.c -- Show system date/time in all locales. -*- coding: euc-jp; -*-
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., 51 Franklin Street, Fifth Floor,
24 @enpage m17n-date display date and time
26 @section m17n-date-synopsis SYNOPSIS
28 m17n-date [ OPTION ... ]
30 @section m17n-date-description DESCRIPTION
32 Display the system date and time in many locales on a window.
34 The following OPTIONs are available.
48 @japage m17n-date Æü»þ¤òɽ¼¨¤¹¤ë
50 @section m17n-date-synopsis ¥·¥Î¥×¥·¥¹
52 m17n-date [ OPTION ... ]
54 @section m17n-date-description ÀâÌÀ
56 ¥·¥¹¥Æ¥à¤ÎÆü»þ¤ò¤µ¤Þ¤¶¤Þ¤Ê¥í¥±¡¼¥ë¤Ç¥¦¥£¥ó¥É¥¦¤Ëɽ¼¨¤¹¤ë¡£
58 °Ê²¼¤Î¥ª¥×¥·¥ç¥ó¤¬ÍøÍѤǤ¤ë¡£
64 ¥Ð¡¼¥¸¥ç¥óÈÖ¹æ¤òɽ¼¨¤¹¤ë¡£
68 ¤³¤Î¥á¥Ã¥»¡¼¥¸¤òɽ¼¨¤¹¤ë¡£
83 #include <m17n-misc.h>
85 #define VERSION "1.3.4"
87 /* Return a plist of all locales currently avairable on the system.
88 The keys and values of the plist are locale name symbols and
89 pointers to MLocale respectively. */
92 list_system_locales ()
98 /* Run the command "locale -a" to get a list of locales. */
99 if (! (p = popen ("locale -a", "r")))
101 fprintf (stderr, "Can't run `locale -a'.\n");
106 /* Read from the pipe one line by one. */
107 while (fgets (buf, 1024, p))
110 int len = strlen (buf);
112 if (buf[len - 1] == '\n')
114 locale = mlocale_set (LC_TIME, buf);
116 /* If the locale is surely usable, it is not duplicated, and we
117 know the corresponding coding system, add it to the list. */
119 && ! mplist_get (plist, mlocale_get_prop (locale, Mname))
120 && mlocale_get_prop (locale, Mcoding) != Mnil)
121 mplist_add (plist, mlocale_get_prop (locale, Mname), locale);
128 /* Print the usage of this program (the name is PROG), and exit with
132 help_exit (char *prog, int exit_code)
140 printf ("Usage: %s [ OPTION ...]\n", prog);
141 printf ("Display the system date and time in many locales on a window.\n");
142 printf ("The following OPTIONs are available.\n");
143 printf (" %-13s %s", "--version", "Print version number.\n");
144 printf (" %-13s %s", "-h, --help", "Print this message.\n");
149 /* Format MSG by FMT and print the result to the stderr, and exit. */
151 #define FATAL_ERROR(fmt, arg) \
153 fprintf (stderr, fmt, arg); \
159 main (int argc, char **argv)
161 time_t current_time_t = time (NULL);
162 struct tm *current_time_tm = localtime (¤t_time_t);
163 /* List of all locales. */
164 MPlist *locale_list, *plist;
165 /* Text to be shown. */
169 for (i = 1; i < argc; i++)
171 if (! strcmp (argv[i], "--help")
172 || ! strcmp (argv[i], "-h")
173 || ! strcmp (argv[i], "-?"))
174 help_exit (argv[0], 0);
175 else if (! strcmp (argv[i], "--version"))
177 printf ("m17n-date (m17n library) %s\n", VERSION);
178 printf ("Copyright (C) 2003 AIST, JAPAN\n");
182 help_exit (argv[0], 1);
186 /* Initialize the m17n library. */
188 if (merror_code != MERROR_NONE)
189 FATAL_ERROR ("%s\n", "Fail to initialize the m17n library.");
191 /* Get a locale list in LOCALE_LIST, and generate an M-text that
192 contains date string in each locale. */
193 locale_list = list_system_locales ();
196 while (mplist_key (plist) != Mnil)
198 char *name = msymbol_name (mplist_key (plist));
201 MLocale *locale = mplist_value (plist);
202 MSymbol coding = mlocale_get_prop (locale, Mcoding);
203 /* One line text for this locale. The format is:
204 "LOCALE-NAME: DATE-AND-TIME-STRING". */
207 sprintf (fmtbuf, "%16s: ", name);
208 len = strlen (fmtbuf);
209 thisline = mconv_decode_buffer (coding, (unsigned char *) fmtbuf, len);
212 mlocale_set (LC_TIME, name);
213 if (mtext_ftime (thisline, "%c", current_time_tm, NULL) > 0)
215 mtext_cat_char (thisline, '\n');
216 mtext_cat (mt, thisline);
218 m17n_object_unref (thisline);
220 plist = mplist_next (plist);
223 /* Show the generated M-text by another example program "mview". */
225 FILE *p = popen ("m17n-view", "w");
228 FATAL_ERROR ("%s\n", "Can't run the program mview!");
229 mconv_encode_stream (Mcoding_utf_8, mt, p);
234 m17n_object_unref (locale_list);
235 m17n_object_unref (mt);
240 #endif /* not FOR_DOXYGEN */