Last active
April 19, 2018 06:30
Ruby script to get a list of leap-second adjustment.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/local/bin/ruby | |
# coding: utf-8 | |
#--------------------------------------------------------------------------------- | |
#= Ruby script to get a list of leap-second adjustment. | |
# | |
# date name version | |
# 2016.07.25 mk-mode 1.00 新規作成 | |
# | |
# Copyright(C) 2016 mk-mode.com All Rights Reserved. | |
#--------------------------------------------------------------------------------- | |
# | |
require 'kconv' | |
require 'nokogiri' | |
require 'open-uri' | |
require 'pp' | |
class GetLeapcecNict | |
URL = "http://jjy.nict.go.jp/QandA/data/leapsec.html" | |
UA = "mk-mode Bot (by Ruby/#{RUBY_VERSION}, Administrator: postmaster@mk-mode.com)" | |
def exec | |
leapsecs = [[0, 1972, 1, 1, -10]] | |
begin | |
html = open(URL, "r:sjis", {"User-Agent" => UA}) { |f| f.read }.toutf8 | |
doc = Nokogiri::HTML.parse(html) | |
doc.xpath("//pre").text.split("\n").each do |line| | |
l = line.scan(/[\s ]+(.+?)[\s ]+(.+?)[\s ]*年[\s ]*(.+?)[\s ]*月[\s ]*(.+?)[\s ]*日.+?秒[\s ]*(.+?)[\s ]*秒$/)[0] | |
next unless l | |
leapsecs << l.map { |a| NKF::nkf("-WwZ1", a).to_i } | |
end | |
pp leapsecs.sort | |
rescue => e | |
msg = "[#{e.class}] #{e.message}\n" | |
msg << e.backtrace.map { |tr| "\t#{tr}\n" }.join("\n") | |
$stderr.puts msg | |
end | |
end | |
end | |
GetLeapcecNict.new.exec if __FILE__ == $0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment