Class: Wareki::Date

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/wareki/date.rb

Overview

Wareki date handling class, main implementation.

Constant Summary collapse

FORMAT_DIRECTIVE_REGEX =
/%J(-|[_0]{0,2}[0-9]*|)([fFyYegGoOiImMsSlLdD][kK]?)/.freeze
FORMAT_EXPANSION_REGEX =
/(?<!%)(?:%%)*\K#{FORMAT_DIRECTIVE_REGEX}/.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(era_name, era_year, month = 1, day = 1, is_leap_month = false) ⇒ Date

Returns a new instance of Date.



81
82
83
84
85
86
87
88
89
# File 'lib/wareki/date.rb', line 81

def initialize(era_name, era_year, month = 1, day = 1, is_leap_month = false)
  @era_name = era_name.to_s
  @era_year = era_year
  @month = month
  @day = day
  @is_leap_month = is_leap_month
  @year = Utils.era_year_to_civil(@era_name, @era_year)
  _validate_date!
end

Instance Attribute Details

#dayObject

Returns the value of attribute day.



13
14
15
# File 'lib/wareki/date.rb', line 13

def day
  @day
end

#era_nameObject

Returns the value of attribute era_name.



13
14
15
# File 'lib/wareki/date.rb', line 13

def era_name
  @era_name
end

#era_yearObject

Returns the value of attribute era_year.



13
14
15
# File 'lib/wareki/date.rb', line 13

def era_year
  @era_year
end

#monthObject

Returns the value of attribute month.



13
14
15
# File 'lib/wareki/date.rb', line 13

def month
  @month
end

#yearObject

Returns the value of attribute year.



13
14
15
# File 'lib/wareki/date.rb', line 13

def year
  @year
end

Class Method Details

._parse(str) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/wareki/date.rb', line 19

def self._parse(str)
  str = str.to_s.gsub(/[[:space:]]/, '')
  match = REGEX.match(str)
  (match && !match[0].empty?) or
    raise ArgumentError, "Invaild Date: #{str}"
  era = match[:era_name]
  if (era.nil? || era == '') && match[:year].nil?
    year = Date.today.year
  else
    year = Utils.k2i(match[:year])
    year > 0 or raise ArgumentError, "Invalid year: #{str}"
  end
  month = day = 1

  era.to_s != '' && era.to_s != '紀元前' && !ERA_BY_NAME[era] and
    raise ArgumentError, "Date parse failed: Invalid era name '#{match[:era_name]}'"

  if match[:month]
    month = Utils.k2i(match[:month])
  elsif match[:alt_month]
    month = Utils.alt_month_name_to_i(match[:alt_month])
  end

  month > 12 || month < 1 and
    raise InvalidDate, "invalid date (month out of range): #{str}"

  is_leap = !!(match[:is_leap] || match[:is_leap_post])

  if match[:day]
    if match[:day] == ''
      civil_year = Utils.era_year_to_civil(era, year)
      day = Utils.last_day_of_era_month(era, civil_year, month, is_leap)
    else
      day = Utils.k2i(match[:day])
    end
  end

  {era: era, year: year, month: month, day: day, is_leap: is_leap}
end

.date(date) ⇒ Object



73
74
75
# File 'lib/wareki/date.rb', line 73

def self.date(date)
  jd(date.jd)
end

.imperial(year, month = 1, day = 1, is_leap_month = false) ⇒ Object



77
78
79
# File 'lib/wareki/date.rb', line 77

def self.imperial(year, month = 1, day = 1, is_leap_month = false)
  new('皇紀', year, month, day, is_leap_month)
end

.jd(d) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/wareki/date.rb', line 64

def self.jd(d)
  era = Utils.find_era(d)
  era or raise UnsupportedDateRange, "Cannot find era for date #{d.inspect}"
  year, month, day, is_leap = Utils.find_date_ary(d)
  obj = new(era.name, year - era.year + 1, month, day, is_leap)
  obj.__set_jd(d)
  obj
end

.parse(str) ⇒ Object



59
60
61
62
# File 'lib/wareki/date.rb', line 59

def self.parse(str)
  di = _parse(str)
  new(di[:era], di[:year], di[:month], di[:day], di[:is_leap])
end

.todayObject



15
16
17
# File 'lib/wareki/date.rb', line 15

def self.today
  jd(::Date.today.jd)
end

Instance Method Details

#+(other) ⇒ Object



314
315
316
317
318
# File 'lib/wareki/date.rb', line 314

def +(other)
  n = _to_days(other)
  n.nil? and raise TypeError, "Cannot add #{other.inspect} to Wareki::Date"
  self.class.jd(jd + n)
