1 /* Define current locale data for LC_TIME category.
2 Copyright (C) 1995, 1996 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
17 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18 Cambridge, MA 02139, USA. */
20 #include <libc-lock.h>
24 #include "localeinfo.h"
26 _NL_CURRENT_DEFINE (LC_TIME
);
28 /* Some of the functions here must not be used while setlocale is called. */
29 __libc_lock_define (extern, __libc_setlocale_lock
)
32 static int era_initialized
;
33 static struct era_entry
**eras
;
34 static size_t num_eras
;
37 static int alt_digits_initialized
;
38 static const char **alt_digits
;
39 static size_t num_alt_digits
;
43 _nl_postload_time (void)
45 /* Prepare lazy initialization of `era' and `alt_digits' array. */
47 alt_digits_initialized
= 0;
52 _nl_get_era_entry (const struct tm
*tp
)
54 struct era_entry
*result
;
57 __libc_lock_lock (__libc_setlocale_lock
);
59 if (era_initialized
== 0)
61 size_t new_num_eras
= _NL_CURRENT_WORD (LC_TIME
,
62 _NL_TIME_ERA_NUM_ENTRIES
);
64 if (eras
!= NULL
&& new_num_eras
== 0)
69 else if (new_num_eras
!= 0)
71 if (num_eras
!= new_num_eras
)
72 eras
= realloc (eras
, new_num_eras
* sizeof (struct era_entry
*));
78 #if __BYTE_ORDER == __LITTLE_ENDIAN
79 const char *ptr
= _NL_CURRENT (LC_TIME
, _NL_TIME_ERA_ENTRIES_EL
);
81 const char *ptr
= _NL_CURRENT (LC_TIME
, _NL_TIME_ERA_ENTRIES_EB
);
83 num_eras
= new_num_eras
;
85 for (cnt
= 0; cnt
< num_eras
; ++cnt
)
87 eras
[cnt
] = (struct era_entry
*) ptr
;
89 /* Skip numeric values. */
90 ptr
+= sizeof (struct era_entry
);
92 ptr
= strchr (ptr
, '\0') + 1;
93 /* Skip era format. */
94 ptr
= strchr (ptr
, '\0') + 1;
96 ptr
+= 3 - (((ptr
- (const char *) eras
[cnt
]) + 3) & 3);
104 /* Now compare date with the available eras. */
105 for (cnt
= 0; cnt
< num_eras
; ++cnt
)
106 if ((eras
[cnt
]->start_date
[0] < tp
->tm_year
107 || (eras
[cnt
]->start_date
[0] == tp
->tm_year
108 && (eras
[cnt
]->start_date
[1] < tp
->tm_mon
109 || (eras
[cnt
]->start_date
[1] == tp
->tm_mon
110 && eras
[cnt
]->start_date
[2] <= tp
->tm_mday
))))
111 && (eras
[cnt
]->stop_date
[0] > tp
->tm_year
112 || (eras
[cnt
]->stop_date
[0] == tp
->tm_year
113 && (eras
[cnt
]->stop_date
[1] > tp
->tm_mon
114 || (eras
[cnt
]->stop_date
[1] == tp
->tm_mon
115 && eras
[cnt
]->stop_date
[2] >= tp
->tm_mday
)))))
118 result
= cnt
< num_eras
? eras
[cnt
] : NULL
;
120 __libc_lock_unlock (__libc_setlocale_lock
);
127 _nl_get_alt_digit (unsigned int number
)
131 __libc_lock_lock (__libc_setlocale_lock
);
133 if (alt_digits_initialized
== 0)
135 size_t new_num_alt_digits
= _NL_CURRENT_WORD (LC_TIME
,
136 _NL_TIME_NUM_ALT_DIGITS
);
138 if (alt_digits
!= NULL
&& new_num_alt_digits
== 0)
143 else if (new_num_alt_digits
!= 0)
145 if (num_alt_digits
!= new_num_alt_digits
)
146 alt_digits
= realloc (alt_digits
, (new_num_alt_digits
147 * sizeof (const char *)));
149 if (alt_digits
== NULL
)
153 const char *ptr
= _NL_CURRENT (LC_TIME
, ALT_DIGITS
);
156 num_alt_digits
= new_num_alt_digits
;
158 for (cnt
= 0; cnt
< num_alt_digits
; ++cnt
)
160 alt_digits
[cnt
] = ptr
;
162 /* Skip digit format. */
163 ptr
= strchr (ptr
, '\0') + 1;
168 alt_digits_initialized
= 1;
171 result
= number
< num_alt_digits
? alt_digits
[number
] : NULL
;
173 __libc_lock_unlock (__libc_setlocale_lock
);