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



30
31
32
33
34
35
36
# File 'lib/wareki/utils.rb', line 30

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



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/wareki/utils.rb', line 18

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



47
48
49
50
51
52
53
54
55
# File 'lib/wareki/utils.rb', line 47

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



57
58
59
60
61
62
63
64
65
# File 'lib/wareki/utils.rb', line 57

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



43
44
45
# File 'lib/wareki/utils.rb', line 43

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

.alt_month_name_to_i(name) ⇒ Object



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

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

.find_date_ary(d) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/wareki/utils.rb', line 67

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



91
92
93
94
95
96
97
98
99
# File 'lib/wareki/utils.rb', line 91

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



86
87
88
89
# File 'lib/wareki/utils.rb', line 86

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

.i2z(num) ⇒ Object



101
102
103
# File 'lib/wareki/utils.rb', line 101

def i2z(num)
  num.to_s.tr('0123456789', '0123456789')
end

.i_to_kan(*args) ⇒ Object

DEPRECATED



121
122
123
124
# File 'lib/wareki/utils.rb', line 121

def i_to_kan(*args)
  warn '[DEPRECATED] Wareki::Utils.i_to_kan: Please use ya_kansuji gem to handle kansuji'
  i2k(*args)
end

.k2i(str) ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/wareki/utils.rb', line 105

def k2i(str)
  str = str.to_s.strip
  if %w(  ).member? str
    1
  else
    YaKansuji.to_i str
  end
end

.kan_to_i(*args) ⇒ Object

DEPRECATED



115
116
117
118
# File 'lib/wareki/utils.rb', line 115

def kan_to_i(*args)
  warn '[DEPRECATED] Wareki::Utils.kan_to_i: Please use ya_kansuji gem to handle kansuji'
  k2i(*args)
end

.last_day_of_month(year, month, is_leap) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/wareki/utils.rb', line 10

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