Guest User

Untitled

a guest
Apr 20th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. #for RubyPico (mruby)
  2.  
  3. #mrubyには String#center がないので適当に実装
  4. class String
  5. def center(n)
  6. i = (n - length).div(2)
  7. " " * i + self + " " * (n - i - length)
  8. end
  9. end
  10.  
  11. month_table = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
  12.  
  13. #うるう年か?
  14. is_uruu = ->(year) {
  15. (year % 4 == 0 and year % 100 != 0) or year % 400 == 0
  16. }
  17.  
  18. #西暦1年1月1日から何日目か(すべてグレゴリオ暦で計算)
  19. days = ->(year, month, day) {
  20. uruu = ->(y) {
  21. y.div(4) - y.div(100) + y.div(400)
  22. }
  23. month_days = ->(mth) {
  24. month_table[0, mth].inject(&:+) + (is_uruu[year] && mth > 2 ? 1 : 0)
  25. }
  26. y1 = year - 1
  27. y1 * 365 + uruu[y1] + month_days[month] + day - 1
  28. }
  29.  
  30. #曜日の計算
  31. week_number = ->(year, month, day) {
  32. (days[year, month, day] + 1) % 7
  33. }
  34.  
  35. #カレンダーの出力
  36. Calender = ->(year, month) {
  37. gen = ->(from, to) {
  38. st = ""
  39. from.upto(to) do |i|
  40. st += sprintf("%2d ", i)
  41. st = st[0..-2] if i < 10 #mrubyのバグ? とりあえずパッチ。
  42. end
  43. st
  44. }
  45. putout = ->(i) {
  46. last = month_table[month]
  47. last += 1 if is_uruu[year] and month == 2
  48. while i + 6 <= last
  49. puts gen[i, i + 6]
  50. i += 7
  51. end
  52. st = gen[i, last]
  53. puts st unless st.empty?
  54. }
  55. puts "#{year}/#{month}".center(27)
  56. puts "sun mon tue wed thu fri sat"
  57. w = week_number[year, month, 1]
  58. puts " " * w + gen[1, 7 - w]
  59. putout[8 - w]
  60. }
  61.  
  62. year, month = Popup.input("年と月を入力して下さい(スペースで区切る)").split.map(&:to_i)
  63. Calender[year, month]
Add Comment
Please, Sign In to add comment