class Zakuro::Western::Calendar

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)

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

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

Attributes

date[R]
param[R]

Public Class Methods

create(date: Date.new) click to toggle source

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

@param [Date] date Ruby標準日付

@return [Calendar] 年月日情報(西暦)

# File lib/zakuro/era/western.rb, line 375
def self.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
new(year: -4712, month: 1, day: 1, type: Type::DEFAULT) click to toggle source

初期化

@param [Integer] year 年 @param [Integer] month 月 @param [Integer] day 日 @param [Symbol] type 日付種別

# File lib/zakuro/era/western.rb, line 191
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.size.zero?

  @date = Date.new(year, month, day, start)
end
parse(str: '', type: Type::DEFAULT) click to toggle source

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

@param [String] str 日付文字列 @param [Symbol] type 日付種別

@return [Calendar] 年月日情報(西暦)

# File lib/zakuro/era/western.rb, line 389
def self.parse(str: '', type: Type::DEFAULT)
  unless Calendar.valid_date_string(str: str, type: type)
    raise ArgumentError, "invalid date string: #{str}"
  end

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

  Calendar.new(
    year: date.year, month: date.month, day: date.day, type: type
  )
end
valid_date_string(str: '', type: Type::DEFAULT) click to toggle source

日付文字列を検証する

@param [String] str 日付文字列 @param [Symbol] type 日付種別

@return [True] 正しい @return [True] 正しくない

# File lib/zakuro/era/western.rb, line 411
def self.valid_date_string(str: '', type: Type::DEFAULT)
  start = DATE_START.fetch(type, DATE_START[Type::DEFAULT])
  begin
    Date.parse(str, start)
  rescue ArgumentError => _e
    false
  end
  true
end

Public Instance Methods

+(other) click to toggle source

加算する

@param [Calendar,Integer] other 年月日情報(西暦),日数

@return [Calendar] 年月日情報(西暦)

# File lib/zakuro/era/western.rb, line 227
def +(other)
  return @date.jd + other.date.jd if other.is_a?(Western::Calendar)

  @date += other
  self
end
-(other) click to toggle source

減算する

@param [Calendar,Integer] other 年月日情報(西暦),日数

@return [Calendar] 年月日情報(西暦)

# File lib/zakuro/era/western.rb, line 241
def -(other)
  return @date.jd - other.date.jd if other.is_a?(Western::Calendar)

  @date -= other
  self
end
<(other) click to toggle source

大小比較する(<)

@param [Calendar] other 年月日情報(西暦)

@return [True] より小さい(過去日である) @return [False] 以上(現在日/未来日である)

# File lib/zakuro/era/western.rb, line 280
def <(other)
  @date < other.date
end
<=(other) click to toggle source

大小比較する(<=)

@param [Calendar] other 年月日情報(西暦)

@return [True] 以下(過去日/現在日である) @return [False] より大きい(未来日である)

# File lib/zakuro/era/western.rb, line 292
def <=(other)
  @date <= other.date
end
==(other) click to toggle source

大小比較する(==)

@param [Calendar] other 年月日情報(西暦)

@return [True] 等しい(現在日である) @return [False] 等しくない(過去日/未来日である)

# File lib/zakuro/era/western.rb, line 304
def ==(other)
  @date == other.date
end
>(other) click to toggle source

大小比較する(>)

@param [Calendar] other 年月日情報(西暦)

@return [True] より大きい(未来日である) @return [False] 以下(現在日/過去日である)

# File lib/zakuro/era/western.rb, line 256
def >(other)
  @date > other.date
end
>=(other) click to toggle source

大小比較する(>=)

@param [Calendar] other 年月日情報(西暦)

@return [True] 以上(現在日/未来日である) @return [False] より小さい(過去日である)

# File lib/zakuro/era/western.rb, line 268
def >=(other)
  @date >= other.date
end
day() click to toggle source

日を取得する

@return [Integer] 日

# File lib/zakuro/era/western.rb, line 331
def day
  @date.day
end
format(form: '%Y-%m-%d') click to toggle source

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

@param [String] form フォーマット

@return [String] 年月日情報

# File lib/zakuro/era/western.rb, line 364
def format(form: '%Y-%m-%d')
  @date.strftime(form)
end
invalid?() click to toggle source

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

@return [True] 無効値 @return [False] 無効値以外

# File lib/zakuro/era/western.rb, line 353
def invalid?
  (@date == Date.new)
end
month() click to toggle source

月を取得する

@return [Integer] 月

# File lib/zakuro/era/western.rb, line 322
def month
  @date.month
end
next_year(num: 1) click to toggle source

次年にする

@param [Integer] num 年数

@return [Calendar] 年月日情報(西暦)

# File lib/zakuro/era/western.rb, line 342
def next_year(num: 1)
  @date = @date.next_year(num)
  self
end
redate(type: Type::DEFAULT) click to toggle source

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

@example 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)>

@param [Symbol] type 日付種別

@return [Calendar] 年月日情報(西暦)

# File lib/zakuro/era/western.rb, line 214
def redate(type: Type::DEFAULT)
  start = DATE_START.fetch(type, DATE_START[Type::DEFAULT])
  @date = @date.new_start(start)
  self
end
valid_date() click to toggle source

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

@return [Array<String>] 不正メッセージ

# File lib/zakuro/era/western.rb, line 169
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_type() click to toggle source

データ型を検証する

@return [Array<String>] 不正メッセージ

# File lib/zakuro/era/western.rb, line 151
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
validate() click to toggle source
# File lib/zakuro/era/western.rb, line 136
def validate
  failed = valid_type

  return failed unless failed.size.zero?

  valid_date
end
year() click to toggle source

年を取得する

@return [Integer] 年

# File lib/zakuro/era/western.rb, line 313
def year
  @date.year
end