lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | #include <locale.h> |
| 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <string.h> |
| 5 | #include <time.h> |
| 6 | #include <wchar.h> |
| 7 | |
| 8 | |
| 9 | static int |
| 10 | do_test (void) |
| 11 | { |
| 12 | locale_t l; |
| 13 | locale_t old; |
| 14 | struct tm tm; |
| 15 | char buf[1000]; |
| 16 | wchar_t wbuf[1000]; |
| 17 | int result = 0; |
| 18 | size_t n; |
| 19 | |
| 20 | l = newlocale (LC_ALL_MASK, "de_DE.ISO-8859-1", NULL); |
| 21 | if (l == NULL) |
| 22 | { |
| 23 | puts ("newlocale failed"); |
| 24 | exit (1); |
| 25 | } |
| 26 | |
| 27 | memset (&tm, '\0', sizeof (tm)); |
| 28 | |
| 29 | tm.tm_year = 102; |
| 30 | tm.tm_mon = 2; |
| 31 | tm.tm_mday = 1; |
| 32 | |
| 33 | if (strftime (buf, sizeof (buf), "%e %^B %Y", &tm) == 0) |
| 34 | { |
| 35 | puts ("initial strftime failed"); |
| 36 | exit (1); |
| 37 | } |
| 38 | if (strcmp (buf, " 1 MARCH 2002") != 0) |
| 39 | { |
| 40 | printf ("initial strftime: expected \"%s\", got \"%s\"\n", |
| 41 | " 1 MARCH 2002", buf); |
| 42 | result = 1; |
| 43 | } |
| 44 | else |
| 45 | printf ("got \"%s\"\n", buf); |
| 46 | |
| 47 | /* Now using the extended locale model. */ |
| 48 | if (strftime_l (buf, sizeof (buf), "%e %^B %Y", &tm, l) == 0) |
| 49 | { |
| 50 | puts ("strftime_l failed"); |
| 51 | result = 1; |
| 52 | } |
| 53 | else if (strcmp (buf, " 1 M\xc4RZ 2002") != 0) |
| 54 | { |
| 55 | printf ("strftime_l: expected \"%s\", got \"%s\"\n", |
| 56 | " 1 M\xc4RZ 2002", buf); |
| 57 | result = 1; |
| 58 | } |
| 59 | else |
| 60 | { |
| 61 | setlocale (LC_ALL, "de_DE.ISO-8859-1"); |
| 62 | printf ("got \"%s\"\n", buf); |
| 63 | setlocale (LC_ALL, "C"); |
| 64 | } |
| 65 | |
| 66 | /* And the wide character version. */ |
| 67 | if (wcsftime_l (wbuf, sizeof (wbuf) / sizeof (wbuf[0]), L"%e %^B %Y", &tm, l) |
| 68 | == 0) |
| 69 | { |
| 70 | puts ("wcsftime_l failed"); |
| 71 | result = 1; |
| 72 | } |
| 73 | else if (wcscmp (wbuf, L" 1 M\x00c4RZ 2002") != 0) |
| 74 | { |
| 75 | printf ("wcsftime_l: expected \"%ls\", got \"%ls\"\n", |
| 76 | L" 1 M\x00c4RZ 2002", wbuf); |
| 77 | result = 1; |
| 78 | } |
| 79 | else |
| 80 | { |
| 81 | setlocale (LC_ALL, "de_DE.ISO-8859-1"); |
| 82 | printf ("got \"%ls\"\n", wbuf); |
| 83 | setlocale (LC_ALL, "C"); |
| 84 | } |
| 85 | |
| 86 | old = uselocale (l); |
| 87 | |
| 88 | n = strftime (buf, sizeof (buf), "%e %^B %Y", &tm); |
| 89 | |
| 90 | /* Switch back. */ |
| 91 | (void) uselocale (old); |
| 92 | |
| 93 | if (n == 0) |
| 94 | { |
| 95 | puts ("strftime after first uselocale failed"); |
| 96 | result = 1; |
| 97 | } |
| 98 | else if (strcmp (buf, " 1 M\xc4RZ 2002") != 0) |
| 99 | { |
| 100 | printf ("strftime in non-C locale: expected \"%s\", got \"%s\"\n", |
| 101 | " 1 M\xc4RZ 2002", buf); |
| 102 | result = 1; |
| 103 | } |
| 104 | else |
| 105 | { |
| 106 | setlocale (LC_ALL, "de_DE.ISO-8859-1"); |
| 107 | printf ("got \"%s\"\n", buf); |
| 108 | setlocale (LC_ALL, "C"); |
| 109 | } |
| 110 | |
| 111 | if (strftime (buf, sizeof (buf), "%e %^B %Y", &tm) == 0) |
| 112 | { |
| 113 | puts ("strftime after second uselocale failed"); |
| 114 | result = 1; |
| 115 | } |
| 116 | else if (strcmp (buf, " 1 MARCH 2002") != 0) |
| 117 | { |
| 118 | printf ("initial strftime: expected \"%s\", got \"%s\"\n", |
| 119 | " 1 MARCH 2002", buf); |
| 120 | result = 1; |
| 121 | } |
| 122 | else |
| 123 | printf ("got \"%s\"\n", buf); |
| 124 | |
| 125 | return result; |
| 126 | } |
| 127 | |
| 128 | #define TEST_FUNCTION do_test () |
| 129 | #include "../test-skeleton.c" |