Skip to content

Instantly share code, notes, and snippets.

@komasaru
Last active Apr 19, 2018
Embed
What would you like to do?
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