Skip to content

Commit 6be62fd

Browse files
committed
Fix Windows native stat timestamps
1 parent 929df8c commit 6be62fd

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

‎graalpython/com.oracle.graal.python.test/src/tests/test_posix.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,15 @@ def test_fstat(self):
502502
with open(TEST_FULL_PATH2, 0) as fd: # follows symlink
503503
self.assertEqual(inode, os.fstat(fd).st_ino)
504504

505+
@unittest.skipUnless(sys.platform == 'win32' and __graalpython__.posix_module_backend() == 'native',
506+
'test requires the Windows native POSIX backend')
507+
def test_windows_native_stat_uses_utc_timestamps(self):
508+
timestamp = 1700000000
509+
os.utime(TEST_FULL_PATH1, (timestamp, timestamp))
510+
self.assertEqual(timestamp, int(os.stat(TEST_FULL_PATH1).st_mtime))
511+
with open(TEST_FULL_PATH1, 0) as fd:
512+
self.assertEqual(timestamp, int(os.fstat(fd).st_mtime))
513+
505514
def test_statvfs(self):
506515
res = os.statvfs(TEST_FULL_PATH1)
507516
with open(TEST_FULL_PATH1, 0) as fd:

‎graalpython/python-libposix/src/posix.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,13 @@ static void unix_time_to_filetime(int64_t seconds, int64_t nanoseconds, FILETIME
579579
out->dwHighDateTime = (DWORD) (ticks >> 32);
580580
}
581581

582+
static int64_t filetime_to_unix_time(FILETIME time) {
583+
ULARGE_INTEGER ticks;
584+
ticks.LowPart = time.dwLowDateTime;
585+
ticks.HighPart = time.dwHighDateTime;
586+
return (int64_t) (ticks.QuadPart / 10000000ULL) - 11644473600LL;
587+
}
588+
582589
static int set_file_times(HANDLE handle, int64_t *times, int32_t nanosecond_resolution) {
583590
FILETIME atime;
584591
FILETIME mtime;
@@ -774,6 +781,9 @@ static void stat_handle_to_longs(HANDLE handle, int64_t *out) {
774781
out[1] = ((int64_t) info.nFileIndexHigh << 32) | info.nFileIndexLow;
775782
out[2] = info.dwVolumeSerialNumber;
776783
out[3] = info.nNumberOfLinks;
784+
out[7] = filetime_to_unix_time(info.ftLastAccessTime);
785+
out[8] = filetime_to_unix_time(info.ftLastWriteTime);
786+
out[9] = filetime_to_unix_time(info.ftCreationTime);
777787
}
778788
}
779789

0 commit comments

Comments
 (0)