timelocal.c revision 267798
1150022Srwatson/*-
2150022Srwatson * Copyright (c) 2001 Alexey Zelkin <phantom@FreeBSD.org>
3150022Srwatson * Copyright (c) 1997 FreeBSD Inc.
4150022Srwatson * All rights reserved.
5150022Srwatson *
6150022Srwatson * Copyright (c) 2011 The FreeBSD Foundation
7150022Srwatson * All rights reserved.
8150022Srwatson * Portions of this software were developed by David Chisnall
9150022Srwatson * under sponsorship from the FreeBSD Foundation.
10150022Srwatson *
11150022Srwatson * Redistribution and use in source and binary forms, with or without
12150022Srwatson * modification, are permitted provided that the following conditions
13150022Srwatson * are met:
14150022Srwatson * 1. Redistributions of source code must retain the above copyright
15150022Srwatson *    notice, this list of conditions and the following disclaimer.
16150022Srwatson * 2. Redistributions in binary form must reproduce the above copyright
17150022Srwatson *    notice, this list of conditions and the following disclaimer in the
18150022Srwatson *    documentation and/or other materials provided with the distribution.
19150022Srwatson *
20150022Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21150022Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22150022Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23150022Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24150022Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25150022Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26150022Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27150022Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28150022Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29150059Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30150059Srwatson * SUCH DAMAGE.
31150091Srwatson */
32150022Srwatson
33150022Srwatson#include <sys/cdefs.h>
34150059Srwatson__FBSDID("$FreeBSD: stable/10/lib/libc/stdtime/timelocal.c 267798 2014-06-23 15:03:51Z pfg $");
35150022Srwatson
36150022Srwatson#include <stddef.h>
37150022Srwatson
38150022Srwatson#include "ldpart.h"
39150022Srwatson#include "timelocal.h"
40150022Srwatson
41150022Srwatsonstruct xlocale_time {
42150022Srwatson	struct xlocale_component header;
43150022Srwatson	char *buffer;
44150022Srwatson	struct lc_time_T locale;
45150022Srwatson};
46150022Srwatson
47150022Srwatsonstruct xlocale_time __xlocale_global_time;
48150022Srwatson
49150022Srwatson#define	LCTIME_SIZE (sizeof(struct lc_time_T) / sizeof(char *))
50150022Srwatson
51150022Srwatsonstatic const struct lc_time_T	_C_time_locale = {
52150022Srwatson	{
53150022Srwatson		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
54150022Srwatson		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
55150022Srwatson	}, {
56150022Srwatson		"January", "February", "March", "April", "May", "June",
57150022Srwatson		"July", "August", "September", "October", "November", "December"
58150022Srwatson	}, {
59150022Srwatson		"Sun", "Mon", "Tue", "Wed",
60150022Srwatson		"Thu", "Fri", "Sat"
61150022Srwatson	}, {
62150022Srwatson		"Sunday", "Monday", "Tuesday", "Wednesday",
63150022Srwatson		"Thursday", "Friday", "Saturday"
64150022Srwatson	},
65150022Srwatson
66150022Srwatson	/* X_fmt */
67150022Srwatson	"%H:%M:%S",
68150022Srwatson
69150022Srwatson	/*
70150091Srwatson	 * x_fmt
71150091Srwatson	 * Since the C language standard calls for
72150022Srwatson	 * "date, using locale's date format," anything goes.
73150091Srwatson	 * Using just numbers (as here) makes Quakers happier;
74150091Srwatson	 * it's also compatible with SVR4.
75150022Srwatson	 */
76150022Srwatson	"%m/%d/%y",
77150022Srwatson
78150091Srwatson	/*
79150093Srwatson	 * c_fmt
80150093Srwatson	 */
81150093Srwatson	"%a %b %e %H:%M:%S %Y",
82150093Srwatson
83150022Srwatson	/* am */
84150022Srwatson	"AM",
85150093Srwatson
86150093Srwatson	/* pm */
87150022Srwatson	"PM",
88150022Srwatson
89150022Srwatson	/* date_fmt */
90150022Srwatson	"%a %b %e %H:%M:%S %Z %Y",
91281450Sngie
92150022Srwatson	/* alt_month
93150022Srwatson	 * Standalone months forms for %OB
94150022Srwatson	 */
95150022Srwatson	{
96150022Srwatson		"January", "February", "March", "April", "May", "June",
97150022Srwatson		"July", "August", "September", "October", "November", "December"
98150022Srwatson	},
99150022Srwatson
100150022Srwatson	/* md_order
101150022Srwatson	 * Month / day order in dates
102150022Srwatson	 */
103150022Srwatson	"md",
104150022Srwatson
105150022Srwatson	/* ampm_fmt
106150022Srwatson	 * To determine 12-hour clock format time (empty, if N/A)
107150022Srwatson	 */
108150022Srwatson	"%I:%M:%S %p"
109150093Srwatson};
110150022Srwatson
111150022Srwatsonstatic void destruct_time(void *v)
112150022Srwatson{
113150022Srwatson	struct xlocale_time *l = v;
114150022Srwatson	if (l->buffer)
115150022Srwatson		free(l->buffer);
116150022Srwatson	free(l);
117150022Srwatson}
118150022Srwatson
119150059Srwatson#include <stdio.h>
120150093Srwatsonstruct lc_time_T *
121150059Srwatson__get_current_time_locale(locale_t loc)
122150059Srwatson{
123150059Srwatson	return (loc->using_time_locale
124150059Srwatson		? &((struct xlocale_time *)loc->components[XLC_TIME])->locale
125150093Srwatson		: (struct lc_time_T *)&_C_time_locale);
126150059Srwatson}
127150059Srwatson
128150093Srwatsonstatic int
129150093Srwatsontime_load_locale(struct xlocale_time *l, int *using_locale, const char *name)
130150093Srwatson{
131150093Srwatson	struct lc_time_T *time_locale = &l->locale;
132150022Srwatson	return (__part_load_locale(name, using_locale,
133281450Sngie			&l->buffer, "LC_TIME",
134150022Srwatson			LCTIME_SIZE, LCTIME_SIZE,
135150022Srwatson			(const char **)time_locale));
136150022Srwatson}
137150022Srwatsonint
138150022Srwatson__time_load_locale(const char *name)
139150022Srwatson{
140150022Srwatson	return time_load_locale(&__xlocale_global_time,
141150022Srwatson			&__xlocale_global_locale.using_time_locale, name);
142150022Srwatson}
143150022Srwatsonvoid* __time_load(const char* name, locale_t loc)
144150022Srwatson{
145150022Srwatson	struct xlocale_time *new = calloc(sizeof(struct xlocale_time), 1);
146150022Srwatson	new->header.header.destructor = destruct_time;
147150022Srwatson	if (time_load_locale(new, &loc->using_time_locale, name) == _LDP_ERROR)
148150022Srwatson	{
149150022Srwatson		xlocale_release(new);
150150022Srwatson		return NULL;
151150022Srwatson	}
152150022Srwatson	return new;
153150093Srwatson}
154150093Srwatson
155150093Srwatson