Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

start to put back tai-utc and Instant (Duration still missing)

  • Loading branch information...
commit f998cbb9f3cd38acf0dfe5f7518af1ef9f88e7bd 1 parent b1faa05
@moritz moritz authored
Showing with 199 additions and 0 deletions.
  1. +111 −0 src/core/Instant.pm
  2. +86 −0 src/core/tai-utc.pm
  3. +2 −0  tools/build/Makefile.in
View
111 src/core/Instant.pm
@@ -0,0 +1,111 @@
+use v6;
+
+my class Instant is Real {
+ has Rat $.x;
+ # A linear count of seconds since 1970-01-01T00:00:00Z, plus
+ # tai-utc::initial-offset. Thus, $.x matches TAI from 1970
+ # to the present.
+
+ method new($x) { self.bless: *, x => $x.Rat }
+
+ method from-posix($posix, Bool $prefer-leap-second = False) {
+ # $posix is in general not expected to be an integer.
+ # If $prefer-leap-second is true, 915148800 is interpreted to
+ # mean 1998-12-31T23:59:60Z rather than 1999-01-01T00:00:00Z.
+ my $p = floor $posix;
+ my $offset = tai-utc::initial-offset;
+ for tai-utc::leap-second-posix() {
+ if $_ < $p {
+ ++$offset;
+ } else {
+ return self.new: $posix + $offset + do
+ $_ == $p && !$prefer-leap-second
+ }
+ }
+ self.new: $posix + $offset;
+ }
+
+ method to-posix() {
+ # The inverse of .from-posix, except that the second return
+ # value is true if *and only if* this Instant is in a leap
+ # second.
+ my $n = floor $.x;
+ my $offset = tai-utc::initial-offset;
+ for tai-utc::leap-second-posix() {
+ if $_ < $n - $offset {
+ ++$offset;
+ } else {
+ return ($.x - $offset, $n - $offset == $_)
+ }
+ }
+ ($.x - $offset, False)
+ }
+
+# method Str() {
+# 'Instant:' ~ default-formatter
+# ::DateTime.new(self), :subseconds
+# }
+#
+# method perl() {
+# sprintf '(DateTime.new(year => 1970).Instant + %s)',
+# ($.x - tai-utc::initial-offset).perl
+# }
+}
+
+multi sub infixcmp»(Instant $a, Instant $b) {
+ $a.x <=> $b.x
+}
+
+multi sub infix:«<=>»(Instant $a, Instant $b) {
+ $a.x <=> $b.x
+}
+
+multi sub infix:«==»(Instant $a, Instant $b) {
+ $a.x == $b.x
+}
+
+multi sub infix:«!=»(Instant $a, Instant $b) {
+ $a.x != $b.x
+}
+
+multi sub infix:«<»(Instant $a, Instant $b) {
+ $a.x < $b.x
+}
+
+multi sub infix:«>»(Instant $a, Instant $b) {
+ $a.x > $b.x
+}
+
+multi sub infix:«<=»(Instant $a, Instant $b) {
+ $a.x <= $b.x
+}
+
+multi sub infix:«>=»(Instant $a, Instant $b) {
+ $a.x >= $b.x
+}
+
+multi sub infix:<+>(Instant $a, Real $b) {
+ Instant.new: $a.x + $b;
+}
+multi sub infix:<+>(Real $a, Instant $b) {
+ Instant.new: $a + $b.x;
+}
+# multi sub infix:<+>(Instant $a, Duration $b) {
+# Instant.new: $a.x + $b.x;
+# }
+# multi sub infix:<+>(Duration $a, Instant $b) {
+# Instant.new: $a.x + $b.x;
+# }
+#
+# multi sub infix:<->(Instant $a, Instant $b) {
+# Duration.new: $a.x - $b.x;
+# }
+multi sub infix:<->(Instant $a, Real $b) {
+ Instant.new: $a.x - $b;
+}
+
+sub term:<now>() {
+ # FIXME: During a leap second, the returned value is one
+ # second greater than it should be.
+ Instant.from-posix: pir::time__n
+}
View
86 src/core/tai-utc.pm
@@ -0,0 +1,86 @@
+# This file keeps track of the differences between TAI and UTC
+# for internal use. The "BEGIN" and "END" comments are for
+# tools/update-tai-utc.pl.
+
+# Some handy tables:
+# http://tf.nist.gov/pubs/bulletin/leapsecond.htm
+# http://hpiers.obspm.fr/eop-pc/earthor/utc/TAI-UTC_tab.html
+
+my module tai-utc {
+
+ #our $initial-offset = 10;
+ our sub initial-offset() { 10 }
+ # TAI - UTC at the Unix epoch (1970-01-01T00:00:00Z).
+
+ # our @leap-second-dates = <
+ our sub leap-second-dates() {
+ #BEGIN leap-second-dates
+ <
+ 1972-06-30
+ 1972-12-31
+ 1973-12-31
+ 1974-12-31
+ 1975-12-31
+ 1976-12-31
+ 1977-12-31
+ 1978-12-31
+ 1979-12-31
+ 1981-06-30
+ 1982-06-30
+ 1983-06-30
+ 1985-06-30
+ 1987-12-31
+ 1989-12-31
+ 1990-12-31
+ 1992-06-30
+ 1993-06-30
+ 1994-06-30
+ 1995-12-31
+ 1997-06-30
+ 1998-12-31
+ 2005-12-31
+ 2008-12-31
+ >
+ #END leap-second-dates
+ };
+
+ # our %leap-seconds =
+ # @leap-second-dates Z=> $initial-offset + 1 .. *;
+
+ # So for any date $d in @leap-second-dates, $d 23:59:00 UTC
+ # is the leap second that made (or will make) UTC
+ # %leap-seconds{$d} seconds behind TAI.
+
+ # Ambiguous POSIX times.
+ our sub leap-second-posix() {
+ #BEGIN leap-second-posix
+ <
+ 78796800
+ 94694400
+ 126230400
+ 157766400
+ 189302400
+ 220924800
+ 252460800
+ 283996800
+ 315532800
+ 362793600
+ 394329600
+ 425865600
+ 489024000
+ 567993600
+ 631152000
+ 662688000
+ 709948800
+ 741484800
+ 773020800
+ 820454400
+ 867715200
+ 915148800
+ 1136073600
+ 1230768000
+ >
+ #END leap-second-posix
+ };
+
+};
View
2  tools/build/Makefile.in
@@ -191,6 +191,8 @@ CORE_SOURCES = \
src/core/Cursor.pm \
src/core/Grammar.pm \
src/core/Regex.pm \
+ src/core/tai-utc.pm \
+ src/core/Instant.pm \
src/core/EXPORTHOW.pm \
src/core/Pod.pm \
src/core/operators.pm \
Please sign in to comment.
Something went wrong with that request. Please try again.