Class: Zakuro::Western::Calendar
- Inherits:
-
Object
- Object
- Zakuro::Western::Calendar
- Defined in:
- lib/zakuro/era/western/calendar.rb
Overview
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 のバリエーションで日付オブジェクトを初期化するだけで良い
Instance Attribute Summary collapse
-
#date ⇒ Date
readonly
日付(Ruby日付型).
-
#param ⇒ Parameter
readonly
初期化引数.
Class Method Summary collapse
-
.create(date: Date.new) ⇒ Calendar
年月日情報(西暦)を生成する.
-
.parse(text: '', type: Type::DEFAULT) ⇒ Calendar
年月日情報(西暦)を生成する.
-
.valid_date_text(text: '') ⇒ True
日付文字列を検証する.
Instance Method Summary collapse
-
#+(other) ⇒ Calendar
加算する.
-
#-(other) ⇒ Calendar
減算する.
-
#<(other) ⇒ True, False
大小比較する(<).
-
#<=(other) ⇒ True, False
大小比較する(<=).
-
#==(other) ⇒ True, False
大小比較する(==).
-
#>(other) ⇒ True, False
大小比較する(>).
-
#>=(other) ⇒ True, False
大小比較する(>=).
-
#day ⇒ Integer
日を取得する.
-
#format(form: '%Y-%m-%d') ⇒ String
年月日をフォーマット化する.
-
#initialize(year: -4712,, month: 1, day: 1, type: Type::DEFAULT) ⇒ Calendar
constructor
初期化.
-
#invalid? ⇒ True, False
無効値(引数なし)かどうかを検証する.
-
#month ⇒ Integer
月を取得する.
-
#next_year(num: 1) ⇒ Calendar
次年にする.
-
#redate(type: Type::DEFAULT) ⇒ Calendar
初期化時の日付とは異なる種別に切り替える.
-
#valid_date ⇒ Array<String>
日付データとして検証する.
-
#valid_type ⇒ Array<String>
データ型を検証する.
-
#validate ⇒ Array<String>
検証する.
-
#year ⇒ Integer
年を取得する.
Constructor Details
#initialize(year: -4712,, month: 1, day: 1, type: Type::DEFAULT) ⇒ Calendar
初期化
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
#date ⇒ Date (readonly)
141 142 143 |
# File 'lib/zakuro/era/western/calendar.rb', line 141 def date @date end |
#param ⇒ Parameter (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
年月日情報(西暦)を生成する
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 |
#day ⇒ Integer
日を取得する
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 |
#month ⇒ Integer
月を取得する
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
初期化時の日付とは異なる種別に切り替える
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_date ⇒ Array<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_type ⇒ Array<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 |
#validate ⇒ Array<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 |
#year ⇒ Integer
年を取得する
327 328 329 |
# File 'lib/zakuro/era/western/calendar.rb', line 327 def year date.year end |