128021Sjoerg/*-
287659Sphantom * Copyright (c) 2001 Alexey Zelkin <phantom@FreeBSD.org>
328021Sjoerg * Copyright (c) 1997 FreeBSD Inc.
428021Sjoerg * All rights reserved.
528021Sjoerg *
6227753Stheraven * Copyright (c) 2011 The FreeBSD Foundation
7227753Stheraven * All rights reserved.
8227753Stheraven * Portions of this software were developed by David Chisnall
9227753Stheraven * under sponsorship from the FreeBSD Foundation.
10227753Stheraven *
1128021Sjoerg * Redistribution and use in source and binary forms, with or without
1228021Sjoerg * modification, are permitted provided that the following conditions
1328021Sjoerg * are met:
1428021Sjoerg * 1. Redistributions of source code must retain the above copyright
1528021Sjoerg *    notice, this list of conditions and the following disclaimer.
1628021Sjoerg * 2. Redistributions in binary form must reproduce the above copyright
1728021Sjoerg *    notice, this list of conditions and the following disclaimer in the
1828021Sjoerg *    documentation and/or other materials provided with the distribution.
1928021Sjoerg *
2028021Sjoerg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2128021Sjoerg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2228021Sjoerg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2328021Sjoerg * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2428021Sjoerg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2528021Sjoerg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2628021Sjoerg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2728021Sjoerg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2828021Sjoerg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2928021Sjoerg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3028021Sjoerg * SUCH DAMAGE.
3128021Sjoerg */
3228021Sjoerg
3392986Sobrien#include <sys/cdefs.h>
3492986Sobrien__FBSDID("$FreeBSD$");
3592986Sobrien
3673359Sache#include <stddef.h>
3773359Sache
3872406Sphantom#include "ldpart.h"
3928021Sjoerg#include "timelocal.h"
4028021Sjoerg
41227753Stheravenstruct xlocale_time {
42227753Stheraven	struct xlocale_component header;
43227753Stheraven	char *buffer;
44227753Stheraven	struct lc_time_T locale;
45227753Stheraven};
4628021Sjoerg
47227753Stheravenstruct xlocale_time __xlocale_global_time;
48227753Stheraven
49267798Spfg#define	LCTIME_SIZE (sizeof(struct lc_time_T) / sizeof(char *))
5051186Sdt
5172167Sphantomstatic const struct lc_time_T	_C_time_locale = {
5228021Sjoerg	{
5328021Sjoerg		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
5428021Sjoerg		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
5528021Sjoerg	}, {
5628021Sjoerg		"January", "February", "March", "April", "May", "June",
5728021Sjoerg		"July", "August", "September", "October", "November", "December"
5828021Sjoerg	}, {
5928021Sjoerg		"Sun", "Mon", "Tue", "Wed",
6028021Sjoerg		"Thu", "Fri", "Sat"
6128021Sjoerg	}, {
6228021Sjoerg		"Sunday", "Monday", "Tuesday", "Wednesday",
6328021Sjoerg		"Thursday", "Friday", "Saturday"
6428021Sjoerg	},
6528021Sjoerg
6628021Sjoerg	/* X_fmt */
6728021Sjoerg	"%H:%M:%S",
6828021Sjoerg
6928021Sjoerg	/*
7089736Sphantom	 * x_fmt
7189736Sphantom	 * Since the C language standard calls for
7289736Sphantom	 * "date, using locale's date format," anything goes.
7389736Sphantom	 * Using just numbers (as here) makes Quakers happier;
7489736Sphantom	 * it's also compatible with SVR4.
7589736Sphantom	 */
7674572Sache	"%m/%d/%y",
7728021Sjoerg
7828021Sjoerg	/*
7989736Sphantom	 * c_fmt
8089736Sphantom	 */
8174572Sache	"%a %b %e %H:%M:%S %Y",
8228021Sjoerg
8328021Sjoerg	/* am */
8428021Sjoerg	"AM",
8528021Sjoerg
8628021Sjoerg	/* pm */
8728021Sjoerg	"PM",
8828021Sjoerg
8928021Sjoerg	/* date_fmt */
9074572Sache	"%a %b %e %H:%M:%S %Z %Y",
9151186Sdt
9274412Sache	/* alt_month
9389736Sphantom	 * Standalone months forms for %OB
9489736Sphantom	 */
9551186Sdt	{
9651186Sdt		"January", "February", "March", "April", "May", "June",
9751186Sdt		"July", "August", "September", "October", "November", "December"
9853940Sache	},
9953940Sache
10074412Sache	/* md_order
10189736Sphantom	 * Month / day order in dates
10289736Sphantom	 */
10374412Sache	"md",
10453960Sache
10573359Sache	/* ampm_fmt
10689736Sphantom	 * To determine 12-hour clock format time (empty, if N/A)
10789736Sphantom	 */
10873359Sache	"%I:%M:%S %p"
10928021Sjoerg};
11028021Sjoerg
111227753Stheravenstatic void destruct_time(void *v)
112227753Stheraven{
113227753Stheraven	struct xlocale_time *l = v;
114227753Stheraven	if (l->buffer)
115227753Stheraven		free(l->buffer);
116227753Stheraven	free(l);
117227753Stheraven}
118227753Stheraven
119227753Stheraven#include <stdio.h>
12072167Sphantomstruct lc_time_T *
121227753Stheraven__get_current_time_locale(locale_t loc)
122101471Sache{
123227753Stheraven	return (loc->using_time_locale
124227753Stheraven		? &((struct xlocale_time *)loc->components[XLC_TIME])->locale
12572167Sphantom		: (struct lc_time_T *)&_C_time_locale);
12672167Sphantom}
12728021Sjoerg
128227753Stheravenstatic int
129227753Stheraventime_load_locale(struct xlocale_time *l, int *using_locale, const char *name)
130227753Stheraven{
131227753Stheraven	struct lc_time_T *time_locale = &l->locale;
132227753Stheraven	return (__part_load_locale(name, using_locale,
133227753Stheraven			&l->buffer, "LC_TIME",
134227753Stheraven			LCTIME_SIZE, LCTIME_SIZE,
135227753Stheraven			(const char **)time_locale));
136227753Stheraven}
13728021Sjoergint
138101471Sache__time_load_locale(const char *name)
139101471Sache{
140227753Stheraven	return time_load_locale(&__xlocale_global_time,
141227753Stheraven			&__xlocale_global_locale.using_time_locale, name);
14251186Sdt}
143227753Stheravenvoid* __time_load(const char* name, locale_t loc)
144227753Stheraven{
145227753Stheraven	struct xlocale_time *new = calloc(sizeof(struct xlocale_time), 1);
146227753Stheraven	new->header.header.destructor = destruct_time;
147227753Stheraven	if (time_load_locale(new, &loc->using_time_locale, name) == _LDP_ERROR)
148227753Stheraven	{
149227753Stheraven		xlocale_release(new);
150227753Stheraven		return NULL;
151227753Stheraven	}
152227753Stheraven	return new;
153227753Stheraven}
154227753Stheraven
155