1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
/* Test of nstrftime in Iran.
Copyright (C) 2025-2026 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
/* Written by Bruno Haible <bruno@clisp.org>, 2025. */
#include <config.h>
/* Specification. */
#include "strftime.h"
#include <locale.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "localcharset.h"
#include "macros.h"
#if defined _WIN32 && !defined __CYGWIN__
# define LOCALE1 "Persian_Iran.65001"
# define LOCALE2 NULL
#else
# define LOCALE1 "fa_IR.UTF-8"
# define LOCALE2 "fa_IR"
#endif
#define DECLARE_TM(variable, greg_year, greg_month, greg_day) \
struct tm variable = \
{ \
.tm_year = (greg_year) - 1900, \
.tm_mon = (greg_month) - 1, \
.tm_mday = (greg_day), \
.tm_hour = 12, .tm_min = 34, .tm_sec = 56 \
}; \
/* Fill the other fields. */ \
time_t tt = timegm (&variable); \
gmtime_r (&tt, &variable)/*;*/
int
main ()
{
if (((setenv ("LC_ALL", LOCALE1, 1),
(setlocale (LC_ALL, "") == NULL
|| strcmp (setlocale (LC_ALL, NULL), "C") == 0))
&& (LOCALE2 == NULL
|| (setenv ("LC_ALL", LOCALE2, 1),
(setlocale (LC_ALL, "") == NULL
|| strcmp (setlocale (LC_ALL, NULL), "C") == 0))))
|| strcmp (locale_charset (), "UTF-8") != 0)
{
fprintf (stderr, "Skipping test: Unicode locale for Iran is not installed\n");
return 77;
}
#if defined __OpenBSD__ || defined _AIX || defined __ANDROID__
fprintf (stderr, "Skipping test: determining the locale name is not worth it on this platform\n");
return 77;
#else
char buf[100];
ptrdiff_t ret;
/* Native Windows does not support dates before 1970-01-01. */
# if !(defined _WIN32 && !defined __CYGWIN__)
{
DECLARE_TM (tm, 1967, 10, 26);
ret = nstrftime (buf, sizeof (buf), "%Y-%m-%d",
&tm, (timezone_t) 0, 0);
ASSERT (ret > 0);
ASSERT (strcmp (buf, "1346-08-04") == 0);
ret = nstrftime (buf, sizeof (buf), "%-d %B %Y",
&tm, (timezone_t) 0, 0);
ASSERT (ret > 0);
ASSERT (strcmp (buf, "4 آبان 1346") == 0);
ret = nstrftime (buf, sizeof (buf), "%x",
&tm, (timezone_t) 0, 0);
ASSERT (ret > 0);
ASSERT (strcmp (buf, "۱۳۴۶/۸/۴") == 0);
}
{
DECLARE_TM (tm, 1969, 12, 28);
ret = nstrftime (buf, sizeof (buf), "%Y-%m-%d",
&tm, (timezone_t) 0, 0);
ASSERT (ret > 0);
ASSERT (strcmp (buf, "1348-10-07") == 0);
ret = nstrftime (buf, sizeof (buf), "%-d %B %Y",
&tm, (timezone_t) 0, 0);
ASSERT (ret > 0);
ASSERT (strcmp (buf, "7 دی 1348") == 0);
ret = nstrftime (buf, sizeof (buf), "%x",
&tm, (timezone_t) 0, 0);
ASSERT (ret > 0);
ASSERT (strcmp (buf, "۱۳۴۸/۱۰/۷") == 0);
}
# endif
/* Verify that 1403 is a leap year and 1404 is not. */
{
DECLARE_TM (tm, 2024, 3, 19);
ret = nstrftime (buf, sizeof (buf), "%Y-%m-%d",
&tm, (timezone_t) 0, 0);
ASSERT (ret > 0);
ASSERT (strcmp (buf, "1402-12-29") == 0);
ret = nstrftime (buf, sizeof (buf), "%-d %B %Y",
&tm, (timezone_t) 0, 0);
ASSERT (ret > 0);
ASSERT (strcmp (buf, "29 اسفند 1402") == 0);
ret = nstrftime (buf, sizeof (buf), "%x",
&tm, (timezone_t) 0, 0);
ASSERT (ret > 0);
ASSERT (strcmp (buf, "۱۴۰۲/۱۲/۲۹") == 0);
}
{
DECLARE_TM (tm, 2024, 3, 22);
ret = nstrftime (buf, sizeof (buf), "%Y-%m-%d",
&tm, (timezone_t) 0, 0);
ASSERT (ret > 0);
ASSERT (strcmp (buf, "1403-01-03") == 0);
ret = nstrftime (buf, sizeof (buf), "%-d %B %Y",
&tm, (timezone_t) 0, 0);
ASSERT (ret > 0);
ASSERT (strcmp (buf, "3 فروردین 1403") == 0);
ret = nstrftime (buf, sizeof (buf), "%x",
&tm, (timezone_t) 0, 0);
ASSERT (ret > 0);
ASSERT (strcmp (buf, "۱۴۰۳/۱/۳") == 0);
}
{
DECLARE_TM (tm, 2025, 3, 19);
ret = nstrftime (buf, sizeof (buf), "%Y-%m-%d",
&tm, (timezone_t) 0, 0);
ASSERT (ret > 0);
ASSERT (strcmp (buf, "1403-12-29") == 0);
ret = nstrftime (buf, sizeof (buf), "%-d %B %Y",
&tm, (timezone_t) 0, 0);
ASSERT (ret > 0);
ASSERT (strcmp (buf, "29 اسفند 1403") == 0);
ret = nstrftime (buf, sizeof (buf), "%x",
&tm, (timezone_t) 0, 0);
ASSERT (ret > 0);
ASSERT (strcmp (buf, "۱۴۰۳/۱۲/۲۹") == 0);
}
{
DECLARE_TM (tm, 2025, 3, 22);
ret = nstrftime (buf, sizeof (buf), "%Y-%m-%d",
&tm, (timezone_t) 0, 0);
ASSERT (ret > 0);
ASSERT (strcmp (buf, "1404-01-02") == 0);
ret = nstrftime (buf, sizeof (buf), "%-d %B %Y",
&tm, (timezone_t) 0, 0);
ASSERT (ret > 0);
ASSERT (strcmp (buf, "2 فروردین 1404") == 0);
ret = nstrftime (buf, sizeof (buf), "%x",
&tm, (timezone_t) 0, 0);
ASSERT (ret > 0);
ASSERT (strcmp (buf, "۱۴۰۴/۱/۲") == 0);
}
return test_exit_status;
#endif
}
|