Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #for RubyPico (mruby)
- #mrubyには String#center がないので適当に実装
- class String
- def center(n)
- i = (n - length).div(2)
- " " * i + self + " " * (n - i - length)
- end
- end
- month_table = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
- #うるう年か?
- is_uruu = ->(year) {
- (year % 4 == 0 and year % 100 != 0) or year % 400 == 0
- }
- #西暦1年1月1日から何日目か(すべてグレゴリオ暦で計算)
- days = ->(year, month, day) {
- uruu = ->(y) {
- y.div(4) - y.div(100) + y.div(400)
- }
- month_days = ->(mth) {
- month_table[0, mth].inject(&:+) + (is_uruu[year] && mth > 2 ? 1 : 0)
- }
- y1 = year - 1
- y1 * 365 + uruu[y1] + month_days[month] + day - 1
- }
- #曜日の計算
- week_number = ->(year, month, day) {
- (days[year, month, day] + 1) % 7
- }
- #カレンダーの出力
- Calender = ->(year, month) {
- gen = ->(from, to) {
- st = ""
- from.upto(to) do |i|
- st += sprintf("%2d ", i)
- st = st[0..-2] if i < 10 #mrubyのバグ? とりあえずパッチ。
- end
- st
- }
- putout = ->(i) {
- last = month_table[month]
- last += 1 if is_uruu[year] and month == 2
- while i + 6 <= last
- puts gen[i, i + 6]
- i += 7
- end
- st = gen[i, last]
- puts st unless st.empty?
- }
- puts "#{year}/#{month}".center(27)
- puts "sun mon tue wed thu fri sat"
- w = week_number[year, month, 1]
- puts " " * w + gen[1, 7 - w]
- putout[8 - w]
- }
- year, month = Popup.input("年と月を入力して下さい(スペースで区切る)").split.map(&:to_i)
- Calender[year, month]
Add Comment
Please, Sign In to add comment