timelocal.c revision 72406
1/*-
2 * Copyright (c) 2001 Alexey Zelkin
3 * Copyright (c) 1997 FreeBSD Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * $FreeBSD: head/lib/libc/stdtime/timelocal.c 72406 2001-02-12 08:53:33Z phantom $
28 */
29
30#include "ldpart.h"
31#include "timelocal.h"
32
33static struct lc_time_T _time_locale;
34static int _time_using_locale;
35static char * time_locale_buf;
36
37#define	LCTIME_SIZE (sizeof(struct lc_time_T) / sizeof(char *))
38
39static const struct lc_time_T	_C_time_locale = {
40	{
41		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
42		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
43	}, {
44		"January", "February", "March", "April", "May", "June",
45		"July", "August", "September", "October", "November", "December"
46	}, {
47		"Sun", "Mon", "Tue", "Wed",
48		"Thu", "Fri", "Sat"
49	}, {
50		"Sunday", "Monday", "Tuesday", "Wednesday",
51		"Thursday", "Friday", "Saturday"
52	},
53
54	/* X_fmt */
55	"%H:%M:%S",
56
57	/*
58	** x_fmt
59	** Since the C language standard calls for
60	** "date, using locale's date format," anything goes.
61	** Using just numbers (as here) makes Quakers happier;
62	** it's also compatible with SVR4.
63	*/
64	"%m/%d/%y",
65
66	/*
67	** c_fmt (ctime-compatible)
68	*/
69	"%a %Ef %T %Y",
70
71	/* am */
72	"AM",
73
74	/* pm */
75	"PM",
76
77	/* date_fmt */
78	"%a %Ef %X %Z %Y",
79
80	{
81		"January", "February", "March", "April", "May", "June",
82		"July", "August", "September", "October", "November", "December"
83	},
84
85	/* Ef_fmt
86	** To determine short months / day order
87	*/
88	"%b %e",
89
90	/* EF_fmt
91	** To determine long months / day order
92	*/
93	"%B %e"
94};
95
96struct lc_time_T *
97__get_current_time_locale(void) {
98	return (_time_using_locale
99		? &_time_locale
100		: (struct lc_time_T *)&_C_time_locale);
101}
102
103int
104__time_load_locale(const char *name) {
105
106	int	ret;
107
108	ret = __part_load_locale(name, &_time_using_locale,
109			time_locale_buf, "LC_TIME", LCTIME_SIZE,
110			(const char **)&_time_locale);
111
112	/* XXX: always overwrite for ctime format parsing compatibility */
113	if (ret == 0 && _time_using_locale)
114		_time_locale.c_fmt = _C_time_locale.c_fmt;
115	return (ret);
116}
117