Update.
[platform/upstream/glibc.git] / locale / lc-time.c
1 /* Define current locale data for LC_TIME category.
2    Copyright (C) 1995-1999, 2000 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
36 static struct era_entry *eras;
37 static size_t num_eras;
38 static int alt_digits_initialized;
39 static const char **alt_digits;
40
41
42 static int walt_digits_initialized;
43 static const wchar_t **walt_digits;
44
45
46 void
47 _nl_postload_time (void)
48 {
49   /* Prepare lazy initialization of `era' and `alt_digits' array.  */
50   era_initialized = 0;
51   alt_digits_initialized = 0;
52   walt_digits_initialized = 0;
53 }
54
55 #define ERA_DATE_CMP(a, b) \
56   (a[0] < b[0] || (a[0] == b[0] && (a[1] < b[1]                               \
57                                     || (a[1] == b[1] && a[2] <= b[2]))))
58
59 static void
60 _nl_init_era_entries (void)
61 {
62   size_t cnt;
63
64   __libc_lock_lock (__libc_setlocale_lock);
65
66   if (era_initialized == 0)
67     {
68       size_t new_num_eras = _NL_CURRENT_WORD (LC_TIME,
69                                               _NL_TIME_ERA_NUM_ENTRIES);
70       if (new_num_eras == 0)
71         {
72           free (eras);
73           eras = NULL;
74         }
75       else
76         {
77           if (num_eras != new_num_eras)
78             eras = (struct era_entry *) realloc (eras,
79                                                  new_num_eras
80                                                  * sizeof (struct era_entry));
81           if (eras == NULL)
82             {
83               num_eras = 0;
84               eras = NULL;
85             }
86           else
87             {
88               const char *ptr = _NL_CURRENT (LC_TIME, _NL_TIME_ERA_ENTRIES);
89               num_eras = new_num_eras;
90
91               for (cnt = 0; cnt < num_eras; ++cnt)
92                 {
93                   const char *base_ptr = ptr;
94                   memcpy ((void *) (eras + cnt), (const void *) ptr,
95                           sizeof (uint32_t) * 8);
96
97                   if (ERA_DATE_CMP(eras[cnt].start_date,
98                                    eras[cnt].stop_date))
99                     if (eras[cnt].direction == (uint32_t) '+')
100                       eras[cnt].absolute_direction = 1;
101                     else
102                       eras[cnt].absolute_direction = -1;
103                   else
104                     if (eras[cnt].direction == (uint32_t) '+')
105                       eras[cnt].absolute_direction = -1;
106                     else
107                       eras[cnt].absolute_direction = 1;
108
109                   /* Skip numeric values.  */
110                   ptr += sizeof (uint32_t) * 8;
111
112                   /* Set and skip era name.  */
113                   eras[cnt].era_name = ptr;
114                   ptr = strchr (ptr, '\0') + 1;
115
116                   /* Set and skip era format.  */
117                   eras[cnt].era_format = ptr;
118                   ptr = strchr (ptr, '\0') + 1;
119
120                   ptr += 3 - (((ptr - (const char *) base_ptr) + 3) & 3);
121
122                   /* Set and skip wide era name.  */
123                   eras[cnt].era_wname = (wchar_t *) ptr;
124                   ptr = (char *) (wcschr ((wchar_t *) ptr, '\0') + 1);
125
126                   /* Set and skip wide era format.  */
127                   eras[cnt].era_wformat = (wchar_t *) ptr;
128                   ptr = (char *) (wcschr ((wchar_t *) ptr, '\0') + 1);
129                 }
130             }
131         }
132
133       era_initialized = 1;
134     }
135
136   __libc_lock_unlock (__libc_setlocale_lock);
137 }
138
139
140 struct era_entry *
141 _nl_get_era_entry (const struct tm *tp)
142 {
143   struct era_entry *result;
144   int32_t tdate[3];
145   size_t cnt;
146
147   tdate[0] = tp->tm_year;
148   tdate[1] = tp->tm_mon;
149   tdate[2] = tp->tm_mday;
150
151   if (era_initialized == 0)
152     _nl_init_era_entries ();
153
154   /* Now compare date with the available eras.  */
155   for (cnt = 0; cnt < num_eras; ++cnt)
156     if ((ERA_DATE_CMP(eras[cnt].start_date, tdate)
157          && ERA_DATE_CMP(tdate, eras[cnt].stop_date))
158         || (ERA_DATE_CMP(eras[cnt].stop_date, tdate)
159             && ERA_DATE_CMP(tdate, eras[cnt].start_date)))
160       break;
161
162   result = cnt < num_eras ? &eras[cnt] : NULL;
163
164   return result;
165 }
166
167
168 struct era_entry *
169 _nl_select_era_entry (int cnt)
170 {
171   if (era_initialized == 0)
172     _nl_init_era_entries ();
173
174   return &eras[cnt];
175 }
176
177
178 const char *
179 _nl_get_alt_digit (unsigned int number)
180 {
181   const char *result;
182
183   __libc_lock_lock (__libc_setlocale_lock);
184
185   if (alt_digits_initialized == 0)
186     {
187       alt_digits_initialized = 1;
188
189       if (alt_digits == NULL)
190         alt_digits = malloc (100 * sizeof (const char *));
191
192       if (alt_digits != NULL)
193         {
194           const char *ptr = _NL_CURRENT (LC_TIME, ALT_DIGITS);
195           size_t cnt;
196
197           if (alt_digits != NULL)
198             for (cnt = 0; cnt < 100; ++cnt)
199               {
200                 alt_digits[cnt] = ptr;
201
202                 /* Skip digit format. */
203                 ptr = strchr (ptr, '\0') + 1;
204               }
205         }
206     }
207
208   result = alt_digits != NULL && number < 100 ? alt_digits[number] : NULL;
209
210   __libc_lock_unlock (__libc_setlocale_lock);
211
212   return result;
213 }
214
215
216 const wchar_t *
217 _nl_get_walt_digit (unsigned int number)
218 {
219   const wchar_t *result;
220
221   __libc_lock_lock (__libc_setlocale_lock);
222
223   if (walt_digits_initialized == 0)
224     {
225       walt_digits_initialized = 1;
226
227       if (walt_digits == NULL)
228         walt_digits = malloc (100 * sizeof (const uint32_t *));
229
230       if (walt_digits != NULL)
231         {
232           const wchar_t *ptr = _NL_CURRENT_WSTR (LC_TIME, _NL_WALT_DIGITS);
233           size_t cnt;
234
235           for (cnt = 0; cnt < 100; ++cnt)
236             {
237               walt_digits[cnt] = ptr;
238
239               /* Skip digit format. */
240               ptr = wcschr (ptr, L'\0') + 1;
241             }
242         }
243     }
244
245   result = walt_digits != NULL && number < 100 ? walt_digits[number] : NULL;
246
247   __libc_lock_unlock (__libc_setlocale_lock);
248
249   return (wchar_t *) result;
250 }