128021Sjoerg/*-
287659Sphantom * Copyright (c) 2001 Alexey Zelkin <phantom@FreeBSD.org>
328021Sjoerg * Copyright (c) 1997 FreeBSD Inc.
428021Sjoerg * All rights reserved.
528021Sjoerg *
6235785Stheraven * Copyright (c) 2011 The FreeBSD Foundation
7235785Stheraven * All rights reserved.
8235785Stheraven * Portions of this software were developed by David Chisnall
9235785Stheraven * under sponsorship from the FreeBSD Foundation.
10235785Stheraven *
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
41235785Stheravenstruct xlocale_time {
42235785Stheraven	struct xlocale_component header;
43235785Stheraven	char *buffer;
44235785Stheraven	struct lc_time_T locale;
45235785Stheraven};
4628021Sjoerg
47235785Stheravenstruct xlocale_time __xlocale_global_time;
48235785Stheraven
4974412Sache#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
111235785Stheravenstatic void destruct_time(void *v)
112235785Stheraven{
113235785Stheraven	struct xlocale_time *l = v;
114235785Stheraven	if (l->buffer)
115235785Stheraven		free(l->buffer);
116235785Stheraven	free(l);
117235785Stheraven}
118235785Stheraven
119235785Stheraven#include <stdio.h>
12072167Sphantomstruct lc_time_T *
121235785Stheraven__get_current_time_locale(locale_t loc)
122101471Sache{
123235785Stheraven	return (loc->using_time_locale
124235785Stheraven		? &((struct xlocale_time *)loc->components[XLC_TIME])->locale
12572167Sphantom		: (struct lc_time_T *)&_C_time_locale);
12672167Sphantom}
12728021Sjoerg
128235785Stheravenstatic int
129235785Stheraventime_load_locale(struct xlocale_time *l, int *using_locale, const char *name)
130235785Stheraven{
131235785Stheraven	struct lc_time_T *time_locale = &l->locale;
132235785Stheraven	return (__part_load_locale(name, using_locale,
133235785Stheraven			&l->buffer, "LC_TIME",
134235785Stheraven			LCTIME_SIZE, LCTIME_SIZE,
135235785Stheraven			(const char **)time_locale));
136235785Stheraven}
13728021Sjoergint
138101471Sache__time_load_locale(const char *name)
139101471Sache{
140235785Stheraven	return time_load_locale(&__xlocale_global_time,
141235785Stheraven			&__xlocale_global_locale.using_time_locale, name);
14251186Sdt}
143235785Stheravenvoid* __time_load(const char* name, locale_t loc)
144235785Stheraven{
145235785Stheraven	struct xlocale_time *new = calloc(sizeof(struct xlocale_time), 1);
146235785Stheraven	new->header.header.destructor = destruct_time;
147235785Stheraven	if (time_load_locale(new, &loc->using_time_locale, name) == _LDP_ERROR)
148235785Stheraven	{
149235785Stheraven		xlocale_release(new);
150235785Stheraven		return NULL;
151235785Stheraven	}
152235785Stheraven	return new;
153235785Stheraven}
154235785Stheraven
155