Class: Zakuro::Western::Calendar

Inherits:
Object
  • Object
show all
Defined in:
lib/zakuro/era/western/calendar.rb

Overview

Calendar 年月日情報(西暦)

このクラスでは以下の機能が求められる。

  1. グレゴリオ暦(yyyy, mm, dd) -> 日付オブジェクト

  2. ユリウス暦(yyyy, mm, dd) -> 日付オブジェクト

  3. 指定なし(yyyy, mm, dd) -> 日付オブジェクト

  4. 日付オブジェクト(グレゴリオ暦) -> グレゴリオ暦(yyyy, mm, dd)

  5. 日付オブジェクト(ユリウス暦) -> ユリウス暦(yyyy, mm, dd)

  6. 日付オブジェクト(指定なし) -> ユリウス暦/グレゴリオ暦(yyyy, mm, dd)

  • 3の “指定なし” とは、グレゴリオ暦開始日からを1、それ以前を2とする方式である

  • 6もまた上記に準じて日付を求める

  • それぞれ日付オブジェクトに変換する目的は、日付の加減算と比較のためである

これらの機能はRubyの標準機能であり、特別な実装を要しない

定数 DATE_START のバリエーションで日付オブジェクトを初期化するだけで良い

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(year: -4712,, month: 1, day: 1, type: Type::DEFAULT) ⇒ Calendar

初期化

Raises:

  • (ArgumentError)

    引数エラー



205
206
207
208
209
210
211
212
213
# File 'lib/zakuro/era/western/calendar.rb', line 205

def initialize(year: -4712, month: 1, day: 1, type: Type::DEFAULT)
  start = Western.to_native_start(type: type)
  @param = Parameter.new(year: year, month: month, day: day, start: start)

  failed = validate
  raise ArgumentError, failed.join('\n') unless failed.empty?

  @date = Date.new(year, month, day, start)
end

Instance Attribute Details

#dateDate (readonly)



141
142
143
# File 'lib/zakuro/era/western/calendar.rb', line 141

def date
  @date
end

#paramParameter (readonly)



139
140
141
# File 'lib/zakuro/era/western/calendar.rb', line 139

def param
  @param
end

Class Method Details

.create(date: Date.new) ⇒ Calendar

年月日情報(西暦)を生成する



390
391
392
393
394
# File 'lib/zakuro/era/western/calendar.rb', line 390

def create(date: Date.new)
  type = Western.to_type(start: date.start)
  Calendar.new(year: date.year, month: date.month,
               day: date.day, type: type)
end

.parse(text: '', type: Type::DEFAULT) ⇒ Calendar

年月日情報(西暦)を生成する

Raises:

  • (ArgumentError)

    引数エラー



406
407
408
409
410
411
412
413
414
415
# File 'lib/zakuro/era/western/calendar.rb', line 406

def parse(text: '', type: Type::DEFAULT)
  raise ArgumentError, "invalid date string: #{text}" unless valid_date_text(text: text)

  start = DATE_START.fetch(type, DATE_START[Type::DEFAULT])
  date = Date.parse(text, start)

  Calendar.new(
    year: date.year, month: date.month, day: date.day, type: type
  )
end

.valid_date_text(text: '') ⇒ True

日付文字列を検証する



426
427
428
# File 'lib/zakuro/era/western/calendar.rb', line 426

def valid_date_text(text: '')
  DateText.validate(text: text)
end

Instance Method Details

#+(other) ⇒ Calendar

加算する



241
242
243
244
245
246
# File 'lib/zakuro/era/western/calendar.rb', line 241

def +(other)
  return date.jd + other.date.jd if other.is_a?(Western::Calendar)

  @date += other
  self
end

#-(other) ⇒ Calendar

減算する



255
256
257
258
259
260
# File 'lib/zakuro/era/western/calendar.rb', line 255

def -(other)
  return date.jd - other.date.jd if other.is_a?(Western::Calendar)

  @date -= other
  self
