1 /* Multithread-safety test for localtime_r().
2 Copyright (C) 2024-2025 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2024. */
21 /* Work around GCC bug 44511. */
22 #if _GL_GNUC_PREREQ (4, 3)
23 # pragma GCC diagnostic ignored "-Wreturn-type"
26 #if USE_ISOC_THREADS || USE_POSIX_THREADS || USE_ISOC_AND_POSIX_THREADS || USE_WINDOWS_THREADS
34 #include "glthread/thread.h"
38 /* Some common time zone name. */
40 #if defined _WIN32 && !defined __CYGWIN__
41 /* Cf. <https://learn.microsoft.com/en-us/windows-hardware/manufacture/desktop/default-time-zones>
42 or <https://ss64.com/timezones.html> */
43 # define FRENCH_TZ "Romance Standard Time"
45 /* Cf. <https://en.wikipedia.org/wiki/List_of_tz_database_time_zones> */
46 # define FRENCH_TZ "Europe/Paris"
51 thread1_func (void *arg
)
56 time_t t
= 1178467200; /* 2007-05-06 18:00:00 */
58 struct tm
*result
= localtime_r (&t
, &tm
);
59 ASSERT (result
== &tm
);
60 if (!(result
->tm_sec
== 0
61 && result
->tm_min
== 0
62 && result
->tm_hour
== 18
63 && result
->tm_mday
== 6
64 && result
->tm_mon
== 5 - 1
65 && result
->tm_year
== 2007 - 1900
66 && result
->tm_wday
== 0
67 && result
->tm_yday
== 125
68 && result
->tm_isdst
== 1))
70 fprintf (stderr
, "thread1 disturbed by thread2!\n"); fflush (stderr
);
79 thread2_func (void *arg
)
83 time_t t
= 1336320000; /* 2012-05-06 18:00:00 */
85 struct tm
*result
= localtime_r (&t
, &tm
);
86 ASSERT (result
== &tm
);
87 if (!(result
->tm_sec
== 0
88 && result
->tm_min
== 0
89 && result
->tm_hour
== 18
90 && result
->tm_mday
== 6
91 && result
->tm_mon
== 5 - 1
92 && result
->tm_year
== 2012 - 1900
93 && result
->tm_wday
== 0
94 && result
->tm_yday
== 126
95 && result
->tm_isdst
== 1))
97 fprintf (stderr
, "thread2 disturbed by thread1!\n"); fflush (stderr
);
106 main (int argc
, char *argv
[])
108 setenv ("TZ", FRENCH_TZ
, 1);
110 /* Check that this TZ works. */
112 time_t t
= 0; /* 1970-01-01 01:00:00 */
113 struct tm
*result
= localtime (&t
);
115 && result
->tm_sec
== 0
116 && result
->tm_min
== 0
117 && result
->tm_hour
== 1
118 && result
->tm_mday
== 1
119 && result
->tm_mon
== 1 - 1
120 && result
->tm_year
== 1970 - 1900
121 && result
->tm_wday
== 4
122 && result
->tm_yday
== 0
123 && result
->tm_isdst
== 0))
125 fputs ("Skipping test: TZ='" FRENCH_TZ
"' is not Paris time\n",
131 /* Create the threads. */
132 gl_thread_create (thread1_func
, NULL
);
133 gl_thread_create (thread2_func
, NULL
);
135 /* Let them run for 1 second. */
137 struct timespec duration
;
138 duration
.tv_sec
= (argc
> 1 ? atoi (argv
[1]) : 1);
139 duration
.tv_nsec
= 0;
141 nanosleep (&duration
, NULL
);
144 return test_exit_status
;
149 /* No multithreading available. */
156 fputs ("Skipping test: multithreading not enabled\n", stderr
);