xref: /freebsd/lib/libc/stdtime/timelocal.c (revision 50bab1e6fba3d997c386e88c13e6d8e3c1a53a81)
137486f03SJoerg Wunsch /*-
24e862380SAlexey Zelkin  * Copyright (c) 2001 Alexey Zelkin
337486f03SJoerg Wunsch  * Copyright (c) 1997 FreeBSD Inc.
437486f03SJoerg Wunsch  * All rights reserved.
537486f03SJoerg Wunsch  *
637486f03SJoerg Wunsch  * Redistribution and use in source and binary forms, with or without
737486f03SJoerg Wunsch  * modification, are permitted provided that the following conditions
837486f03SJoerg Wunsch  * are met:
937486f03SJoerg Wunsch  * 1. Redistributions of source code must retain the above copyright
1037486f03SJoerg Wunsch  *    notice, this list of conditions and the following disclaimer.
1137486f03SJoerg Wunsch  * 2. Redistributions in binary form must reproduce the above copyright
1237486f03SJoerg Wunsch  *    notice, this list of conditions and the following disclaimer in the
1337486f03SJoerg Wunsch  *    documentation and/or other materials provided with the distribution.
1437486f03SJoerg Wunsch  *
1537486f03SJoerg Wunsch  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1637486f03SJoerg Wunsch  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1737486f03SJoerg Wunsch  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1837486f03SJoerg Wunsch  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1937486f03SJoerg Wunsch  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2037486f03SJoerg Wunsch  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2137486f03SJoerg Wunsch  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2237486f03SJoerg Wunsch  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2337486f03SJoerg Wunsch  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2437486f03SJoerg Wunsch  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2537486f03SJoerg Wunsch  * SUCH DAMAGE.
2637486f03SJoerg Wunsch  *
277f3dea24SPeter Wemm  * $FreeBSD$
2837486f03SJoerg Wunsch  */
2937486f03SJoerg Wunsch 
3050bab1e6SAndrey A. Chernov #include <stddef.h>
3150bab1e6SAndrey A. Chernov 
324e862380SAlexey Zelkin #include "ldpart.h"
3337486f03SJoerg Wunsch #include "timelocal.h"
3437486f03SJoerg Wunsch 
354e862380SAlexey Zelkin static struct lc_time_T _time_locale;
3618f3e1e4SAlexey Zelkin static int _time_using_locale;
374e862380SAlexey Zelkin static char * time_locale_buf;
3837486f03SJoerg Wunsch 
3950bab1e6SAndrey A. Chernov #define LCTIME_SIZE_FULL (sizeof(struct lc_time_T) / sizeof(char *))
4050bab1e6SAndrey A. Chernov #define LCTIME_SIZE_MIN \
4150bab1e6SAndrey A. Chernov 		(offsetof(struct lc_time_T, ampm_fmt) / sizeof(char *))
42da3785efSDmitrij Tejblum 
4318f3e1e4SAlexey Zelkin static const struct lc_time_T	_C_time_locale = {
4437486f03SJoerg Wunsch 	{
4537486f03SJoerg Wunsch 		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4637486f03SJoerg Wunsch 		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
4737486f03SJoerg Wunsch 	}, {
4837486f03SJoerg Wunsch 		"January", "February", "March", "April", "May", "June",
4937486f03SJoerg Wunsch 		"July", "August", "September", "October", "November", "December"
5037486f03SJoerg Wunsch 	}, {
5137486f03SJoerg Wunsch 		"Sun", "Mon", "Tue", "Wed",
5237486f03SJoerg Wunsch 		"Thu", "Fri", "Sat"
5337486f03SJoerg Wunsch 	}, {
5437486f03SJoerg Wunsch 		"Sunday", "Monday", "Tuesday", "Wednesday",
5537486f03SJoerg Wunsch 		"Thursday", "Friday", "Saturday"
5637486f03SJoerg Wunsch 	},
5737486f03SJoerg Wunsch 
5837486f03SJoerg Wunsch 	/* X_fmt */
5937486f03SJoerg Wunsch 	"%H:%M:%S",
6037486f03SJoerg Wunsch 
6137486f03SJoerg Wunsch 	/*
6237486f03SJoerg Wunsch 	** x_fmt
6337486f03SJoerg Wunsch 	** Since the C language standard calls for
6437486f03SJoerg Wunsch 	** "date, using locale's date format," anything goes.
6537486f03SJoerg Wunsch 	** Using just numbers (as here) makes Quakers happier;
6637486f03SJoerg Wunsch 	** it's also compatible with SVR4.
6737486f03SJoerg Wunsch 	*/
6837486f03SJoerg Wunsch 	"%m/%d/%y",
6937486f03SJoerg Wunsch 
7037486f03SJoerg Wunsch 	/*
7137486f03SJoerg Wunsch 	** c_fmt (ctime-compatible)
7237486f03SJoerg Wunsch 	*/
73740972dcSAndrey A. Chernov 	"%a %Ef %T %Y",
7437486f03SJoerg Wunsch 
7537486f03SJoerg Wunsch 	/* am */
7637486f03SJoerg Wunsch 	"AM",
7737486f03SJoerg Wunsch 
7837486f03SJoerg Wunsch 	/* pm */
7937486f03SJoerg Wunsch 	"PM",
8037486f03SJoerg Wunsch 
8137486f03SJoerg Wunsch 	/* date_fmt */
82c63a4303SAndrey A. Chernov 	"%a %Ef %X %Z %Y",
83da3785efSDmitrij Tejblum 
84da3785efSDmitrij Tejblum 	{
85da3785efSDmitrij Tejblum 		"January", "February", "March", "April", "May", "June",
86da3785efSDmitrij Tejblum 		"July", "August", "September", "October", "November", "December"
8711cd0d32SAndrey A. Chernov 	},
8811cd0d32SAndrey A. Chernov 
89c63a4303SAndrey A. Chernov 	/* Ef_fmt
90c63a4303SAndrey A. Chernov 	** To determine short months / day order
9111cd0d32SAndrey A. Chernov 	*/
92c63a4303SAndrey A. Chernov 	"%b %e",
93c63a4303SAndrey A. Chernov 
94c63a4303SAndrey A. Chernov 	/* EF_fmt
95c63a4303SAndrey A. Chernov 	** To determine long months / day order
96c63a4303SAndrey A. Chernov 	*/
9750bab1e6SAndrey A. Chernov 	"%B %e",
9850bab1e6SAndrey A. Chernov 
9950bab1e6SAndrey A. Chernov 	/* ampm_fmt
10050bab1e6SAndrey A. Chernov 	** To determine 12-hour clock format time (empty, if N/A)
10150bab1e6SAndrey A. Chernov 	*/
10250bab1e6SAndrey A. Chernov 	"%I:%M:%S %p"
10337486f03SJoerg Wunsch };
10437486f03SJoerg Wunsch 
10518f3e1e4SAlexey Zelkin struct lc_time_T *
10618f3e1e4SAlexey Zelkin __get_current_time_locale(void) {
10718f3e1e4SAlexey Zelkin 	return (_time_using_locale
1084e862380SAlexey Zelkin 		? &_time_locale
10918f3e1e4SAlexey Zelkin 		: (struct lc_time_T *)&_C_time_locale);
11018f3e1e4SAlexey Zelkin }
11137486f03SJoerg Wunsch 
11237486f03SJoerg Wunsch int
1134e862380SAlexey Zelkin __time_load_locale(const char *name) {
11437486f03SJoerg Wunsch 
1154e862380SAlexey Zelkin 	int	ret;
11637486f03SJoerg Wunsch 
11750bab1e6SAndrey A. Chernov 	_time_locale.ampm_fmt = _C_time_locale.ampm_fmt;
11850bab1e6SAndrey A. Chernov 
1194e862380SAlexey Zelkin 	ret = __part_load_locale(name, &_time_using_locale,
12050bab1e6SAndrey A. Chernov 			time_locale_buf, "LC_TIME",
12150bab1e6SAndrey A. Chernov 			LCTIME_SIZE_FULL, LCTIME_SIZE_MIN,
1224e862380SAlexey Zelkin 			(const char **)&_time_locale);
12337486f03SJoerg Wunsch 
124740972dcSAndrey A. Chernov 	/* XXX: always overwrite for ctime format parsing compatibility */
1254e862380SAlexey Zelkin 	_time_locale.c_fmt = _C_time_locale.c_fmt;
12650bab1e6SAndrey A. Chernov 
1274e862380SAlexey Zelkin 	return (ret);
128da3785efSDmitrij Tejblum }
129