time: fix incorrect DST offset when using POSIX timezones without DST
[musl.git] / src / locale / newlocale.c
blobf50bbe9132df39445c642ea3215428b4b2b960d5
1 #include <stdlib.h>
2 #include <string.h>
3 #include "locale_impl.h"
4 #include "libc.h"
6 int __loc_is_allocated(locale_t loc)
8 return loc && loc != C_LOCALE && loc != UTF8_LOCALE;
11 locale_t __newlocale(int mask, const char *name, locale_t loc)
13 int i, j;
14 struct __locale_struct tmp;
15 const struct __locale_map *lm;
17 /* For locales with allocated storage, modify in-place. */
18 if (__loc_is_allocated(loc)) {
19 for (i=0; i<LC_ALL; i++)
20 if (mask & (1<<i))
21 loc->cat[i] = __get_locale(i, name);
22 return loc;
25 /* Otherwise, build a temporary locale object, which will only
26 * be instantiated in allocated storage if it does not match
27 * one of the built-in static locales. This makes the common
28 * usage case for newlocale, getting a C locale with predictable
29 * behavior, very fast, and more importantly, fail-safe. */
30 for (j=i=0; i<LC_ALL; i++) {
31 if (loc && !(mask & (1<<i)))
32 lm = loc->cat[i];
33 else
34 lm = __get_locale(i, mask & (1<<i) ? name : "");
35 if (lm) j++;
36 tmp.cat[i] = lm;
39 if (!j)
40 return C_LOCALE;
41 if (j==1 && tmp.cat[LC_CTYPE]==&__c_dot_utf8)
42 return UTF8_LOCALE;
44 if ((loc = malloc(sizeof *loc))) *loc = tmp;
46 return loc;
49 weak_alias(__newlocale, newlocale);