class Zakuro::Western::Calendar
Calendar
年月日情報(西暦)
このクラスでは以下の機能が求められる。
-
グレゴリオ暦(yyyy, mm, dd) -> 日付オブジェクト
-
ユリウス暦(yyyy, mm, dd) -> 日付オブジェクト
-
指定なし(yyyy, mm, dd) -> 日付オブジェクト
-
日付オブジェクト(グレゴリオ暦) -> グレゴリオ暦(yyyy, mm, dd)
-
日付オブジェクト(ユリウス暦) -> ユリウス暦(yyyy, mm, dd)
-
日付オブジェクト(指定なし) -> ユリウス暦/グレゴリオ暦(yyyy, mm, dd)
-
3の “指定なし” とは、グレゴリオ暦開始日からを1、それ以前を2とする方式である
-
6もまた上記に準じて日付を求める
-
それぞれ日付オブジェクトに変換する目的は、日付の加減算と比較のためである
これらの機能はRubyの標準機能であり、特別な実装を要しない
定数 DATE_START のバリエーションで日付オブジェクトを初期化するだけで良い
Attributes
Public Class Methods
年月日情報(西暦)を生成する
@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
初期化
@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
年月日情報(西暦)を生成する
@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
日付文字列を検証する
@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
加算する
@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
減算する
@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
大小比較する(<)
@param [Calendar] other 年月日情報(西暦)
@return [True] より小さい(過去日である) @return [False] 以上(現在日/未来日である)
# File lib/zakuro/era/western.rb, line 280 def <(other) @date < other.date end
大小比較する(<=)
@param [Calendar] other 年月日情報(西暦)
@return [True] 以下(過去日/現在日である) @return [False] より大きい(未来日である)
# File lib/zakuro/era/western.rb, line 292 def <=(other) @date <= other.date end
大小比較する(==)
@param [Calendar] other 年月日情報(西暦)
@return [True] 等しい(現在日である) @return [False] 等しくない(過去日/未来日である)
# File lib/zakuro/era/western.rb, line 304 def ==(other) @date == other.date end
大小比較する(>)
@param [Calendar] other 年月日情報(西暦)
@return [True] より大きい(未来日である) @return [False] 以下(現在日/過去日である)
# File lib/zakuro/era/western.rb, line 256 def >(other) @date > other.date end
大小比較する(>=)
@param [Calendar] other 年月日情報(西暦)
@return [True] 以上(現在日/未来日である) @return [False] より小さい(過去日である)
# File lib/zakuro/era/western.rb, line 268 def >=(other) @date >= other.date end
日を取得する
@return [Integer] 日
# File lib/zakuro/era/western.rb, line 331 def day @date.day end
年月日をフォーマット化する
@param [String] form フォーマット
@return [String] 年月日情報
# File lib/zakuro/era/western.rb, line 364 def format(form: '%Y-%m-%d') @date.strftime(form) end
無効値(引数なし)かどうかを検証する
@return [True] 無効値 @return [False] 無効値以外
# File lib/zakuro/era/western.rb, line 353 def invalid? (@date == Date.new) end
月を取得する
@return [Integer] 月
# File lib/zakuro/era/western.rb, line 322 def month @date.month end
次年にする
@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
初期化時の日付とは異なる種別に切り替える
@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
日付データとして検証する
@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
データ型を検証する
@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
# File lib/zakuro/era/western.rb, line 136 def validate failed = valid_type return failed unless failed.size.zero? valid_date end
年を取得する
@return [Integer] 年
# File lib/zakuro/era/western.rb, line 313 def year @date.year end