At 15:02 -1000 2000.08.19, Tim Jenness wrote:
>I'm of the camp that feels perl should have a fixed epoch rather than the
>epoch of the underlining OS. Furthermore, I can understand that the OS
>epoch can also be important when looking outside of perl. Thinking this
>over for a while it occurred to me that time() currently has no arguments
>and that an epoch argument would seemingly solve all our problems.
>
>
>     time()          - returns seconds since Perl epoch
>     time("sys")     - seconds since start of OS epoch
>     time("tai")     - seconds since TAI began
>     time("jul")     - seconds since julian day 0
>     time("vms")     - seconds since VMS epoch (MJD 0)
>     time("wall")    - seconds since Larry Wall was born
>     etc.
>
>The only assumption here is that time() returns seconds (not necessarily
>integers) from an epoch and that Perl epoch is the thing expected by the
>localtime() replacement. Whether time() returns fractioanl seconds or a
>libtai object (with seconds and nanoseconds) is another issue.

So what would (stat($_))[8,9,10] return?  :)

I think maybe what should happen instead is that a function or module be
provided to handle epoch conversions, to and from the default "Perl" epoch.
perl2epoch($perlsec, 'vms'), epoch2perl($vmssec, 'vms').


>All of
>these are simply integer second offsets from each other.

Not quite.  For Mac OS, you'd need a third time zone argument (perhaps
offset from GMT), since Mac OS epoch is seconds sin Jan 01, 1904 00:00:00
_local time_:  perl2epoch($perlsec, 'macos', '-0500').  Yummy.

Anyway, here is some code that works.


#!perl -wl
my $perlsec  = 966770661;
my $epochsec = perl2epoch($perlsec, 'macos', '-0400');
my $perlsec2 = epoch2perl($epochsec, 'macos');

print $perlsec;
print $perlsec2;
print $epochsec;
print scalar localtime $epochsec;

sub perl2epoch {
    my($perlsec, $epoch, @extra) = @_;
    my $epochsec;

    if ($epoch eq 'macos') {
        $epochsec = $perlsec + macos_offset();
        $epochsec += tz_offset($extra[0]);
    }

    return $epochsec;

}

sub epoch2perl {
    my($epochsec, $epoch, @extra) = @_;
    my $perlsec;

    if ($epoch eq 'macos') {
        $perlsec = $epochsec - macos_offset();
        $perlsec -= tz_offset($extra[0]);
    }

    return $perlsec;

}

sub macos_offset () { 2082844801 }

# guess with Time::Local if not provided?
sub tz_offset {
    my($offset) = @_;
    my $tz_offset;

    if ($offset) {
        $tz_offset = $offset * 36;
    } else {
        require Time::Local;
        $tz_offset = sprintf "%+0.4d\n", (Time::Local::timelocal(localtime)
            - Time::Local::timelocal(gmtime));
    }

    return $tz_offset;
}

Returns:

966770661
966770661
3049601062
Sun Aug 20 07:24:22 2000

-- 
Chris Nandor       |     [EMAIL PROTECTED]      |     http://pudge.net/
Andover.Net        | [EMAIL PROTECTED] | http://slashcode.com/

Reply via email to