in reply to stat() and time-zone-offsets?

mmhh... could be my memory plays tricks on me, but what brought me to thinking that the mtime/epoch isn't timezone corrected were a number of stats() where the time was skewed against what Windows or the CLI stat told me.

Now thinking again, I tend to accept stat() returns GMT epoch.
Still, are you sure this is consistent across *nix and Windows. Does a Perl stat() on Windows/NTFS return GMT epoch - really?
The incident where I ran into this skew, if I recall correctly, was a script that worked across filesystems (although both NTFS). It recorded the stat() on a file on a first filesystem and then duplicated the file and its stat() on a second. When I looked at the cloned file's metadata via Windows's file properties it showed a 1 hour time offset. And I couldn't quite figure out if this bug was introduced by my script, by Windows/NTFS or an ignorance of the TZ offset in stat(). stat() returning time-zone-offset'ed epochs would have beautifully explained it...

Replies are listed 'Best First'.
Re^2: stat() and time-zone-offsets?
by ikegami (Patriarch) on Jun 17, 2010 at 14:55 UTC

    Epoch time in unix is the number of seconds since the beginning of Jan 1st, 1970 GMT (or UTC? It's basically the same thing as far as most people are concerned).

    Still, are you sure this is consistent across *nix and Windows.

    Yes, stat returns unix epoch times on Windows. But read on.

    When I looked at the cloned file's metadata via Windows's file properties it showed a 1 hour time offset.

    Explorer shows the timestamp using the current local time zone. Sounds normal, but there's a catch. It views timezones as offsets from GMT, not slices of geograghy, so EST and EDT are two different time zones to it.

    If I created a file at

    2010-01-01 18:00:00 (local time)

    and if checked the creation date today, Explorer would show it as being created at

    2010-01-01 17:00:00 (EDT)
    rather than
    2010-01-01 18:00:00 (America/New_York)

    I don't know if that's a problem in Explorer, or a problem with the OS itself. It could even be specific to the file system. This could have an effect on stat.

Re^2: stat() and time-zone-offsets?
by Anonymous Monk on Jun 17, 2010 at 11:58 UTC
    Still, are you sure this is consistent across *nix and Windows. Does a Perl stat() on Windows/NTFS return GMT epoch - really?

    Yes, I think so

    #!/usr/bin/perl -- use strict; use warnings; use autodie 2.10; use File::Spec; Main(@ARGV); exit(0); sub Main { my $file = File::Spec->rel2abs(__FILE__) . '-testfile'; open my ($fh), '>', $file; close $fh; print " gmtime ", scalar gmtime, "\n"; print " stat gmtime ", scalar gmtime( ( stat $file )[10] ), "\n" +; print " localtime ", scalar localtime, "\n"; print "stat localtime ", scalar localtime( ( stat $file )[10] ), " +\n"; unlink $file; } ## end sub Main __END__
    gmtime Thu Jun 17 11:36:22 2010 stat gmtime Thu Jun 17 11:36:22 2010 localtime Thu Jun 17 04:36:22 2010 stat localtime Thu Jun 17 04:36:22 2010