Class: Wareki::Date
- Inherits:
-
Object
- Object
- Wareki::Date
- Defined in:
- lib/wareki/date.rb
Instance Attribute Summary collapse
-
#day ⇒ Object
Returns the value of attribute day.
-
#era_name ⇒ Object
Returns the value of attribute era_name.
-
#era_year ⇒ Object
Returns the value of attribute era_year.
-
#jd ⇒ Object
readonly
Returns the value of attribute jd.
-
#month ⇒ Object
Returns the value of attribute month.
-
#year ⇒ Object
Returns the value of attribute year.
Class Method Summary collapse
- ._parse(str) ⇒ Object
- .date(date) ⇒ Object
- .imperial(year, month = 1, day = 1, is_leap_month = false) ⇒ Object
- .jd(d) ⇒ Object
- .parse(str) ⇒ Object
Instance Method Summary collapse
- #__set_jd(v) ⇒ Object
- #format(key) ⇒ Object
- #imperial_year ⇒ Object
- #imperial_year=(v) ⇒ Object
-
#initialize(era_name, era_year, month = 1, day = 1, is_leap_month = false) ⇒ Date
constructor
A new instance of Date.
- #leap_month=(v) ⇒ Object
- #leap_month? ⇒ Boolean
- #month_index ⇒ Object
- #strftime(format_str = "%JF") ⇒ Object
- #to_date(start = ::Date::ITALY) ⇒ Object
Constructor Details
#initialize(era_name, era_year, month = 1, day = 1, is_leap_month = false) ⇒ Date
Returns a new instance of Date.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/wareki/date.rb', line 75 def initialize(era_name, era_year, month = 1, day = 1, is_leap_month = false) if era_name.to_s != "" && era_name != "西暦" && !ERA_BY_NAME[era_name] raise ArgumentError, "Undefined era '#{era_name}'" end @month = month @day = day @is_leap_month = is_leap_month @era_name = era_name @era_year = era_year if era_name.to_s == "" @year = @era_year elsif era_name == "皇紀" || era_name == "神武天皇即位紀元" @year = era_year + IMPERIAL_START_YEAR else @year = ERA_BY_NAME[era_name].year + era_year - 1 end end |
Instance Attribute Details
#day ⇒ Object
Returns the value of attribute day.
9 10 11 |
# File 'lib/wareki/date.rb', line 9 def day @day end |
#era_name ⇒ Object
Returns the value of attribute era_name.
9 10 11 |
# File 'lib/wareki/date.rb', line 9 def era_name @era_name end |
#era_year ⇒ Object
Returns the value of attribute era_year.
9 10 11 |
# File 'lib/wareki/date.rb', line 9 def era_year @era_year end |
#jd ⇒ Object (readonly)
Returns the value of attribute jd.
8 9 10 |
# File 'lib/wareki/date.rb', line 8 def jd @jd end |
#month ⇒ Object
Returns the value of attribute month.
9 10 11 |
# File 'lib/wareki/date.rb', line 9 def month @month end |
#year ⇒ Object
Returns the value of attribute year.
9 10 11 |
# File 'lib/wareki/date.rb', line 9 def year @year end |
Class Method Details
._parse(str) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/wareki/date.rb', line 11 def self._parse(str) match = REGEX.match(str.to_s.gsub(/[[:space:]]/, '')) if !match || !match[:year] raise ArgumentError, "Invaild Date: #{str}" end era = match[:era_name] year = Utils.kan_to_i(match[:year]) month = 1 day = 1 if era.to_s != "" && !ERA_BY_NAME[era] raise ArgumentError, "Date parse failed: Invalid era name '#{match[:era_name]}'" end if match[:month] month = Utils.kan_to_i(match[:month]) elsif match[:alt_month] month = Utils.alt_month_name_to_i(match[:alt_month]) end month > 12 || month < 0 and raise ArgumentError, "Invalid month: #{str}" if match[:day] if match[:day] == "晦" day = Utils.last_day_of_month(ERA_BY_NAME[era].year + year -1, month, match[:is_leap]) else day = Utils.kan_to_i(match[:day]) end end if (era == "明治" && year == 5 || era.to_s == "" && year == GREGORIAN_START_YEAR - 1 || (era == "皇紀" || era == "神武天皇即位紀元") && year == GREGORIAN_START_YEAR - IMPERIAL_START_YEAR - 1) && month == 12 && day > 2 raise ArgumentError, "Invaild Date: #{str}" end {era: era, year: year, month: month, day: day, is_leap: !!match[:is_leap]} end |
.date(date) ⇒ Object
67 68 69 |
# File 'lib/wareki/date.rb', line 67 def self.date(date) jd(date.jd) end |
.imperial(year, month = 1, day = 1, is_leap_month = false) ⇒ Object
71 72 73 |
# File 'lib/wareki/date.rb', line 71 def self.imperial(year, month = 1, day = 1, is_leap_month = false) new("皇紀", year, month, day, is_leap_month) end |
.jd(d) ⇒ Object
58 59 60 61 62 63 64 65 |
# File 'lib/wareki/date.rb', line 58 def self.jd(d) era = Utils.find_era(d) era or raise UnsupportedDateRange, "Cannot find era for date #{d.inspect}" year, month, day, is_leap = Utils.find_date_ary(d) obj = new(era.name, year - era.year + 1, month, day, is_leap) obj.__set_jd(d) obj end |
.parse(str) ⇒ Object
53 54 55 56 |
# File 'lib/wareki/date.rb', line 53 def self.parse(str) di = _parse(str) new(di[:era], di[:year], di[:month], di[:day], di[:is_leap]) end |
Instance Method Details
#__set_jd(v) ⇒ Object
109 110 111 |
# File 'lib/wareki/date.rb', line 109 def __set_jd(v) @jd = v end |
#format(key) ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/wareki/date.rb', line 152 def format(key) case key.to_sym when :e; era_name when :g; era_name.to_s == "" ? '' : era_year when :G; era_name.to_s == "" ? '' : Utils.i_to_zen(era_year) when :Gk; era_name.to_s == "" ? '' : Utils.i_to_kan(era_year) when :GK if era_name.to_s == "" '' elsif era_year == 1 "元" else Utils.i_to_kan(era_year) end when :o; year when :O; Utils.i_to_zen(year) when :Ok; Utils.i_to_kan(year) when :i; imperial_year when :I; Utils.i_to_zen(imperial_year) when :Ik; Utils.i_to_kan(imperial_year) when :s; month when :S; Utils.i_to_zen(month) when :Sk; Utils.i_to_kan(month) when :SK; ALT_MONTH_NAME[month-1] when :l; leap_month? ? "'" : "" when :L; leap_month? ? "’" : "" when :Lk; leap_month? ? "閏" : "" when :d; day when :D; Utils.i_to_zen(day) when :Dk; Utils.i_to_kan(day) when :DK if month == 1 && !leap_month? && day == 1 "元" elsif day == 1 "朔" elsif day == Utils.last_day_of_month(year, month, leap_month?) "晦" else Utils.i_to_kan(day) end when :m; "#{format(:s)}#{format(:l)}" when :M; "#{format(:Lk)}#{format(:S)}" when :Mk; "#{format(:Lk)}#{format(:Sk)}" when :y; "#{format(:e)}#{format(:g)}" when :Y; "#{format(:e)}#{format(:G)}" when :Yk; "#{format(:e)}#{format(:Gk)}" when :YK; "#{format(:e)}#{format(:GK)}" when :f; "#{format(:e)}#{format(:g)}年#{format(:s)}#{format(:l)}月#{format(:d)}日" when :F; "#{format(:e)}#{format(:GK)}年#{format(:Lk)}#{format(:Sk)}月#{format(:Dk)}日" else nil end end |
#imperial_year ⇒ Object
93 94 95 |
# File 'lib/wareki/date.rb', line 93 def imperial_year @year - IMPERIAL_START_YEAR end |
#imperial_year=(v) ⇒ Object
97 98 99 |
# File 'lib/wareki/date.rb', line 97 def imperial_year=(v) @year = v - IMPERIAL_START_YEAR end |
#leap_month=(v) ⇒ Object
105 106 107 |
# File 'lib/wareki/date.rb', line 105 def leap_month=(v) @is_leap_month = v end |
#leap_month? ⇒ Boolean
101 102 103 |
# File 'lib/wareki/date.rb', line 101 def leap_month? !!@is_leap_month end |
#month_index ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/wareki/date.rb', line 113 def month_index if @era_name == "西暦" || @year >= GREGORIAN_START_YEAR return month -1 end yobj = YEAR_BY_NUM[@year] or raise UnsupportedDateRange, "Cannot get year info of #{self.inspect}" idx = month - 1 if leap_month? || yobj.leap_month && month > yobj.leap_month idx += 1 end idx end |
#strftime(format_str = "%JF") ⇒ Object
145 146 147 148 149 150 |
# File 'lib/wareki/date.rb', line 145 def strftime(format_str = "%JF") ret = format_str.to_str.gsub(/%J([fFyYegGoOiImMsSlLdD][kK]?)/) { format($1) || $& } ret.index("%") or return ret d = to_date d.respond_to?(:_wareki_strftime_orig) ? d._wareki_strftime_orig(ret) : d.strftime(ret) end |