Class: Wareki::Date
Overview
Wareki date handling class, main implementation.
Constant Summary collapse
- FORMAT_DIRECTIVE_REGEX =
/%J(-|[_0]{0,2}[0-9]*|)([fFyYegGoOiImMsSlLdD][kK]?)/.freeze
- FORMAT_EXPANSION_REGEX =
/(?<!%)(?:%%)*\K#{FORMAT_DIRECTIVE_REGEX}/.freeze
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.
-
#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
- .today ⇒ Object
Instance Method Summary collapse
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #<=>(other) ⇒ Object
- #===(other) ⇒ Object
- #__set_jd(v) ⇒ Object
- #_jd_if_date_like(other) ⇒ Object
- #_number_format(opt) ⇒ Object
- #_to_days(other) ⇒ Object
- #_validate_date! ⇒ Object
- #eql?(other) ⇒ Boolean (also: #==)
- #expand_wareki_format(format_str) ⇒ Object
- #format(key, opt = '') ⇒ Object
- #hash ⇒ 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.
- #jd ⇒ Object
- #last_day_of_month ⇒ Object
- #leap_month=(v) ⇒ Object
- #leap_month? ⇒ Boolean
- #month_index ⇒ Object
- #strftime(format_str = '%JF') ⇒ Object
- #succ ⇒ Object
- #to_date(start = ::Date::ITALY) ⇒ Object
- #to_time ⇒ Object
Constructor Details
#initialize(era_name, era_year, month = 1, day = 1, is_leap_month = false) ⇒ Date
Returns a new instance of Date.
81 82 83 84 85 86 87 88 89 |
# File 'lib/wareki/date.rb', line 81 def initialize(era_name, era_year, month = 1, day = 1, is_leap_month = false) @era_name = era_name.to_s @era_year = era_year @month = month @day = day @is_leap_month = is_leap_month @year = Utils.era_year_to_civil(@era_name, @era_year) _validate_date! end |
Instance Attribute Details
#day ⇒ Object
Returns the value of attribute day.
13 14 15 |
# File 'lib/wareki/date.rb', line 13 def day @day end |
#era_name ⇒ Object
Returns the value of attribute era_name.
13 14 15 |
# File 'lib/wareki/date.rb', line 13 def era_name @era_name end |
#era_year ⇒ Object
Returns the value of attribute era_year.
13 14 15 |
# File 'lib/wareki/date.rb', line 13 def era_year @era_year end |
#month ⇒ Object
Returns the value of attribute month.
13 14 15 |
# File 'lib/wareki/date.rb', line 13 def month @month end |
#year ⇒ Object
Returns the value of attribute year.
13 14 15 |
# File 'lib/wareki/date.rb', line 13 def year @year end |
Class Method Details
._parse(str) ⇒ Object
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 52 53 54 55 56 57 |
# File 'lib/wareki/date.rb', line 19 def self._parse(str) str = str.to_s.gsub(/[[:space:]]/, '') match = REGEX.match(str) (match && !match[0].empty?) or raise ArgumentError, "Invaild Date: #{str}" era = match[:era_name] if (era.nil? || era == '') && match[:year].nil? year = Date.today.year else year = Utils.k2i(match[:year]) year > 0 or raise ArgumentError, "Invalid year: #{str}" end month = day = 1 era.to_s != '' && era.to_s != '紀元前' && !ERA_BY_NAME[era] and raise ArgumentError, "Date parse failed: Invalid era name '#{match[:era_name]}'" if match[:month] month = Utils.k2i(match[:month]) elsif match[:alt_month] month = Utils.alt_month_name_to_i(match[:alt_month]) end month > 12 || month < 1 and raise InvalidDate, "invalid date (month out of range): #{str}" is_leap = !!(match[:is_leap] || match[:is_leap_post]) if match[:day] if match[:day] == '晦' civil_year = Utils.era_year_to_civil(era, year) day = Utils.last_day_of_era_month(era, civil_year, month, is_leap) else day = Utils.k2i(match[:day]) end end {era: era, year: year, month: month, day: day, is_leap: is_leap} end |
.date(date) ⇒ Object
73 74 75 |
# File 'lib/wareki/date.rb', line 73 def self.date(date) jd(date.jd) end |
.imperial(year, month = 1, day = 1, is_leap_month = false) ⇒ Object
77 78 79 |
# File 'lib/wareki/date.rb', line 77 def self.imperial(year, month = 1, day = 1, is_leap_month = false) new('皇紀', year, month, day, is_leap_month) end |
.jd(d) ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'lib/wareki/date.rb', line 64 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
59 60 61 62 |
# File 'lib/wareki/date.rb', line 59 def self.parse(str) di = _parse(str) new(di[:era], di[:year], di[:month], di[:day], di[:is_leap]) end |
.today ⇒ Object
15 16 17 |
# File 'lib/wareki/date.rb', line 15 def self.today jd(::Date.today.jd) end |
Instance Method Details
#+(other) ⇒ Object
316 317 318 319 320 |
# File 'lib/wareki/date.rb', line 316 def +(other) n = _to_days(other) n.nil? and raise TypeError, "Cannot add #{other.inspect} to Wareki::Date" self.class.jd(jd + n) end |
#-(other) ⇒ Object
308 309 310 311 312 313 314 |
# File 'lib/wareki/date.rb', line 308 def -(other) n = _to_days(other) n.nil? or return self.class.jd(jd - n) ojd = _jd_if_date_like(other) ojd and return jd - ojd raise TypeError, "Cannot subtract #{other.inspect} from Wareki::Date" end |
#<=>(other) ⇒ Object
297 298 299 300 301 302 |
# File 'lib/wareki/date.rb', line 297 def <=>(other) ojd = _jd_if_date_like(other) ojd = other if ojd.nil? && other.is_a?(Numeric) ojd.nil? and return nil jd <=> ojd end |
#===(other) ⇒ Object
284 285 286 287 288 289 290 291 |
# File 'lib/wareki/date.rb', line 284 def ===(other) begin other.jd == jd or return false rescue NoMethodError, NotImplementedError return false end true end |
#__set_jd(v) ⇒ Object
140 141 142 |
# File 'lib/wareki/date.rb', line 140 def __set_jd(v) @jd = v end |
#_jd_if_date_like(other) ⇒ Object
330 331 332 333 334 335 |
# File 'lib/wareki/date.rb', line 330 def _jd_if_date_like(other) return other.jd if other.respond_to?(:jd) other.respond_to?(:to_date) && !other.is_a?(Numeric) and other = other.to_date other.respond_to?(:jd) ? other.jd : nil end |
#_number_format(opt) ⇒ Object
216 217 218 |
# File 'lib/wareki/date.rb', line 216 def _number_format(opt) Utils.number_format(opt) end |
#_to_days(other) ⇒ Object
322 323 324 325 326 327 328 |
# File 'lib/wareki/date.rb', line 322 def _to_days(other) # rubocop:disable Style/ClassEqualityComparison return other.in_days if other.class.name == 'ActiveSupport::Duration' # rubocop:enable Style/ClassEqualityComparison other.is_a?(Numeric) ? other : nil end |
#_validate_date! ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/wareki/date.rb', line 159 def _validate_date! (month.is_a?(Integer) && month >= 1 && month <= 12) or raise InvalidDate, "invalid date (month out of range): #{inspect}" (day.is_a?(Integer) && day >= 1) or raise InvalidDate, "invalid date (day out of range): #{inspect}" if !WESTERN_ERA_NAMES.include?(@era_name) && @year < GREGORIAN_START_YEAR # 暦テーブル外の年は従来どおり jd 変換時の UnsupportedDateRange に委ねる yobj = YEAR_BY_NUM[@year] or return !leap_month? || yobj.leap_month == month or raise InvalidDate, "invalid date (no leap month): #{inspect}" day <= yobj.month_days[month_index] or raise InvalidDate, "invalid date (day out of range): #{inspect}" else leap_month? and raise InvalidDate, "invalid date (no leap month): #{inspect}" day <= last_day_of_month or raise InvalidDate, "invalid date (day out of range): #{inspect}" end end |
#eql?(other) ⇒ Boolean Also known as: ==
272 273 274 275 276 277 278 279 280 281 |
# File 'lib/wareki/date.rb', line 272 def eql?(other) begin %i[year month day era_year era_name leap_month?].each do |attr| other.public_send(attr) == public_send(attr) or return false end rescue NoMethodError, NotImplementedError return false end true end |
#expand_wareki_format(format_str) ⇒ Object
205 206 207 |
# File 'lib/wareki/date.rb', line 205 def (format_str) format_str.to_str.gsub(FORMAT_EXPANSION_REGEX) { format($2, $1) || $& } end |
#format(key, opt = '') ⇒ Object
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/wareki/date.rb', line 220 def format(key, opt = '') case key.to_sym when :e then era_name when :g then era_name.to_s == '' ? '' : Kernel.format(_number_format(opt), era_year) when :G then era_name.to_s == '' ? '' : Utils.i2z(era_year) when :Gk then era_name.to_s == '' ? '' : YaKansuji.to_kan(era_year, :simple) when :GK if era_name.to_s == '' '' elsif era_year == 1 '元' else YaKansuji.to_kan(era_year, :simple) end when :o then year when :O then Utils.i2z(year) when :Ok then YaKansuji.to_kan(year, :simple) when :i then imperial_year when :I then Utils.i2z(imperial_year) when :Ik then YaKansuji.to_kan(imperial_year, :simple) when :s then Kernel.format(_number_format(opt), month) when :S then Utils.i2z(month) when :Sk then YaKansuji.to_kan(month, :simple) when :SK then Utils.alt_month_name(month) when :l then leap_month? ? "'" : '' when :L then leap_month? ? '’' : '' when :Lk then leap_month? ? '閏' : '' when :d then Kernel.format(_number_format(opt), day) when :D then Utils.i2z(day) when :Dk then YaKansuji.to_kan(day, :simple) when :DK if month == 1 && !leap_month? && day == 1 '元' elsif day == 1 '朔' elsif day == last_day_of_month '晦' else YaKansuji.to_kan(day, :simple) end when :m then "#{format(:s, opt)}#{format(:l)}" when :M then "#{format(:Lk)}#{format(:S)}" when :Mk then "#{format(:Lk)}#{format(:Sk)}" when :y then "#{format(:e)}#{format(:g, opt)}" when :Y then "#{format(:e)}#{format(:G)}" when :Yk then "#{format(:e)}#{format(:Gk)}" when :YK then "#{format(:e)}#{format(:GK)}" when :f then "#{format(:e)}#{format(:g, opt)}年#{format(:s, opt)}#{format(:l)}月#{format(:d, opt)}日" when :F then "#{format(:e)}#{format(:GK)}年#{format(:Lk)}#{format(:Sk)}月#{format(:Dk)}日" end end |
#hash ⇒ Object
293 294 295 |
# File 'lib/wareki/date.rb', line 293 def hash [self.class, @era_name, @era_year, @year, @month, @day, leap_month?].hash end |
#imperial_year ⇒ Object
91 92 93 |
# File 'lib/wareki/date.rb', line 91 def imperial_year @year - IMPERIAL_START_YEAR end |
#imperial_year=(v) ⇒ Object
95 96 97 |
# File 'lib/wareki/date.rb', line 95 def imperial_year=(v) self.year = v + IMPERIAL_START_YEAR end |
#jd ⇒ Object
179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/wareki/date.rb', line 179 def jd @jd and return @jd _validate_date! WESTERN_ERA_NAMES.include?(@era_name) and return @jd = ::Date.new(@year, month, day, ::Date::ITALY).jd @year >= GREGORIAN_START_YEAR and return @jd = ::Date.new(@year, month, day, ::Date::GREGORIAN).jd yobj = YEAR_BY_NUM[@year] or raise UnsupportedDateRange, "Cannot convert to jd #{inspect}" @jd = yobj.month_starts[month_index] + day - 1 end |
#last_day_of_month ⇒ Object
155 156 157 |
# File 'lib/wareki/date.rb', line 155 def last_day_of_month Utils.last_day_of_era_month(@era_name, @year, month, leap_month?) end |
#leap_month=(v) ⇒ Object
103 104 105 106 |
# File 'lib/wareki/date.rb', line 103 def leap_month=(v) @jd = nil @is_leap_month = v end |
#leap_month? ⇒ Boolean
99 100 101 |
# File 'lib/wareki/date.rb', line 99 def leap_month? !!@is_leap_month end |
#month_index ⇒ Object
144 145 146 147 148 149 150 151 152 153 |
# File 'lib/wareki/date.rb', line 144 def month_index return month - 1 if WESTERN_ERA_NAMES.include?(@era_name) || @year >= GREGORIAN_START_YEAR yobj = YEAR_BY_NUM[@year] or raise UnsupportedDateRange, "Cannot get year info of #{inspect}" idx = month - 1 idx += 1 if leap_month? || (yobj.leap_month && month > yobj.leap_month) idx end |
#strftime(format_str = '%JF') ⇒ Object
209 210 211 212 213 214 |
# File 'lib/wareki/date.rb', line 209 def strftime(format_str = '%JF') ret = (format_str) ret.index('%') or return ret d = to_date d.respond_to?(:_wareki_strftime_orig) ? d._wareki_strftime_orig(ret) : d.strftime(ret) end |
#succ ⇒ Object
304 305 306 |
# File 'lib/wareki/date.rb', line 304 def succ self + 1 end |
#to_date(start = ::Date::ITALY) ⇒ Object
194 195 196 |
# File 'lib/wareki/date.rb', line 194 def to_date(start = ::Date::ITALY) ::Date.jd(jd, start) end |
#to_time ⇒ Object
198 199 200 |
# File 'lib/wareki/date.rb', line 198 def to_time to_date.to_time end |