Skip to content

Instantly share code, notes, and snippets.

@komasaru
Last active April 19, 2018 06:16
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/2d5e95a70bf6accb42e6f1140a746177 to your computer and use it in GitHub Desktop.
Save komasaru/2d5e95a70bf6accb42e6f1140a746177 to your computer and use it in GitHub Desktop.
Ruby script to calculate a Japan's calendar with a selfmade gem library.
#! /usr/local/bin/ruby
# coding: utf-8
require 'date'
require 'mk_calendar'
class Calendar
USAGE = "[USAGE] .calendar.rb [YYYYMMDD]"
MSG_ERR = "[ERROR] Invalid date!"
def initialize
@date = ARGV.shift
@date ||= Time.now.strftime("%Y%m%d")
unless @date =~ /\d{8}/
puts USAGE
exit 0
end
unless Date.valid_date?(@date[0,4].to_i, @date[4,2].to_i, @date[6,2].to_i)
puts MSG_ERR
exit 0
end
end
def calc
@obj = MkCalendar.new(@date)
str = sprintf("%04d-%02d-%02d", @obj.year, @obj.month, @obj.day)
str << " #{@obj.yobi}曜日"
str << " #{@obj.holiday}" unless @obj.holiday == ""
str << " #{@obj.jd}UTC(#{@obj.jd_jst}JST) #{@obj.kanshi} "
str << sprintf("%04d-%02d-%02d", @obj.oc[0], @obj.oc[2], @obj.oc[3])
str << "(閏)" if @obj.oc[1] == 1
str << " #{@obj.oc[4]}"
str << " #{@obj.sekki_24}" unless @obj.sekki_24 == ""
str << " #{@obj.zassetsu}" unless @obj.zassetsu == ""
str << " #{@obj.sekku}" unless @obj.sekku == ""
str << " #{@obj.lambda_sun} #{@obj.lambda_moon} #{@obj.moonage}"
puts str
rescue => e
$stderr.puts "[#{e.class}] #{e.message}"
e.backtrace.each { |tr| $stderr.puts "\t#{tr}"}
exit 1
end
end
Calendar.new.calc if __FILE__ == $0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment