Skip to content

Instantly share code, notes, and snippets.

@komasaru
Last active April 19, 2018 06:30

Revisions

  1. komasaru revised this gist Apr 19, 2018. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions get_leapsec_nict.rb
    Original file line number Diff line number Diff line change
    @@ -38,5 +38,4 @@ def exec
    end
    end

    exit 0 unless __FILE__ == $0
    GetLeapcecNict.new.exec
    GetLeapcecNict.new.exec if __FILE__ == $0
  2. komasaru revised this gist Jul 25, 2016. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion get_leapsec_nict.rb
    Original file line number Diff line number Diff line change
    @@ -12,6 +12,7 @@
    require 'kconv'
    require 'nokogiri'
    require 'open-uri'
    require 'pp'

    class GetLeapcecNict
    URL = "http://jjy.nict.go.jp/QandA/data/leapsec.html"
    @@ -28,7 +29,7 @@ def exec
    next unless l
    leapsecs << l.map { |a| NKF::nkf("-WwZ1", a).to_i }
    end
    p leapsecs.sort
    pp leapsecs.sort
    rescue => e
    msg = "[#{e.class}] #{e.message}\n"
    msg << e.backtrace.map { |tr| "\t#{tr}\n" }.join("\n")
  3. komasaru created this gist Jul 25, 2016.
    41 changes: 41 additions & 0 deletions get_leapsec_nict.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    #! /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'

    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
    p 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

    exit 0 unless __FILE__ == $0
    GetLeapcecNict.new.exec