Skip to content

Commit 794f77f

Browse files
authored
Fix timespec/localtime (#981)
* fix localtime timespec * fix localtime timespec
1 parent fa842cb commit 794f77f

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/glibc/target/include/bits/types/struct_timespec.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@
3232

3333
struct timespec
3434
{
35-
__time64_t tv_sec; /* Seconds. */
36-
long int tv_nsec; /* Nanoseconds. */
35+
time_t tv_sec; /* Seconds. */
3736
__int32_t __padding; /* Padding. */
37+
long int tv_nsec; /* Nanoseconds. */
38+
__int32_t __padding2; /* Padding. */
3839
};
3940

4041
#endif
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <time.h>
4+
#include <sys/stat.h>
5+
#include <assert.h>
6+
7+
int main() {
8+
/* Verify struct timespec.tv_sec is time_t (POSIX requirement).
9+
This was broken when the installed header used __time64_t
10+
instead of time_t for tv_sec on wasm32. */
11+
12+
/* 1. sizeof check: tv_sec must be the same size as time_t */
13+
struct timespec ts;
14+
assert(sizeof(ts.tv_sec) == sizeof(time_t));
15+
16+
/* 2. Type compatibility: &ts.tv_sec must be assignable to time_t* */
17+
time_t *tp = &ts.tv_sec;
18+
(void)tp;
19+
20+
/* 3. The actual use case that broke: stat + localtime */
21+
struct stat st;
22+
int ret = stat("/", &st);
23+
assert(ret == 0);
24+
25+
struct tm *tm = localtime(&st.st_mtime);
26+
assert(tm != NULL);
27+
28+
printf("timespec_time_t_compat: all checks passed\n");
29+
return 0;
30+
}

0 commit comments

Comments
 (0)