Class: Zakuro::Japan::Calendar
- Inherits:
-
Object
- Object
- Zakuro::Japan::Calendar
- Defined in:
- lib/zakuro/era/japan/calendar.rb
Overview
Calendar 年月日情報(和暦)
Constant Summary collapse
- INVALID =
Returns 無効値.
-1 # @return [String] 空文字列
- EMPTY =
Returns 空文字列.
''
- LEAPED_TEXT =
Returns 閏を示す文字列.
'閏'
- FORMAT =
Returns 和暦日フォーマット.
/^([一-龥]{2,4})([0-9]+)年(#{LEAPED_TEXT})?([0-9]+)月([0-9]+)日$/.freeze
- DEFAULT_OUTPUT_FORM =
Returns 出力用デフォルトフォーマット.
'%s%02d年%s%02d月%02d日'
Instance Attribute Summary collapse
-
#day ⇒ Integer
readonly
日.
-
#gengou ⇒ String
readonly
元号.
- #leaped ⇒ True, False readonly
-
#month ⇒ Integer
readonly
月.
-
#year ⇒ Integer
readonly
元号年.
Class Method Summary collapse
-
.parse(regex: FORMAT, text: '') ⇒ Calendar
年月日情報(和暦)を生成する.
-
.valid_date_text(regex: FORMAT, text: '') ⇒ True
日付文字列を検証する.
Instance Method Summary collapse
-
#format(form: DEFAULT_OUTPUT_FORM) ⇒ String
文字列にする.
-
#initialize(gengou: EMPTY, year: INVALID, leaped: false, month: INVALID, day: INVALID) ⇒ Calendar
constructor
初期化.
-
#invalid? ⇒ True, False
無効か.
-
#same_month?(leaped:, month:) ⇒ True
同月か 年と日は無視する.
Constructor Details
#initialize(gengou: EMPTY, year: INVALID, leaped: false, month: INVALID, day: INVALID) ⇒ Calendar
初期化
47 48 49 50 51 52 53 |
# File 'lib/zakuro/era/japan/calendar.rb', line 47 def initialize(gengou: EMPTY, year: INVALID, leaped: false, month: INVALID, day: INVALID) @gengou = gengou @year = year @leaped = leaped @month = month @day = day end |
Instance Attribute Details
#day ⇒ Integer (readonly)
Returns 日.
36 37 38 |
# File 'lib/zakuro/era/japan/calendar.rb', line 36 def day @day end |
#gengou ⇒ String (readonly)
Returns 元号.
27 28 29 |
# File 'lib/zakuro/era/japan/calendar.rb', line 27 def gengou @gengou end |
#leaped ⇒ True, False (readonly)
32 33 34 |
# File 'lib/zakuro/era/japan/calendar.rb', line 32 def leaped @leaped end |
#month ⇒ Integer (readonly)
Returns 月.
34 35 36 |
# File 'lib/zakuro/era/japan/calendar.rb', line 34 def month @month end |
#year ⇒ Integer (readonly)
Returns 元号年.
29 30 31 |
# File 'lib/zakuro/era/japan/calendar.rb', line 29 def year @year end |
Class Method Details
.parse(regex: FORMAT, text: '') ⇒ Calendar
年月日情報(和暦)を生成する
100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/zakuro/era/japan/calendar.rb', line 100 def parse(regex: FORMAT, text: '') return Calendar.new unless valid_date_text(regex: regex, text: text) matched = text.match(regex) Calendar.new( gengou: matched[1], year: Tool::Typeconv.to_i(text: matched[2], default: INVALID), leaped: matched[3] ? true : false, month: Tool::Typeconv.to_i(text: matched[4], default: INVALID), day: Tool::Typeconv.to_i(text: matched[5], default: INVALID) ) end |
.valid_date_text(regex: FORMAT, text: '') ⇒ True
日付文字列を検証する
123 124 125 126 127 128 129 130 131 |
# File 'lib/zakuro/era/japan/calendar.rb', line 123 def valid_date_text(regex: FORMAT, text: '') return false unless text matched = text.match(regex) return false unless matched matched.size == 6 end |
Instance Method Details
#format(form: DEFAULT_OUTPUT_FORM) ⇒ String
文字列にする
70 71 72 73 |
# File 'lib/zakuro/era/japan/calendar.rb', line 70 def format(form: DEFAULT_OUTPUT_FORM) leaped_text = @leaped ? LEAPED_TEXT : '' super(form, @gengou, @year, leaped_text, @month, @day) end |
#invalid? ⇒ True, False
無効か
61 62 63 |
# File 'lib/zakuro/era/japan/calendar.rb', line 61 def invalid? @gengou == EMPTY || @year == INVALID || @month == INVALID || @day == INVALID end |
#same_month?(leaped:, month:) ⇒ True
同月か
年と日は無視する
85 86 87 88 89 |
# File 'lib/zakuro/era/japan/calendar.rb', line 85 def same_month?(leaped:, month:) return false unless @leaped == leaped @month == month end |