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. All of
these are simply integer second offsets from each other.

--
Tim Jenness
JCMT software engineer/Support scientist
http://www.jach.hawaii.edu/~timj

Search Discussions

  • Chris Nandor at Aug 20, 2000 at 11:34 am

    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 | pudge@pobox.com | http://pudge.net/
    Andover.Net | chris.nandor@andover.net | http://slashcode.com/
  • Tim Jenness at Aug 20, 2000 at 7:48 pm

    On Sun, 20 Aug 2000, Chris Nandor wrote:
    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? :)
    System calls will return Perl epoch - it would be a bit confusing if some
    functions returned system epoch and others perl epoch. Your
    perl2epoch() command could be used to convert to 'system' epoch if
    required.
    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').
    Fine with me. I was just making use of the fact that time() has no
    arguments.

    --
    Tim Jenness
    JCMT software engineer/Support scientist
    http://www.jach.hawaii.edu/~timj

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupperl6-language-datetime @
categoriesperl
postedAug 20, '00 at 1:02a
activeAug 20, '00 at 7:48p
posts3
users2
websiteperl6.org

2 users in discussion

Tim Jenness: 2 posts Chris Nandor: 1 post

People

Translate

site design / logo © 2021 Grokbase