Module: Wareki::Utils

Defined in:
lib/wareki/utils.rb

Overview

Static utility methods.

Class Method Summary collapse

Class Method Details

._last_day_of_month_from_defs(year, month, is_leap) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/wareki/utils.rb', line 26

def _last_day_of_month_from_defs(year, month, is_leap)
  yobj = YEAR_BY_NUM[year] or
    raise UnsupportedDateRange, "Cannot find year #{inspect}"
  month_idx = month - 1
  month_idx += 1 if is_leap || yobj.leap_month && yobj.leap_month < month
  yobj.month_days[month_idx]
end

._last_day_of_month_gregorian(year, month) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/wareki/utils.rb', line 14

def _last_day_of_month_gregorian(year, month)
  tmp_y = year
  tmp_m = month
  if month == 12
    tmp_y += 1
    tmp_m = 1
  else
    tmp_m += 1
  end
  (::Date.new(tmp_y, tmp_m, 1, ::Date::GREGORIAN) - 1).day
end

._to_date(d) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/wareki/utils.rb', line 43

def _to_date(d)
  if d.is_a? ::Date
    d # nothing to do
  elsif d.is_a?(Time)
    d.to_date
  else
    ::Date.jd(d.to_i)
  end
end

._to_jd(d) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/wareki/utils.rb', line 53

def _to_jd(d)
  if d.is_a? ::Date
    d.jd
  elsif d.is_a?(Time)
    d.to_date.jd
  else
    d.to_i
  end
end

.alt_month_name(month) ⇒ Object



39
40
41
# File 'lib/wareki/utils.rb', line 39

def alt_month_name(month)
  ALT_MONTH_NAME[month - 1]
end

.alt_month_name_to_i(name) ⇒ Object



34
35
36
37
# File 'lib/wareki/utils.rb', line 34

def alt_month_name_to_i(name)
  i = ALT_MONTH_NAME.index(name) or return false
  i + 1
end

.find_date_ary(d) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/wareki/utils.rb', line 63

def find_date_ary(d)
  d = _to_date(d).new_start(::Date::GREGORIAN)
  d.jd >= GREGORIAN_START and
    return [d.year, d.month, d.day, false]

  yobj = find_year(d) or raise UnsupportedDateRange, "Unsupported date: #{d.inspect}"
  month = 0
  if yobj.month_starts.last <= d.jd
    month = yobj.month_starts.count
  else
    month = yobj.month_starts.find_index { |m| d.jd <= (m - 1) }
  end
  month_start = yobj.month_starts[month - 1]
  is_leap = (yobj.leap_month == (month - 1))
  yobj.leap_month && yobj.leap_month < month and
    month -= 1
  [yobj.year, month, d.jd - month_start + 1, is_leap]
end

.find_era(d) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'lib/wareki/utils.rb', line 87

def find_era(d)
  jd = _to_jd(d)
  ERA_DEFS.reverse_each do |e|
    e.start > jd and next
    e.end < jd and next
    return e
  end
  nil
end

.find_year(d) ⇒ Object



82
83
84
85
# File 'lib/wareki/utils.rb', line 82

def find_year(d)
  jd = _to_jd(d)
  YEAR_DEFS.bsearch { |y| y.end >= jd }
end

.last_day_of_month(year, month, is_leap) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/wareki/utils.rb', line 6

def last_day_of_month(year, month, is_leap)
  if year >= GREGORIAN_START_YEAR
    _last_day_of_month_gregorian(year, month)
  else
    _last_day_of_month_from_defs(year, month, is_leap)
  end
end