require 'net/http' require 'uri' require 'time' BASE = 'https://www.wis-jma.go.jp/d/o' TIMEOUT = 120 if ARGV.empty? warn "Usage: ruby script.rb " warn " path = CCCC/type/cat/subc like VHHH/Alphanumeric/Surface/SYNOP" exit 1 end path = ARGV[0] ENV['TZ']='UTC' # UTC日付 date_str = Time.now.utc.strftime('%Y%m%d') url = "#{BASE}/#{path}/#{date_str}/" uri = URI.parse(url) begin http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true http.open_timeout = TIMEOUT http.read_timeout = TIMEOUT res = http.get(uri.request_uri) unless res.code.to_i == 200 warn "HTTP Error: #{res.code}" exit 1 end body = res.body # 例: 2026-06-10 09:42 regex = /(\d{4}-\d{2}-\d{2} \d{2}:\d{2})/ times = [] body.each_line do |line| if line =~ regex begin times << Time.strptime($1, '%Y-%m-%d %H:%M').utc rescue # 無視 end end end if times.empty? warn "No timestamps found" puts "535" exit 1 end latest = times.max now = Time.now.utc diff = now - latest # 指定フォーマットで出力 puts "%6.3f" % diff rescue Net::OpenTimeout warn "OpenTimeout" puts "505" exit 1 rescue Net::ReadTimeout warn "ReadTimeout" puts "515" exit 1 rescue => e warn "Error: #{e.message}" puts "525" exit 1 end