Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added a Perl 5 script to update the leap-second tables in tai-utc.pm.
I tried to do it in Perl 6 first, but I couldn't get perl6-lwp-simple to work.
  • Loading branch information
Kodi Arfer authored and Kodi Arfer committed Jan 2, 2011
1 parent db11af9 commit fc2db18
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 24 deletions.
53 changes: 29 additions & 24 deletions src/core/tai-utc.pm
@@ -1,7 +1,8 @@
use v6;

# This file keeps track of the differences between TAI and UTC
# for internal use.
# 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
Expand All @@ -15,6 +16,7 @@ module tai-utc {

# our @leap-second-dates = <
our sub leap-second-dates() { <
#BEGIN leap-second-dates
1972-06-30
1972-12-31
1973-12-31
Expand All @@ -39,6 +41,7 @@ module tai-utc {
1998-12-31
2005-12-31
2008-12-31
#END leap-second-dates
> };

# our %leap-seconds =
Expand All @@ -50,30 +53,32 @@ module tai-utc {

# 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
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
> };

};
47 changes: 47 additions & 0 deletions tools/update-tai-utc.pl
@@ -0,0 +1,47 @@
#!/usr/bin/perl
# Updates src/core/tai-utc.pm.

use warnings;
use strict;
use Time::y2038 'timegm';
use File::Slurp qw(slurp write_file);
use LWP::Simple 'get';

my $url = 'ftp://hpiers.obspm.fr/iers/bul/bulc/TimeSteps.history';

$ARGV[0] or die "Please provide a path to src/core/tai-utc.pm.\n";
my $tu_path = $ARGV[0];

my @dates = do {
my @lines = split /\n/, get $url;
pop @lines;
shift @lines until $lines[0] =~ /\A 1972 Jul\. 1/;
map {
/(\d{4}) (Jan|Jul)/;
$2 eq 'Jan' ? [$1 - 1, 12, 31] : [$1, 6, 30]
} @lines
};

my $tu = slurp $tu_path;
sub replace {
my ($find, $fmt, $f) = @_;
$tu =~ s
{^( *)#BEGIN $find\n.+?^ *#END $find\n}
{ sprintf "$1#BEGIN $find\n%s\n$1#END $find\n", join "\n",
map { sprintf "%s$fmt", $1, $f->(@$_) } @dates }ems
or die "Couldn't replace $find";
}
replace 'leap-second-dates', '%d-%02d-%02d', sub { @_ };
replace 'leap-second-posix', '%10d', sub {
my ($y, $m, $d) = @_;
1 + timegm 59, 59, 23, $d, $m - 1, $y - 1900;
};
write_file $tu_path, $tu;
print "Updated.\n";

# The IERS announces midyear leap seconds in early January and
# end-of-year leap seconds in early July. So:

my $month = (gmtime)[4];
printf "This program should next be run in %s.\n",
1 < $month && $month < 8 ? 'August' : 'February';

0 comments on commit fc2db18

Please sign in to comment.