Skip to content

Instantly share code, notes, and snippets.

@komasaru
Last active April 19, 2018 06:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save komasaru/027516ea3fe4f09878e7446f0f23444f to your computer and use it in GitHub Desktop.
Save komasaru/027516ea3fe4f09878e7446f0f23444f to your computer and use it in GitHub Desktop.
Ruby script to get a list of leap-second adjustment.
#! /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