end

#<(other) ⇒ True, False

大小比較する(<)



294
295
296
# File 'lib/zakuro/era/western/calendar.rb', line 294

def <(other)
  date < other.date
end

#<=(other) ⇒ True, False

大小比較する(<=)



306
307
308
# File 'lib/zakuro/era/western/calendar.rb', line 306

def <=(other)
  date <= other.date
end

#==(other) ⇒ True, False

大小比較する(==)



318
319
320
# File 'lib/zakuro/era/western/calendar.rb', line 318

def ==(other)
  date == other.date
end

#>(other) ⇒ True, False

大小比較する(>)



270
271
272
# File 'lib/zakuro/era/western/calendar.rb', line 270

def >(other)
  date > other.date
end

#>=(other) ⇒ True, False

大小比較する(>=)



282
283
284
# File 'lib/zakuro/era/western/calendar.rb', line 282

def >=(other)
  date >= other.date
end

#dayInteger

日を取得する



345
346
347
# File 'lib/zakuro/era/western/calendar.rb', line 345

def day
  date.day
end

#format(form: '%Y-%m-%d') ⇒ String

年月日をフォーマット化する



378
379
380
# File 'lib/zakuro/era/western/calendar.rb', line 378

def format(form: '%Y-%m-%d')
  date.strftime(form)
end

#invalid?True, False

無効値(引数なし)かどうかを検証する



367
368
369
# File 'lib/zakuro/era/western/calendar.rb', line 367

def invalid?
  (date == Date.new)
end

#monthInteger

月を取得する



336
337
338
# File 'lib/zakuro/era/western/calendar.rb', line 336

def month
  date.month
end

#next_year(num: 1) ⇒ Calendar

次年にする



356
357
358
359
# File 'lib/zakuro/era/western/calendar.rb', line 356

def next_year(num: 1)
  @date = date.next_year(num)
  self
end

#redate(type: Type::DEFAULT) ⇒ Calendar

初期化時の日付とは異なる種別に切り替える

Examples:

Ruby標準の start_with に相当する

> date = Date.new(1582, 10, 15)
=> #<Date: 1582-10-15 ((2299161j,0s,0n),+0s,2299161j)>
> date.new_start(Date::JULIAN)
=> #<Date: 1582-10-05 ((2299161j,0s,0n),+0s,Infj)>


228
229
230
231
232
# File 'lib/zakuro/era/western/calendar.rb', line 228

def redate(type: Type::DEFAULT)
  start = DATE_START.fetch(type, DATE_START[Type::DEFAULT])
  @date = date.new_start(start)
  self
end

#valid_dateArray<String>

日付データとして検証する



181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/zakuro/era/western/calendar.rb', line 181

def valid_date
  failed = []

  year = param.year
  month = param.month
  day = param.day
  start = param.start
  unless Date.valid_date?(year, month, day, start)
    failed.push("year: #{year}, month: #{month}, " \
                "day: #{day}, start: #{start}")
  end
  failed
end

#valid_typeArray<String>

データ型を検証する



163
164
165
166
167
168
169
170
171
172
# File 'lib/zakuro/era/western/calendar.rb', line 163

def valid_type
  failed = []
  year = param.year
  month = param.month
  day = param.day
  failed.push("wrong type. year: #{year}") unless year.is_a?(Integer)
  failed.push("wrong type. month: #{month}") unless month.is_a?(Integer)
  failed.push("wrong type. day: #{day}") unless day.is_a?(Integer)
  failed
end

#validateArray<String>

検証する



148
149
150
151
152
153
154
# File 'lib/zakuro/era/western/calendar.rb', line 148

def validate
  failed = valid_type

  return failed unless failed.empty?

  valid_date
end

#yearInteger

年を取得する



327
328
329
# File 'lib/zakuro/era/western/calendar.rb', line 327

def year
  date.year
end