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