Skip to content

Instantly share code, notes, and snippets.

@komasaru
Last active January 30, 2020 12:11
Show Gist options
  • Save komasaru/5c4c74071508a543088e to your computer and use it in GitHub Desktop.
Save komasaru/5c4c74071508a543088e to your computer and use it in GitHub Desktop.
Ruby script to calc remaining seconds to the date of Problem-2038.
#! /usr/local/bin/ruby
#
# 2038年問題までの残り時間計算
# ( 2038年1月19日3時14分08秒 までの残り秒数 )
#
# date name version
# 2011.10.06 Masaru Koizumi 1.00 新規作成
# 2015.01.03 Masaru Koizumi 1.01 Shebang string 追加等
# 2020.01.30 Masaru Koizumi 1.02 ソース整形
#
# Copyright(C) 2011-2020 mk-mode.com All Rights Reserved.
#---------------------------------------------------------------------------------
# @param: [YYYYMMDDHHMMSS]
#---------------------------------------------------------------------------------
# 2019-01-15 01:34:08 で「残り 600,000,000 秒」
# 2020-02-01 00:14:08 で「残り 567,000,000 秒」
#++
require 'time'
class Problem2038
DT_2038 = "2038-01-19 12:14:08"
def initialize
@tm_2038 = Time.parse(DT_2038).strftime("%Y-%m-%d %H:%M:%S")
@sec_2038 = Time.parse(DT_2038).to_i
tm = Time.now
tm = Time.strptime(ARGV[0], "%Y%m%d%H%M%S") if ARGV[0]
@tm_target = tm.strftime("%Y-%m-%d %H:%M:%S")
@sec_target = tm.to_i
end
def calc
sec_remain = @sec_2038 - @sec_target
str = "PROBLEM: #{@tm_2038}\n"
str << " %13s" % comma(@sec_2038)
str << "( %032d ) secs.\n" % @sec_2038.to_s(2)
str << " TARGET: #{@tm_target}\n"
str << " %13s" % comma(@sec_target)
str << "( %032d ) secs.\n" % @sec_target.to_s(2)
str << "REMAING:\n"
str << " %13s" % comma(sec_remain)
str << "( %032d ) secs." % sec_remain.to_s(2)
puts str
rescue => e
$stderr.puts "[EXCEPTION][#{self.class.name}.#{__method__}] #{e}"
exit 1
end
private
# カンマ挿入
def comma(str)
return str.to_s.gsub(/(\d)(?=(\d{3})+(?!\d))/, '\1,')
end
end
Problem2038.new.calc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment