xref: /dflybsd-src/lib/libc/stdtime/timelocal.c (revision 8d7b8f975bdf97f45f67855432659d278147bfbf)
1*0d5acd74SJohn Marino /*-
2*0d5acd74SJohn Marino  * Copyright (c) 2001 Alexey Zelkin <phantom@FreeBSD.org>
3*0d5acd74SJohn Marino  * Copyright (c) 1997 FreeBSD Inc.
4*0d5acd74SJohn Marino  * All rights reserved.
5*0d5acd74SJohn Marino  *
6*0d5acd74SJohn Marino  * Copyright (c) 2011 The FreeBSD Foundation
7*0d5acd74SJohn Marino  * All rights reserved.
8*0d5acd74SJohn Marino  * Portions of this software were developed by David Chisnall
9*0d5acd74SJohn Marino  * under sponsorship from the FreeBSD Foundation.
10*0d5acd74SJohn Marino  *
11*0d5acd74SJohn Marino  * Redistribution and use in source and binary forms, with or without
12*0d5acd74SJohn Marino  * modification, are permitted provided that the following conditions
13*0d5acd74SJohn Marino  * are met:
14*0d5acd74SJohn Marino  * 1. Redistributions of source code must retain the above copyright
15*0d5acd74SJohn Marino  *    notice, this list of conditions and the following disclaimer.
16*0d5acd74SJohn Marino  * 2. Redistributions in binary form must reproduce the above copyright
17*0d5acd74SJohn Marino  *    notice, this list of conditions and the following disclaimer in the
18*0d5acd74SJohn Marino  *    documentation and/or other materials provided with the distribution.
19*0d5acd74SJohn Marino  *
20*0d5acd74SJohn Marino  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21*0d5acd74SJohn Marino  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22*0d5acd74SJohn Marino  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23*0d5acd74SJohn Marino  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24*0d5acd74SJohn Marino  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25*0d5acd74SJohn Marino  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26*0d5acd74SJohn Marino  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27*0d5acd74SJohn Marino  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28*0d5acd74SJohn Marino  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29*0d5acd74SJohn Marino  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30*0d5acd74SJohn Marino  * SUCH DAMAGE.
31*0d5acd74SJohn Marino  *
32*0d5acd74SJohn Marino  * $FreeBSD: head/lib/libc/stdtime/timelocal.c 227753 2011-11-20 14:45:42Z theraven $
33*0d5acd74SJohn Marino  */
34*0d5acd74SJohn Marino 
35*0d5acd74SJohn Marino 
36*0d5acd74SJohn Marino #include <stddef.h>
37*0d5acd74SJohn Marino 
38*0d5acd74SJohn Marino #include "ldpart.h"
39*0d5acd74SJohn Marino #include "timelocal.h"
40*0d5acd74SJohn Marino 
41*0d5acd74SJohn Marino struct xlocale_time {
42*0d5acd74SJohn Marino 	struct xlocale_component header;
43*0d5acd74SJohn Marino 	char *buffer;
44*0d5acd74SJohn Marino 	struct lc_time_T locale;
45*0d5acd74SJohn Marino };
46*0d5acd74SJohn Marino 
47*0d5acd74SJohn Marino struct xlocale_time __xlocale_global_time;
48*0d5acd74SJohn Marino 
49*0d5acd74SJohn Marino #define	LCTIME_SIZE (sizeof(struct lc_time_T) / sizeof(char *))
50*0d5acd74SJohn Marino 
51*0d5acd74SJohn Marino static const struct lc_time_T	_C_time_locale = {
52*0d5acd74SJohn Marino 	{
53*0d5acd74SJohn Marino 		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
54*0d5acd74SJohn Marino 		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
55*0d5acd74SJohn Marino 	}, {
56*0d5acd74SJohn Marino 		"January", "February", "March", "April", "May", "June",
57*0d5acd74SJohn Marino 		"July", "August", "September", "October", "November", "December"
58*0d5acd74SJohn Marino 	}, {
59*0d5acd74SJohn Marino 		"Sun", "Mon", "Tue", "Wed",
60*0d5acd74SJohn Marino 		"Thu", "Fri", "Sat"
61*0d5acd74SJohn Marino 	}, {
62*0d5acd74SJohn Marino 		"Sunday", "Monday", "Tuesday", "Wednesday",
63*0d5acd74SJohn Marino 		"Thursday", "Friday", "Saturday"
64*0d5acd74SJohn Marino 	},
65*0d5acd74SJohn Marino 
66*0d5acd74SJohn Marino 	/* X_fmt */
67*0d5acd74SJohn Marino 	"%H:%M:%S",
68*0d5acd74SJohn Marino 
69*0d5acd74SJohn Marino 	/*
70*0d5acd74SJohn Marino 	 * x_fmt
71*0d5acd74SJohn Marino 	 * Since the C language standard calls for
72*0d5acd74SJohn Marino 	 * "date, using locale's date format," anything goes.
73*0d5acd74SJohn Marino 	 * Using just numbers (as here) makes Quakers happier;
74*0d5acd74SJohn Marino 	 * it's also compatible with SVR4.
75*0d5acd74SJohn Marino 	 */
76*0d5acd74SJohn Marino 	"%m/%d/%y",
77*0d5acd74SJohn Marino 
78*0d5acd74SJohn Marino 	/*
79*0d5acd74SJohn Marino 	 * c_fmt
80*0d5acd74SJohn Marino 	 */
81*0d5acd74SJohn Marino 	"%a %b %e %H:%M:%S %Y",
82*0d5acd74SJohn Marino 
83*0d5acd74SJohn Marino 	/* am */
84*0d5acd74SJohn Marino 	"AM",
85*0d5acd74SJohn Marino 
86*0d5acd74SJohn Marino 	/* pm */
87*0d5acd74SJohn Marino 	"PM",
88*0d5acd74SJohn Marino 
89*0d5acd74SJohn Marino 	/* date_fmt */
90*0d5acd74SJohn Marino 	"%a %b %e %H:%M:%S %Z %Y",
91*0d5acd74SJohn Marino 
92*0d5acd74SJohn Marino 	/* alt_month
93*0d5acd74SJohn Marino 	 * Standalone months forms for %OB
94*0d5acd74SJohn Marino 	 */
95*0d5acd74SJohn Marino 	{
96*0d5acd74SJohn Marino 		"January", "February", "March", "April", "May", "June",
97*0d5acd74SJohn Marino 		"July", "August", "September", "October", "November", "December"
98*0d5acd74SJohn Marino 	},
99*0d5acd74SJohn Marino 
100*0d5acd74SJohn Marino 	/* md_order
101*0d5acd74SJohn Marino 	 * Month / day order in dates
102*0d5acd74SJohn Marino 	 */
103*0d5acd74SJohn Marino 	"md",
104*0d5acd74SJohn Marino 
105*0d5acd74SJohn Marino 	/* ampm_fmt
106*0d5acd74SJohn Marino 	 * To determine 12-hour clock format time (empty, if N/A)
107*0d5acd74SJohn Marino 	 */
108*0d5acd74SJohn Marino 	"%I:%M:%S %p"
109*0d5acd74SJohn Marino };
110*0d5acd74SJohn Marino 
destruct_time(void * v)111*0d5acd74SJohn Marino static void destruct_time(void *v)
112*0d5acd74SJohn Marino {
113*0d5acd74SJohn Marino 	struct xlocale_time *l = v;
114*0d5acd74SJohn Marino 	if (l->buffer)
115*0d5acd74SJohn Marino 		free(l->buffer);
116*0d5acd74SJohn Marino 	free(l);
117*0d5acd74SJohn Marino }
118*0d5acd74SJohn Marino 
119*0d5acd74SJohn Marino #include <stdio.h>
120*0d5acd74SJohn Marino struct lc_time_T *
__get_current_time_locale(locale_t loc)121*0d5acd74SJohn Marino __get_current_time_locale(locale_t loc)
122*0d5acd74SJohn Marino {
123*0d5acd74SJohn Marino 	return (loc->using_time_locale
124*0d5acd74SJohn Marino 		? &((struct xlocale_time *)loc->components[XLC_TIME])->locale
125*0d5acd74SJohn Marino 		: (struct lc_time_T *)&_C_time_locale);
126*0d5acd74SJohn Marino }
127*0d5acd74SJohn Marino 
128*0d5acd74SJohn Marino static int
time_load_locale(struct xlocale_time * l,int * using_locale,const char * name)129*0d5acd74SJohn Marino time_load_locale(struct xlocale_time *l, int *using_locale, const char *name)
130*0d5acd74SJohn Marino {
131*0d5acd74SJohn Marino 	struct lc_time_T *time_locale = &l->locale;
132*0d5acd74SJohn Marino 	return (__part_load_locale(name, using_locale,
133*0d5acd74SJohn Marino 			&l->buffer, "LC_TIME",
134*0d5acd74SJohn Marino 			LCTIME_SIZE, LCTIME_SIZE,
135*0d5acd74SJohn Marino 			(const char **)time_locale));
136*0d5acd74SJohn Marino }
137*0d5acd74SJohn Marino int
__time_load_locale(const char * name)138*0d5acd74SJohn Marino __time_load_locale(const char *name)
139*0d5acd74SJohn Marino {
140*0d5acd74SJohn Marino 	return time_load_locale(&__xlocale_global_time,
141*0d5acd74SJohn Marino 			&__xlocale_global_locale.using_time_locale, name);
142*0d5acd74SJohn Marino }
__time_load(const char * name,locale_t loc)143*0d5acd74SJohn Marino void* __time_load(const char* name, locale_t loc)
144*0d5acd74SJohn Marino {
145*0d5acd74SJohn Marino 	struct xlocale_time *new = calloc(sizeof(struct xlocale_time), 1);
146*0d5acd74SJohn Marino 	new->header.header.destructor = destruct_time;
147*0d5acd74SJohn Marino 	if (time_load_locale(new, &loc->using_time_locale, name) == _LDP_ERROR)
148*0d5acd74SJohn Marino 	{
149*0d5acd74SJohn Marino 		xlocale_release(new);
150*0d5acd74SJohn Marino 		return NULL;
151*0d5acd74SJohn Marino 	}
152*0d5acd74SJohn Marino 	return new;
153*0d5acd74SJohn Marino }
154*0d5acd74SJohn Marino 
155