Update.
[glibc.git] / locale / lc-time.c
blob78789dce41805c1984dd978dd71770aabe1f3fd3
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.
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.
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.
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. */
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"
28 _NL_CURRENT_DEFINE (LC_TIME);
30 /* Some of the functions here must not be used while setlocale is called. */
31 __libc_lock_define (extern, __libc_setlocale_lock)
34 static int era_initialized;
35 static struct era_entry **eras;
36 static size_t num_eras;
39 static int alt_digits_initialized;
40 static const char **alt_digits;
43 static int walt_digits_initialized;
44 static const wchar_t **walt_digits;
47 void
48 _nl_postload_time (void)
50 /* Prepare lazy initialization of `era' and `alt_digits' array. */
51 era_initialized = 0;
52 alt_digits_initialized = 0;
53 walt_digits_initialized = 0;
57 struct era_entry *
58 _nl_get_era_entry (const struct tm *tp)
60 struct era_entry *result;
61 size_t cnt;
63 __libc_lock_lock (__libc_setlocale_lock);
65 if (era_initialized == 0)
67 size_t new_num_eras = _NL_CURRENT_WORD (LC_TIME,
68 _NL_TIME_ERA_NUM_ENTRIES);
70 if (eras != NULL && new_num_eras == 0)
72 free (eras);
73 eras = NULL;
75 else if (new_num_eras != 0)
77 if (num_eras != new_num_eras)
78 eras = realloc (eras, new_num_eras * sizeof (struct era_entry *));
80 if (eras == NULL)
81 num_eras = 0;
82 else
84 const char *ptr = _NL_CURRENT (LC_TIME, _NL_TIME_ERA_ENTRIES);
85 num_eras = new_num_eras;
87 for (cnt = 0; cnt < num_eras; ++cnt)
89 eras[cnt] = (struct era_entry *) ptr;
91 /* Skip numeric values. */
92 ptr += sizeof (struct era_entry);
93 /* Skip era name. */
94 ptr = strchr (ptr, '\0') + 1;
95 /* Skip era format. */
96 ptr = strchr (ptr, '\0') + 1;
98 ptr += 3 - (((ptr - (const char *) eras[cnt]) + 3) & 3);
100 /* Skip wide era name. */
101 ptr = (char *) (wcschr ((wchar_t *) ptr, '\0') + 1);
102 /* Skip wide era format. */
103 ptr = (char *) (wcschr ((wchar_t *) ptr, '\0') + 1);
108 era_initialized = 1;
111 /* Now compare date with the available eras. */
112 for (cnt = 0; cnt < num_eras; ++cnt)
113 if ((eras[cnt]->start_date[0] < tp->tm_year
114 || (eras[cnt]->start_date[0] == tp->tm_year
115 && (eras[cnt]->start_date[1] < tp->tm_mon
116 || (eras[cnt]->start_date[1] == tp->tm_mon
117 && eras[cnt]->start_date[2] <= tp->tm_mday))))
118 && (eras[cnt]->stop_date[0] > tp->tm_year
119 || (eras[cnt]->stop_date[0] == tp->tm_year
120 && (eras[cnt]->stop_date[1] > tp->tm_mon
121 || (eras[cnt]->stop_date[1] == tp->tm_mon
122 && eras[cnt]->stop_date[2] >= tp->tm_mday)))))
123 break;
125 result = cnt < num_eras ? eras[cnt] : NULL;
127 __libc_lock_unlock (__libc_setlocale_lock);
129 return result;
133 const char *
134 _nl_get_alt_digit (unsigned int number)
136 const char *result;
138 __libc_lock_lock (__libc_setlocale_lock);
140 if (alt_digits_initialized == 0)
142 alt_digits_initialized = 1;
144 if (alt_digits == NULL)
145 alt_digits = malloc (100 * sizeof (const char *));
147 if (alt_digits != NULL)
149 const char *ptr = _NL_CURRENT (LC_TIME, ALT_DIGITS);
150 size_t cnt;
152 if (alt_digits != NULL)
153 for (cnt = 0; cnt < 100; ++cnt)
155 alt_digits[cnt] = ptr;
157 /* Skip digit format. */
158 ptr = strchr (ptr, '\0') + 1;
163 result = alt_digits != NULL && number < 100 ? alt_digits[number] : NULL;
165 __libc_lock_unlock (__libc_setlocale_lock);
167 return result;
171 const wchar_t *
172 _nl_get_walt_digit (unsigned int number)
174 const wchar_t *result;
176 __libc_lock_lock (__libc_setlocale_lock);
178 if (walt_digits_initialized == 0)
180 walt_digits_initialized = 1;
182 if (walt_digits == NULL)
183 walt_digits = malloc (100 * sizeof (const uint32_t *));
185 if (walt_digits != NULL)
187 const wchar_t *ptr = _NL_CURRENT_WSTR (LC_TIME, _NL_WALT_DIGITS);
188 size_t cnt;
190 for (cnt = 0; cnt < 100; ++cnt)
192 walt_digits[cnt] = ptr;
194 /* Skip digit format. */
195 ptr = wcschr (ptr, L'\0') + 1;
200 result = walt_digits != NULL && number < 100 ? walt_digits[number] : NULL;
202 __libc_lock_unlock (__libc_setlocale_lock);
204 return (wchar_t *) result;