|
| 1 | +'use strict'; |
| 2 | +const common = require('../common'); |
| 3 | +const tmpdir = require('../common/tmpdir'); |
| 4 | + |
| 5 | +const assert = require('assert'); |
| 6 | +const fs = require('fs'); |
| 7 | + |
| 8 | +tmpdir.refresh(); |
| 9 | + |
| 10 | +// Check for Y2K38 support on POSIX. For Windows, assume it's there. |
| 11 | +if (!common.isWindows) { |
| 12 | + const testFilePath = `${tmpdir.path}/y2k38-test`; |
| 13 | + const testFileDate = '203801020304'; |
| 14 | + const { spawnSync } = require('child_process'); |
| 15 | + const touchResult = spawnSync('touch', |
| 16 | + ['-t', testFileDate, testFilePath], |
| 17 | + { encoding: 'utf8' }); |
| 18 | + assert.strictEqual(touchResult.status, 0); |
| 19 | + |
| 20 | + const dateResult = spawnSync('date', |
| 21 | + ['-r', testFilePath, '+%Y%m%d%H%M'], |
| 22 | + { encoding: 'utf8' }); |
| 23 | + assert.strictEqual(dateResult.status, 0); |
| 24 | + |
| 25 | + if (dateResult.stdout.trim() !== testFileDate) { |
| 26 | + common.skip('File system appears to lack Y2k38 support'); |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +// Ref: https://github.com/nodejs/node/issues/13255 |
| 31 | +const path = `${tmpdir.path}/test-utimes-precision`; |
| 32 | +fs.writeFileSync(path, ''); |
| 33 | + |
| 34 | +const Y2K38_mtime = 2 ** 31; |
| 35 | +fs.utimesSync(path, Y2K38_mtime, Y2K38_mtime); |
| 36 | +const Y2K38_stats = fs.statSync(path); |
| 37 | +assert.strictEqual(Y2K38_stats.mtime.getTime() / 1000, Y2K38_mtime); |
| 38 | + |
| 39 | +if (common.isWindows) { |
| 40 | + // This value would get converted to (double)1713037251359.9998 |
| 41 | + const truncate_mtime = 1713037251360; |
| 42 | + fs.utimesSync(path, truncate_mtime / 1000, truncate_mtime / 1000); |
| 43 | + const truncate_stats = fs.statSync(path); |
| 44 | + assert.strictEqual(truncate_stats.mtime.getTime(), truncate_mtime); |
| 45 | + |
| 46 | + // test Y2K38 for windows |
| 47 | + // This value if treaded as a `signed long` gets converted to -2135622133469. |
| 48 | + // POSIX systems stores timestamps in {long t_sec, long t_usec}. |
| 49 | + // NTFS stores times in nanoseconds in a single `uint64_t`, so when libuv |
| 50 | + // calculates (long)`uv_timespec_t.tv_sec` we get 2's complement. |
| 51 | + const overflow_mtime = 2159345162531; |
| 52 | + fs.utimesSync(path, overflow_mtime / 1000, overflow_mtime / 1000); |
| 53 | + const overflow_stats = fs.statSync(path); |
| 54 | + assert.strictEqual(overflow_stats.mtime.getTime(), overflow_mtime); |
| 55 | +} |
0 commit comments