end

#-(other) ⇒ Object

Raises:

  • (TypeError)


306
307
308
309
310
311
312
# File 'lib/wareki/date.rb', line 306

def -(other)
  n = _to_days(other)
  n.nil? or return self.class.jd(jd - n)
  ojd = _jd_if_date_like(other)
  ojd and return jd - ojd
  raise TypeError, "Cannot subtract #{other.inspect} from Wareki::Date"
end

#<=>(other) ⇒ Object



295
296
297
298
299
300
# File 'lib/wareki/date.rb', line 295

def <=>(other)
  ojd = _jd_if_date_like(other)
  ojd = other if ojd.nil? && other.is_a?(Numeric)
  ojd.nil? and return nil
  jd <=> ojd
end

#===(other) ⇒ Object



282
283
284
285
286
287
288
289
# File 'lib/wareki/date.rb', line 282

def ===(other)
  begin
    other.jd == jd or return false
  rescue NoMethodError, NotImplementedError
    return false
  end
  true
end

#__set_jd(v) ⇒ Object



140
141
142
# File 'lib/wareki/date.rb', line 140

def __set_jd(v)
  @jd = v
end

#_jd_if_date_like(other) ⇒ Object



328
329
330
331
332
333
# File 'lib/wareki/date.rb', line 328

def _jd_if_date_like(other)
  return other.jd if other.respond_to?(:jd)

  other.respond_to?(:to_date) && !other.is_a?(Numeric) and other = other.to_date
  other.respond_to?(:jd) ? other.jd : nil
end

#_number_format(opt) ⇒ Object



214
215
216
# File 'lib/wareki/date.rb', line 214

def _number_format(opt)
  Utils.number_format(opt)
end

#_to_days(other) ⇒ Object



320
321
322
323
324
325
326
# File 'lib/wareki/date.rb', line 320

def _to_days(other)
  # rubocop:disable Style/ClassEqualityComparison
  return other.in_days if other.class.name == 'ActiveSupport::Duration'
  # rubocop:enable Style/ClassEqualityComparison

  other.is_a?(Numeric) ? other : nil
end

#_validate_date!Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/wareki/date.rb', line 157

def _validate_date!
  (month.is_a?(Integer) && month >= 1 && month <= 12) or
    raise InvalidDate, "invalid date (month out of range): #{inspect}"
  (day.is_a?(Integer) && day >= 1) or
    raise InvalidDate, "invalid date (day out of range): #{inspect}"
  if !WESTERN_ERA_NAMES.include?(@era_name) && @year < GREGORIAN_START_YEAR
    # 暦テーブル外の年は従来どおり jd 変換時の UnsupportedDateRange に委ねる
    Calendar.covers_year?(@year) or return
    !leap_month? || Calendar.leap_month(@year) == month or
      raise InvalidDate, "invalid date (no leap month): #{inspect}"
    day <= Calendar.last_day_of_month(@year, month, leap_month?) or
      raise InvalidDate, "invalid date (day out of range): #{inspect}"
  else
    leap_month? and
      raise InvalidDate, "invalid date (no leap month): #{inspect}"
    day <= last_day_of_month or
      raise InvalidDate, "invalid date (day out of range): #{inspect}"
  end
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


270
271
272
273
274
275
276
277
278
279
# File 'lib/wareki/date.rb', line 270

def eql?(other)
  begin
    %i[year month day era_year era_name leap_month?].each do |attr|
      other.public_send(attr) == public_send(attr) or return false
    end
  rescue NoMethodError, NotImplementedError
    return false
  end
  true
end

#expand_wareki_format(format_str) ⇒ Object



203
204
205
# File 'lib/wareki/date.rb', line 203

def expand_wareki_format(format_str)
  format_str.to_str.gsub(FORMAT_EXPANSION_REGEX) { format($2, $1) || $& }
end

#format(key, opt = '') ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/wareki/date.rb', line 218

