1 /* mdate.c -- Show system date/time in all locales. -*- coding: euc-jp; -*-
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008
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 /* Return a plist of all locales currently avairable on the system.
86 The keys and values of the plist are locale name symbols and
87 pointers to MLocale respectively. */
90 list_system_locales ()
96 /* Run the command "locale -a" to get a list of locales. */
97 if (! (p = popen ("locale -a", "r")))
99 fprintf (stderr, "Can't run `locale -a'.\n");
104 /* Read from the pipe one line by one. */
105 while (fgets (buf, 1024, p))
108 int len = strlen (buf);
110 if (buf[len - 1] == '\n')
112 locale = mlocale_set (LC_TIME, buf);
114 /* If the locale is surely usable, it is not duplicated, and we
115 know the corresponding coding system, add it to the list. */
117 && ! mplist_get (plist, mlocale_get_prop (locale, Mname))
118 && mlocale_get_prop (locale, Mcoding) != Mnil)
119 mplist_add (plist, mlocale_get_prop (locale, Mname), locale);
126 /* Print the usage of this program (the name is PROG), and exit with
130 help_exit (char *prog, int exit_code)
138 printf ("Usage: %s [ OPTION ...]\n", prog);
139 printf ("Display the system date and time in many locales on a window.\n");
140 printf ("The following OPTIONs are available.\n");
141 printf (" %-13s %s", "--version", "Print version number.\n");
142 printf (" %-13s %s", "-h, --help", "Print this message.\n");
147 /* Format MSG by FMT and print the result to the stderr, and exit. */
149 #define FATAL_ERROR(fmt, arg) \
151 fprintf (stderr, fmt, arg); \
157 main (int argc, char **argv)
159 time_t current_time_t = time (NULL);
160 struct tm *current_time_tm = localtime (¤t_time_t);
161 /* List of all locales. */
162 MPlist *locale_list, *plist;
163 /* Text to be shown. */
167 for (i = 1; i < argc; i++)
169 if (! strcmp (argv[i], "--help")
170 || ! strcmp (argv[i], "-h")
171 || ! strcmp (argv[i], "-?"))
172 help_exit (argv[0], 0);
173 else if (! strcmp (argv[i], "--version"))
175 printf ("m17n-date (m17n library) %s\n", M17NLIB_VERSION_NAME);
176 printf ("Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 AIST, JAPAN\n");
180 help_exit (argv[0], 1);
184 /* Initialize the m17n library. */
186 if (merror_code != MERROR_NONE)
187 FATAL_ERROR ("%s\n", "Fail to initialize the m17n library.");
189 /* Get a locale list in LOCALE_LIST, and generate an M-text that
190 contains date string in each locale. */
191 locale_list = list_system_locales ();
194 while (mplist_key (plist) != Mnil)
196 char *name = msymbol_name (mplist_key (plist));
199 MLocale *locale = mplist_value (plist);
200 MSymbol coding = mlocale_get_prop (locale, Mcoding);
201 /* One line text for this locale. The format is:
202 "LOCALE-NAME: DATE-AND-TIME-STRING". */
205 sprintf (fmtbuf, "%16s: ", name);
206 len = strlen (fmtbuf);
207 thisline = mconv_decode_buffer (coding, (unsigned char *) fmtbuf, len);
210 mlocale_set (LC_TIME, name);
211 if (mtext_ftime (thisline, "%c", current_time_tm, NULL) > 0)
213 mtext_cat_char (thisline, '\n');
214 mtext_cat (mt, thisline);
216 m17n_object_unref (thisline);
218 plist = mplist_next (plist);
221 /* Show the generated M-text by another example program "mview". */
223 FILE *p = popen ("m17n-view", "w");
226 FATAL_ERROR ("%s\n", "Can't run the program mview!");
227 mconv_encode_stream (Mcoding_utf_8, mt, p);
232 m17n_object_unref (locale_list);
233 m17n_object_unref (mt);
238 #endif /* not FOR_DOXYGEN */