]> sourceware.org Git - glibc.git/blob - locale/lc-time.c
Update.
[glibc.git] / locale / lc-time.c
1 /* Define current locale data for LC_TIME category.
2 Copyright (C) 1995, 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
19
20 #include <bits/libc-lock.h>
21 #include <endian.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <time.h>
25 #include <wchar.h>
26 #include "localeinfo.h"
27
28 _NL_CURRENT_DEFINE (LC_TIME);
29
30 /* Some of the functions here must not be used while setlocale is called. */
31 __libc_lock_define (extern, __libc_setlocale_lock)
32
33
34 static int era_initialized;
35 static struct era_entry **eras;
36 static const void **eras_nf;
37 static size_t num_eras;
38
39 #define ERAS_NF(cnt, category) \
40 *(eras_nf + ERA_NAME_FORMAT_MEMBERS * (cnt) + (category))
41
42 static int alt_digits_initialized;
43 static const char **alt_digits;
44
45
46 static int walt_digits_initialized;
47 static const wchar_t **walt_digits;
48
49
50 void
51 _nl_postload_time (void)
52 {
53 /* Prepare lazy initialization of `era' and `alt_digits' array. */
54 era_initialized = 0;
55 alt_digits_initialized = 0;
56 walt_digits_initialized = 0;
57 }
58
59
60 static void
61 init_era_entry (void)
62 {
63 size_t cnt;
64
65 if (era_initialized == 0)
66 {
67 size_t new_num_eras = _NL_CURRENT_WORD (LC_TIME,
68 _NL_TIME_ERA_NUM_ENTRIES);
69
70 if (new_num_eras == 0)
71 {
72 if (eras != NULL)
73 {
74 free (eras);
75 eras = NULL;
76 }
77 if (eras_nf != NULL)
78 {
79 free (eras_nf);
80 eras_nf = NULL;
81 }
82 }
83 else
84 {
85 if (num_eras != new_num_eras)
86 {
87 eras = realloc (eras,
88 new_num_eras * sizeof (struct era_entry *));
89 eras_nf = realloc (eras_nf,
90 new_num_eras * sizeof (void *)
91 * ERA_NAME_FORMAT_MEMBERS);
92 }
93
94 if (eras == NULL || eras_nf == NULL)
95 {
96 num_eras = 0;
97 eras = NULL;
98 eras_nf = NULL;
99 }
100 else
101 {
102 const char *ptr = _NL_CURRENT (LC_TIME, _NL_TIME_ERA_ENTRIES);
103 num_eras = new_num_eras;
104
105 for (cnt = 0; cnt < num_eras; ++cnt)
106 {
107 eras[cnt] = (struct era_entry *) ptr;
108
109 /* Skip numeric values. */
110 ptr += sizeof (struct era_entry);
111
112 /* Set and skip era name. */
113 ERAS_NF (cnt, ERA_M_NAME) = (void *) ptr;
114 ptr = strchr (ptr, '\0') + 1;
115
116 /* Set and skip era format. */
117 ERAS_NF (cnt, ERA_M_FORMAT) = (void *) ptr;
118 ptr = strchr (ptr, '\0') + 1;
119
120 ptr += 3 - (((ptr - (const char *) eras[cnt]) + 3) & 3);
121
122 /* Set and skip wide era name. */
123 ERAS_NF (cnt, ERA_W_NAME) = (void *) ptr;
124 ptr = (char *) (wcschr ((wchar_t *) ptr, '\0') + 1);
125
126 /* Set and skip wide era format. */
127 ERAS_NF (cnt, ERA_W_FORMAT) = (void *) ptr;
128 ptr = (char *) (wcschr ((wchar_t *) ptr, '\0') + 1);
129 }
130 }
131 }
132
133 era_initialized = 1;
134 }
135 }
136
137
138 struct era_entry *
139 _nl_get_era_entry (const struct tm *tp)
140 {
141 struct era_entry *result;
142 size_t cnt;
143
144 __libc_lock_lock (__libc_setlocale_lock);
145
146 init_era_entry ();
147
148 /* Now compare date with the available eras. */
149 for (cnt = 0; cnt < num_eras; ++cnt)
150 if ((eras[cnt]->start_date[0] < tp->tm_year
151 || (eras[cnt]->start_date[0] == tp->tm_year
152 && (eras[cnt]->start_date[1] < tp->tm_mon
153 || (eras[cnt]->start_date[1] == tp->tm_mon
154 && eras[cnt]->start_date[2] <= tp->tm_mday))))
155 && (eras[cnt]->stop_date[0] > tp->tm_year
156 || (eras[cnt]->stop_date[0] == tp->tm_year
157 && (eras[cnt]->stop_date[1] > tp->tm_mon
158 || (eras[cnt]->stop_date[1] == tp->tm_mon
159 && eras[cnt]->stop_date[2] >= tp->tm_mday)))))
160 break;
161
162 result = cnt < num_eras ? eras[cnt] : NULL;
163
164 __libc_lock_unlock (__libc_setlocale_lock);
165
166 return result;
167 }
168
169
170 const void *
171 _nl_get_era_nf_entry (int cnt, int category)
172 {
173 const void *result;
174
175 __libc_lock_lock (__libc_setlocale_lock);
176
177 init_era_entry ();
178
179 if (eras_nf == NULL)
180 result = NULL;
181 else
182 result = ERAS_NF (cnt, category);
183
184 __libc_lock_unlock (__libc_setlocale_lock);
185
186 return result;
187 }
188
189
190 int
191 _nl_get_era_year_offset (int cnt, int val)
192 {
193 __libc_lock_lock (__libc_setlocale_lock);
194
195 init_era_entry ();
196
197 if (eras == NULL)
198 val = -1;
199 else
200 {
201 val -= eras[cnt]->offset;
202
203 if (val < 0 ||
204 val > (eras[cnt]->stop_date[0] - eras[cnt]->start_date[0]))
205 val = -1;
206 }
207
208 __libc_lock_unlock (__libc_setlocale_lock);
209
210 return val;
211 }
212
213
214 int
215 _nl_get_era_year_start (int cnt)
216 {
217 int result;
218
219 __libc_lock_lock (__libc_setlocale_lock);
220
221 init_era_entry ();
222
223 result = eras[cnt]->start_date[0];
224
225 __libc_lock_unlock (__libc_setlocale_lock);
226
227 return result;
228 }
229
230
231 const char *
232 _nl_get_alt_digit (unsigned int number)
233 {
234 const char *result;
235
236 __libc_lock_lock (__libc_setlocale_lock);
237
238 if (alt_digits_initialized == 0)
239 {
240 alt_digits_initialized = 1;
241
242 if (alt_digits == NULL)
243 alt_digits = malloc (100 * sizeof (const char *));
244
245 if (alt_digits != NULL)
246 {
247 const char *ptr = _NL_CURRENT (LC_TIME, ALT_DIGITS);
248 size_t cnt;
249
250 if (alt_digits != NULL)
251 for (cnt = 0; cnt < 100; ++cnt)
252 {
253 alt_digits[cnt] = ptr;
254
255 /* Skip digit format. */
256 ptr = strchr (ptr, '\0') + 1;
257 }
258 }
259 }
260
261 result = alt_digits != NULL && number < 100 ? alt_digits[number] : NULL;
262
263 __libc_lock_unlock (__libc_setlocale_lock);
264
265 return result;
266 }
267
268
269 const wchar_t *
270 _nl_get_walt_digit (unsigned int number)
271 {
272 const wchar_t *result;
273
274 __libc_lock_lock (__libc_setlocale_lock);
275
276 if (walt_digits_initialized == 0)
277 {
278 walt_digits_initialized = 1;
279
280 if (walt_digits == NULL)
281 walt_digits = malloc (100 * sizeof (const uint32_t *));
282
283 if (walt_digits != NULL)
284 {
285 const wchar_t *ptr = _NL_CURRENT_WSTR (LC_TIME, _NL_WALT_DIGITS);
286 size_t cnt;
287
288 for (cnt = 0; cnt < 100; ++cnt)
289 {
290 walt_digits[cnt] = ptr;
291
292 /* Skip digit format. */
293 ptr = wcschr (ptr, L'\0') + 1;
294 }
295 }
296 }
297
298 result = walt_digits != NULL && number < 100 ? walt_digits[number] : NULL;
299
300 __libc_lock_unlock (__libc_setlocale_lock);
301
302 return (wchar_t *) result;
303 }
This page took 0.054054 seconds and 5 git commands to generate.