def format(key, opt = '')
  case key.to_sym
  when :e  then era_name
  when :g  then era_name.to_s == '' ? '' : Kernel.format(_number_format(opt), era_year)
  when :G  then era_name.to_s == '' ? '' : Utils.i2z(era_year)
  when :Gk then era_name.to_s == '' ? '' : Utils.to_simple_kan(era_year)
  when :GK
    if era_name.to_s == ''
      ''
    elsif era_year == 1
      ''
    else
      Utils.to_simple_kan(era_year)
    end
  when :o  then year
  when :O  then Utils.i2z(year)
  when :Ok then Utils.to_simple_kan(year)
  when :i  then imperial_year
  when :I  then Utils.i2z(imperial_year)
  when :Ik then Utils.to_simple_kan(imperial_year)
  when :s  then Kernel.format(_number_format(opt), month)
  when :S  then Utils.i2z(month)
  when :Sk then Utils.to_simple_kan(month)
  when :SK then Utils.alt_month_name(month)
  when :l  then leap_month? ? "'" : ''
  when :L  then leap_month? ? '' : ''
  when :Lk then leap_month? ? '' : ''
  when :d  then Kernel.format(_number_format(opt), day)
  when :D  then Utils.i2z(day)
  when :Dk then Utils.to_simple_kan(day)
  when :DK
    if month == 1 && !leap_month? && day == 1
      ''
    elsif day == 1
      ''
    elsif day == last_day_of_month
      ''
    else
      Utils.to_simple_kan(day)
    end
  when :m  then "#{format(:s, opt)}#{format(:l)}"
  when :M  then "#{format(:Lk)}#{format(:S)}"
  when :Mk then "#{format(:Lk)}#{format(:Sk)}"
  when :y  then "#{format(:e)}#{format(:g, opt)}"
  when :Y  then "#{format(:e)}#{format(:G)}"
  when :Yk then "#{format(:e)}#{format(:Gk)}"
  when :YK then "#{format(:e)}#{format(:GK)}"
  when :f  then "#{format(:e)}#{format(:g, opt)}#{format(:s, opt)}#{format(:l)}#{format(:d, opt)}"
  when :F  then "#{format(:e)}#{format(:GK)}#{format(:Lk)}#{format(:Sk)}#{format(:Dk)}"
  end
end

#hashObject



291
292
293
# File 'lib/wareki/date.rb', line 291

def hash
  [self.class, @era_name, @era_year, @year, @month, @day, leap_month?].hash
end

#imperial_yearObject



91
92
93
# File 'lib/wareki/date.rb', line 91

def imperial_year
  @year - IMPERIAL_START_YEAR
end

#imperial_year=(v) ⇒ Object



95
96
97
# File 'lib/wareki/date.rb', line 95

def imperial_year=(v)
  self.year = v + IMPERIAL_START_YEAR
end

#jdObject



177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/wareki/date.rb', line 177

def jd
  @jd and return @jd

  _validate_date!
  WESTERN_ERA_NAMES.include?(@era_name) and
    return @jd = ::Date.new(@year, month, day, ::Date::ITALY).jd

  @year >= GREGORIAN_START_YEAR and
    return @jd = ::Date.new(@year, month, day, ::Date::GREGORIAN).jd

  @jd = Calendar.to_jd(@year, month, day, leap_month?) or
    raise UnsupportedDateRange, "Cannot convert to jd #{inspect}"
  @jd
end

#last_day_of_monthObject



153
154
155
# File 'lib/wareki/date.rb', line 153

def last_day_of_month
  Utils.last_day_of_era_month(@era_name, @year, month, leap_month?)
end

#leap_month=(v) ⇒ Object



103
104
105
106
# File 'lib/wareki/date.rb', line 103

def leap_month=(v)
  @jd = nil
  @is_leap_month = v
end

#leap_month?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/wareki/date.rb', line 99

def leap_month?
  !!@is_leap_month
end

#month_indexObject



144
145
146
147
148
149
150
151
# File 'lib/wareki/date.rb', line 144

def month_index
  return month - 1 if
    WESTERN_ERA_NAMES.include?(@era_name) || @year >= GREGORIAN_START_YEAR

  Calendar.covers_year?(@year) or
    raise UnsupportedDateRange, "Cannot get year info of #{inspect}"
  Calendar.month_index(@year, month, leap_month?)
end

#strftime(format_str = '%JF') ⇒ Object



207
208
209
210
211
212
# File 'lib/wareki/date.rb', line 207

def strftime(format_str = '%JF')
  ret = expand_wareki_format(format_str)
  ret.index('%') or return ret
  d = to_date
  d.respond_to?(:_wareki_strftime_orig) ? d._wareki_strftime_orig(ret) : d.strftime(ret)
end

#succObject



302
303
304
# File 'lib/wareki/date.rb', line 302

def succ
  self + 1
end

#to_date(start = ::Date::ITALY) ⇒ Object



192
193
194
# File 'lib/wareki/date.rb', line 192

def to_date(start = ::Date::ITALY)
  ::Date.jd(jd, start)
end

#to_timeObject



196
197
198
# File 'lib/wareki/date.rb', line 196

def to_time
  to_date.to_time
end