wareki 2.0.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/wareki/common.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'wareki/calendar_def'
3
+ require 'wareki/calendar'
4
4
  require 'wareki/era_def'
5
5
  require 'date'
6
6
  # Wareki module common constants definitions
@@ -12,7 +12,6 @@ module Wareki
12
12
  WESTERN_ERA_NAMES = ['', '西暦', '紀元前'].freeze
13
13
  IMPERIAL_ERA_NAMES = %w(皇紀 神武天皇即位紀元).freeze
14
14
  DATE_INFINITY = ::Date.new(280_000_000, 12, 31) # Use 280000000 for jruby limitation...
15
- YEAR_BY_NUM = Hash[*YEAR_DEFS.map { |y| [y.year, y] }.flatten].freeze
16
15
  KANJI_VARIANTS = {
17
16
  '宝' => '寳',
18
17
  '霊' => '靈',
@@ -89,7 +88,7 @@ module Wareki
89
88
  Date.parse(str).to_date(start)
90
89
  rescue InvalidDate
91
90
  raise
92
- rescue ArgumentError, UnsupportedDateRange
91
+ rescue ArgumentError
93
92
  ::Date.parse(str, true, start)
94
93
  end
95
94
  end
data/lib/wareki/date.rb CHANGED
@@ -145,11 +145,9 @@ module Wareki
145
145
  return month - 1 if
146
146
  WESTERN_ERA_NAMES.include?(@era_name) || @year >= GREGORIAN_START_YEAR
147
147
 
148
- yobj = YEAR_BY_NUM[@year] or
148
+ Calendar.covers_year?(@year) or
149
149
  raise UnsupportedDateRange, "Cannot get year info of #{inspect}"
150
- idx = month - 1
151
- idx += 1 if leap_month? || (yobj.leap_month && month > yobj.leap_month)
152
- idx
150
+ Calendar.month_index(@year, month, leap_month?)
153
151
  end
154
152
 
155
153
  def last_day_of_month
@@ -163,10 +161,10 @@ module Wareki
163
161
  raise InvalidDate, "invalid date (day out of range): #{inspect}"
164
162
  if !WESTERN_ERA_NAMES.include?(@era_name) && @year < GREGORIAN_START_YEAR
165
163
  # 暦テーブル外の年は従来どおり jd 変換時の UnsupportedDateRange に委ねる
166
- yobj = YEAR_BY_NUM[@year] or return
167
- !leap_month? || yobj.leap_month == month or
164
+ Calendar.covers_year?(@year) or return
165
+ !leap_month? || Calendar.leap_month(@year) == month or
168
166
  raise InvalidDate, "invalid date (no leap month): #{inspect}"
169
- day <= yobj.month_days[month_index] or
167
+ day <= Calendar.last_day_of_month(@year, month, leap_month?) or
170
168
  raise InvalidDate, "invalid date (day out of range): #{inspect}"
171
169
  else
172
170
  leap_month? and
@@ -186,9 +184,9 @@ module Wareki
186
184
  @year >= GREGORIAN_START_YEAR and
187
185
  return @jd = ::Date.new(@year, month, day, ::Date::GREGORIAN).jd
188
186
 
189
- yobj = YEAR_BY_NUM[@year] or
187
+ @jd = Calendar.to_jd(@year, month, day, leap_month?) or
190
188
  raise UnsupportedDateRange, "Cannot convert to jd #{inspect}"
191
- @jd = yobj.month_starts[month_index] + day - 1
189
+ @jd
192
190
  end
193
191
 
194
192
  def to_date(start = ::Date::ITALY)
@@ -222,31 +220,31 @@ module Wareki
222
220
  when :e then era_name
223
221
  when :g then era_name.to_s == '' ? '' : Kernel.format(_number_format(opt), era_year)
224
222
  when :G then era_name.to_s == '' ? '' : Utils.i2z(era_year)
225
- when :Gk then era_name.to_s == '' ? '' : YaKansuji.to_kan(era_year, :simple)
223
+ when :Gk then era_name.to_s == '' ? '' : Utils.to_simple_kan(era_year)
226
224
  when :GK
227
225
  if era_name.to_s == ''
228
226
  ''
229
227
  elsif era_year == 1
230
228
  '元'
231
229
  else
232
- YaKansuji.to_kan(era_year, :simple)
230
+ Utils.to_simple_kan(era_year)
233
231
  end
234
232
  when :o then year
235
233
  when :O then Utils.i2z(year)
236
- when :Ok then YaKansuji.to_kan(year, :simple)
234
+ when :Ok then Utils.to_simple_kan(year)
237
235
  when :i then imperial_year
238
236
  when :I then Utils.i2z(imperial_year)
239
- when :Ik then YaKansuji.to_kan(imperial_year, :simple)
237
+ when :Ik then Utils.to_simple_kan(imperial_year)
240
238
  when :s then Kernel.format(_number_format(opt), month)
241
239
  when :S then Utils.i2z(month)
242
- when :Sk then YaKansuji.to_kan(month, :simple)
240
+ when :Sk then Utils.to_simple_kan(month)
243
241
  when :SK then Utils.alt_month_name(month)
244
242
  when :l then leap_month? ? "'" : ''
245
243
  when :L then leap_month? ? '’' : ''
246
244
  when :Lk then leap_month? ? '閏' : ''
247
245
  when :d then Kernel.format(_number_format(opt), day)
248
246
  when :D then Utils.i2z(day)
249
- when :Dk then YaKansuji.to_kan(day, :simple)
247
+ when :Dk then Utils.to_simple_kan(day)
250
248
  when :DK
251
249
  if month == 1 && !leap_month? && day == 1
252
250
  '元'
@@ -255,7 +253,7 @@ module Wareki
255
253
  elsif day == last_day_of_month
256
254
  '晦'
257
255
  else
258
- YaKansuji.to_kan(day, :simple)
256
+ Utils.to_simple_kan(day)
259
257
  end
260
258
  when :m then "#{format(:s, opt)}#{format(:l)}"
261
259
  when :M then "#{format(:Lk)}#{format(:S)}"
@@ -1,12 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
  # coding: utf-8
3
+
4
+ # Generated by build-util/gen-era-def.rb from manakai data-locale era-defs.json.
5
+ # Do not edit this file directly.
3
6
  module Wareki
4
7
  DAY_MAX = 1684383730585466947585
5
8
  Era = Struct.new(:name, :year, :start, :end)
6
9
  ERA_DEFS = [
7
10
  Era.new("大化", 645, 1956842, 1958551),
8
- Era.new("白雉", 650, 1958551, 1960259),
9
- Era.new("朱鳥", 686, 1971845, 1971893),
11
+ Era.new("白雉", 650, 1958551, 1960339),
12
+ Era.new("朱鳥", 686, 1971845, 1972033),
10
13
  Era.new("大宝", 701, 1977221, 1978361),
11
14
  Era.new("慶雲", 704, 1978361, 1979692),
12
15
  Era.new("和銅", 708, 1979692, 1982487),
@@ -163,9 +166,9 @@ module Wareki
163
166
  Era.new("建武", 1334, 2208365, 2209133),
164
167
  Era.new("延元", 1336, 2209133, 2210638),
165
168
  Era.new("興国", 1340, 2210638, 2213069),
166
- Era.new("正平", 1346, 2213069, 2221678),
167
- Era.new("建徳", 1370, 2221678, 2222302),
168
- Era.new("文中", 1372, 2222302, 2223453),
169
+ Era.new("正平", 1346, 2213069, 2221512),
170
+ Era.new("建徳", 1370, 2221512, 2222332),
171
+ Era.new("文中", 1372, 2222332, 2223453),
169
172
  Era.new("天授", 1375, 2223453, 2225533),
170
173
  Era.new("弘和", 1381, 2225533, 2226702),
171
174
  Era.new("元中", 1384, 2226702, 2229809),
@@ -255,8 +258,8 @@ module Wareki
255
258
  ].each(&:freeze).freeze
256
259
  ERA_NORTH_DEFS = [
257
260
  Era.new("大化", 645, 1956842, 1958551),
258
- Era.new("白雉", 650, 1958551, 1960259),
259
- Era.new("朱鳥", 686, 1971845, 1971893),
261
+ Era.new("白雉", 650, 1958551, 1960339),
262
+ Era.new("朱鳥", 686, 1971845, 1972033),
260
263
  Era.new("大宝", 701, 1977221, 1978361),
261
264
  Era.new("慶雲", 704, 1978361, 1979692),
262
265
  Era.new("和銅", 708, 1979692, 1982487),
@@ -407,15 +410,15 @@ module Wareki
407
410
  Era.new("元亨", 1321, 2203634, 2205008),
408
411
  Era.new("正中", 1324, 2205008, 2205527),
409
412
  Era.new("嘉暦", 1326, 2205527, 2206740),
410
- Era.new("元徳", 1329, 2206740, 2207459),
413
+ Era.new("元徳", 1329, 2206740, 2207714),
411
414
  Era.new("元弘", 1331, 2207459, 2208365),
412
415
  Era.new("正慶", 1332, 2207714, 2208124),
413
416
  Era.new("建武", 1334, 2208365, 2210046),
414
417
  Era.new("延元", 1336, 2209133, 2210638),
415
418
  Era.new("興国", 1340, 2210638, 2213069),
416
- Era.new("正平", 1346, 2213069, 2221678),
417
- Era.new("建徳", 1370, 2221678, 2222302),
418
- Era.new("文中", 1372, 2222302, 2223453),
419
+ Era.new("正平", 1346, 2213069, 2221512),
420
+ Era.new("建徳", 1370, 2221512, 2222332),
421
+ Era.new("文中", 1372, 2222332, 2223453),
419
422
  Era.new("天授", 1375, 2223453, 2225533),
420
423
  Era.new("弘和", 1381, 2225533, 2226702),
421
424
  Era.new("元中", 1384, 2226702, 2229809),
@@ -43,24 +43,28 @@ class Date
43
43
  class << self
44
44
  alias _wareki_parse_orig parse
45
45
  def parse(str = '-4712-01-01', comp = true, start = ::Date::ITALY)
46
+ return ::Date._wareki_parse_orig(str, comp, start) if str.to_s.ascii_only?
47
+
46
48
  str = Wareki::Utils.normalize_time(str)
47
49
  str.to_s =~ Wareki::PARSE_QUICK_FILTER or
48
50
  return ::Date._wareki_parse_orig(str, comp, start)
49
51
  Wareki::Date.parse(str).to_date(start)
50
52
  rescue Wareki::InvalidDate
51
53
  raise
52
- rescue ArgumentError, Wareki::UnsupportedDateRange
54
+ rescue ArgumentError
53
55
  ::Date._wareki_parse_orig(str, comp, start)
54
56
  end
55
57
 
56
58
  alias _wareki__parse_orig _parse
57
59
  def _parse(str, comp = true)
60
+ return ::Date._wareki__parse_orig(str, comp) if str.to_s.ascii_only?
61
+
58
62
  str = Wareki::Utils.normalize_time(str)
59
63
  str.to_s =~ Wareki::PARSE_QUICK_FILTER or
60
64
  return ::Date._wareki__parse_orig(str, comp)
61
65
  di = Wareki::Date._parse(str)
62
66
  wdate = Wareki::Date.new(di[:era], di[:year], di[:month], di[:day], di[:is_leap])
63
- rescue ArgumentError, Wareki::UnsupportedDateRange
67
+ rescue ArgumentError
64
68
  ::Date._wareki__parse_orig(str, comp)
65
69
  else
66
70
  ::Date._wareki__parse_orig(str.sub(Wareki::REGEX, wdate.strftime('%F ')), comp)
data/lib/wareki/utils.rb CHANGED
@@ -7,6 +7,7 @@ module Wareki
7
7
  module Utils
8
8
  TIME_FORMAT_DIRECTIVE_REGEX = /%J(-|[_0]{0,2}[0-9]*|)(T(?:[fF]|[HMS]k?))/.freeze
9
9
  TIME_FORMAT_EXPANSION_REGEX = /(?<!%)(?:%%)*\K#{TIME_FORMAT_DIRECTIVE_REGEX}/.freeze
10
+ SIMPLE_KANSUJI_CACHE = (0..99).map { |num| YaKansuji.to_kan(num, :simple).freeze }.freeze
10
11
 
11
12
  module_function
12
13
 
@@ -23,11 +24,8 @@ module Wareki
23
24
  end
24
25
 
25
26
  def _last_day_of_month_from_defs(year, month, is_leap)
26
- yobj = YEAR_BY_NUM[year] or
27
+ Calendar.last_day_of_month(year, month, is_leap) or
27
28
  raise UnsupportedDateRange, "Cannot find year #{year}"
28
- month_idx = month - 1
29
- month_idx += 1 if is_leap || (yobj.leap_month && yobj.leap_month < month)
30
- yobj.month_days[month_idx]
31
29
  end
32
30
 
33
31
  def era_year_to_civil(era_name, era_year)
@@ -94,24 +92,8 @@ module Wareki
94
92
  d.jd >= GREGORIAN_START and
95
93
  return [d.year, d.month, d.day, false]
96
94
 
97
- yobj = find_year(d) or raise UnsupportedDateRange, "Unsupported date: #{d.inspect}"
98
- month = 0
99
- if yobj.month_starts.last <= d.jd
100
- month = yobj.month_starts.count
101
- else
102
- month = yobj.month_starts.find_index { |m| d.jd <= (m - 1) }
103
- end
104
- month_start = yobj.month_starts[month - 1]
105
- is_leap = (yobj.leap_month == (month - 1))
106
- yobj.leap_month && yobj.leap_month < month and
107
- month -= 1
108
- [yobj.year, month, d.jd - month_start + 1, is_leap]
109
- end
110
-
111
- def find_year(d)
112
- jd = _to_jd(d)
113
- jd < YEAR_DEFS.first.start and return nil
114
- YEAR_DEFS.bsearch { |y| y.end >= jd }
95
+ Calendar.find_date_ary(d.jd) or
96
+ raise UnsupportedDateRange, "Unsupported date: #{d.inspect}"
115
97
  end
116
98
 
117
99
  def find_era(d)
@@ -137,6 +119,13 @@ module Wareki
137
119
  end
138
120
  end
139
121
 
122
+ def to_simple_kan(num)
123
+ return SIMPLE_KANSUJI_CACHE[num].dup if
124
+ num.is_a?(Integer) && num >= 0 && num < SIMPLE_KANSUJI_CACHE.length
125
+
126
+ YaKansuji.to_kan(num, :simple)
127
+ end
128
+
140
129
  # 日本語の時刻表記 (漢数字・全角数字の時分秒、午前/午後、半、正午) を
141
130
  # 等価な "HH:MM(:SS)" 表記へ置換する。値の範囲チェックは行わず、
142
131
  # 妥当性判断は Ruby 標準パーサに委ねる (二十五時 -> "25:00")。
@@ -179,14 +168,13 @@ module Wareki
179
168
  nf = number_format(opt)
180
169
  Kernel.format("#{nf}時#{nf}分#{nf}秒", time.hour, time.min, time.sec)
181
170
  when :TF
182
- "#{YaKansuji.to_kan(time.hour, :simple)}時#{YaKansuji.to_kan(time.min, :simple)}" \
183
- "#{YaKansuji.to_kan(time.sec, :simple)}秒"
171
+ "#{to_simple_kan(time.hour)}時#{to_simple_kan(time.min)}分#{to_simple_kan(time.sec)}秒"
184
172
  when :TH then i2z(time.hour)
185
- when :THk then YaKansuji.to_kan(time.hour, :simple)
173
+ when :THk then to_simple_kan(time.hour)
186
174
  when :TM then i2z(time.min)
187
- when :TMk then YaKansuji.to_kan(time.min, :simple)
175
+ when :TMk then to_simple_kan(time.min)
188
176
  when :TS then i2z(time.sec)
189
- when :TSk then YaKansuji.to_kan(time.sec, :simple)
177
+ when :TSk then to_simple_kan(time.sec)
190
178
  end
191
179
  end
192
180
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Wareki
4
- VERSION = '2.0.0'
4
+ VERSION = '2.1.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wareki
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tatsuki Sugiura
@@ -43,6 +43,7 @@ files:
43
43
  - LICENSE
44
44
  - README.md
45
45
  - lib/wareki.rb
46
+ - lib/wareki/calendar.rb
46
47
  - lib/wareki/calendar_def.rb
47
48
  - lib/wareki/common.rb
48
49
  - lib/wareki/date.rb
@@ -64,7 +65,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
64
65
  requirements:
65
66
  - - ">="
66
67
  - !ruby/object:Gem::Version
67
- version: 2.0.0
68
+ version: 2.3.0
68
69
  required_rubygems_version: !ruby/object:Gem::Requirement
69
70
  requirements:
70
71
  - - ">="