coreutils icon indicating copy to clipboard operation
coreutils copied to clipboard

`touch -t` fails on Linux if machine has timezone GMT

Open barnex opened this issue 3 years ago • 8 comments

cargo test currently fails on my Debian and my Ubuntu 21.04 machine, both of which have timezone GMT (UTC+0).

---- test_du::test_du_time stdout ----
current_directory_resolved: 
run: /<redacted>/src/coreutils/target/debug/coreutils touch -a -t 201505150000 date_test
thread 'test_du::test_du_time' panicked at 'Command was expected to succeed.
stdout = 
 stderr = touch: invalid date format '201505150000'
', tests/common/util.rs:174:9

Actually cargo run touch -a -t xxxxxxxxxx file fails for any valid timestamp, irrespective of it being near daylight saving time changes or not.

When I change my timezone to anything other than GMT, the issue disappears.

I suspect this issue may be related to handling of daylight saving time, introduced in https://github.com/uutils/coreutils/pull/2073/commits/85a7375ae9aba9523edd23f50e7f0b0e27763ec0#diff-9565d8fad7b29837b3b468a61a809dc187fb351a72421ffe4b7c4879a8445b27R279.

Adding debug statements for that code shows, for the failing test case:

tm = { 
  tm_sec: 0, tm_min: 0, 
  tm_hour: 0, 
  tm_mday: 15, tm_mon: 4, tm_year: 115, tm_wday: 0, tm_yday: 0, tm_isdst: 0, 
  tm_utcoff: 0, 
  tm_nsec: 0 
},

tm2 = Tm {
  tm_sec: 0, tm_min: 0, 
  tm_hour: 1, 
  tm_mday: 15, tm_mon: 4, tm_year: 115, tm_wday: 5, tm_yday: 134, tm_isdst: 0,
  tm_utcoff: 3600, 
  tm_nsec: 0
}

Note the difference in tm_utcoff between the two, which appears to be the cause of this issue: even though the two times are equivalent, they have a different tm_hour, causing them to be interpreted as different and therefore invalid.

barnex avatar Dec 08 '21 17:12 barnex

@barnex do you want to propose a fix? thanks

sylvestre avatar Dec 08 '21 17:12 sylvestre

Sure, happy to look into this.

barnex avatar Dec 08 '21 18:12 barnex

@barnex ping?

sylvestre avatar Jan 29 '22 00:01 sylvestre

When I update the time comparison to take UTC offsets into account, the test in question passes, but then other tests fail because we now accept some timestamps that GNU rejects. I'm afraid that I'll have to hand off this issue at this point. Perhaps @ajanicij who wrote the initial timestamp checks is better placed to look into this.

barnex avatar Feb 01 '22 11:02 barnex

I will look into it.

ajanicij avatar Feb 01 '22 13:02 ajanicij

Hi, wanted to look at this recently and it seems to pass the tests, can this be closed?

qua3k avatar Aug 23 '22 00:08 qua3k

Sorry I haven't had time to look into it. It's good news that it is passing. @qua3k In which timezone did you try it?

ajanicij avatar Aug 23 '22 04:08 ajanicij

This was with GMT (temporarily symlinked /etc/localtime to the appropriate file on Ubuntu 22.04)

qua3k avatar Aug 23 '22 11:08 qua3k

This currently works very nicely, @barnex might you consider closing this?

# ls -l /etc/localtime 
lrwxrwxrwx 1 root root 25 12. Mär 00:15 /etc/localtime -> ../usr/share/zoneinfo/UTC
# target/release/coreutils touch -a -t 201505150000 date_test
# LC_ALL=C stat date_test 
  File: 'date_test'
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: 2ah/42d	Inode: 238673      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2015-05-15 00:00:00.000000000 +0000
Modify: 2016-06-16 04:00:00.000000000 +0000
Change: 2020-03-12 00:18:59.659290375 +0000
 Birth: -

kralo avatar Jun 21 '24 22:06 kralo

Thanks!

barnex avatar Jun 22 '24 09:06 barnex