era_ja 1.0.0 → 1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 58f5955178ca4a898c771451e6b33345acf59352ae6d89a1fc1334da5b0e2e3a
4
- data.tar.gz: baef3d1dcc782d26d062095479f6f6121941d2a258e3870dbecbc1080fa3eaba
3
+ metadata.gz: 38838bfc2c49efc21bfb6ea0ad206af0cdda6d67c66e21eb786d02990e1c2edd
4
+ data.tar.gz: 2fe65c8cf87ea96e14840c0835f02a87469ac92e2b3cbaddc008284d39691bcf
5
5
  SHA512:
6
- metadata.gz: a501415e87bc28d933553835d1baca333d5c67cd15becf01fca6cbe71ca662cf20e56e427e2bcbfb72dab2650026a1bdd96299ce37ca120614c4028420ac044b
7
- data.tar.gz: f95522ba00aadfb7f3a97d3eaeb0b7a958f7817e20c3d0a76a618a9b34ef515022223792b794e04c568e8234c2d71607d3d237e7ba79ec203e9ffd91ce1760dd
6
+ metadata.gz: 6c7c3e10a11d56a7c33670629e4570f2e4b19a935d42a53d6f1a825846cc660fe70eb8d7035466541e9536919c34b3a3b5c992b95a5db4a3e3137e19fe98c523
7
+ data.tar.gz: 8940a5d449b38cce4be8e10f54f228ca78afdc24743d51956748490eb058f42804e999094ae0de7d9496cc74ecc1f6bb98c186364bf2369a44fb9bc2c676d3fb
@@ -79,6 +79,18 @@ Time.mktime(2012,4,29).to_era("%O%JE年%Jm月%Jd日", era_names: { heisei: ['h',
79
79
  # same as Date
80
80
  ```
81
81
 
82
+ ### 元号に変換できるかどうかの判定方法 ###
83
+
84
+ 日付が元号に変換できるかどうかを判定するために、`era_convertible?` を使うことができます。
85
+
86
+ ```ruby
87
+ require 'era_ja'
88
+ Time.mktime(1868,9,7).era_convertible? #=> false
89
+ Time.mktime(1868,9,8).era_convertible? #=> true
90
+ ```
91
+
92
+ 元号に変換できない日付に関しては、[Noteセクション](#note) を参照してください。
93
+
82
94
  ## Support
83
95
 
84
96
  問題を報告したり、機能追加の要望する場合はgithubのIssuesに登録してください。 https://github.com/tomiacannondale/era_ja/issues
data/README.md CHANGED
@@ -80,6 +80,18 @@ Time.mktime(2012,4,29).to_era("%O%JE年%Jm月%Jd日", era_names: { heisei: ['h',
80
80
  # same as Date
81
81
  ```
82
82
 
83
+ ### Checking a date is convertible ###
84
+
85
+ You can use `era_convertible?` to check if a given date is convertible or not.
86
+
87
+ ```ruby
88
+ require 'era_ja'
89
+ Time.mktime(1868,9,7).era_convertible? #=> false
90
+ Time.mktime(1868,9,8).era_convertible? #=> true
91
+ ```
92
+
93
+ See [Note](#note) section for more details.
94
+
83
95
  ## Support
84
96
 
85
97
  Report issues and feature requests to github Issues. https://github.com/tomiacannondale/era_ja/issues
@@ -2,3 +2,4 @@
2
2
  require "era_ja/version"
3
3
  require "era_ja/date"
4
4
  require "era_ja/time"
5
+ require "era_ja/error"
@@ -8,11 +8,9 @@ module EraJa
8
8
  taisho: ["T", "大正"],
9
9
  showa: ["S", "昭和"],
10
10
  heisei: ["H", "平成"],
11
- reiwa: ["R", "令和"]
11
+ reiwa: ["R", "令和"]
12
12
  }.freeze
13
13
 
14
- ERR_DATE_OUT_OF_RANGE = "#to_era only works on dates after 1868,9,8".freeze
15
-
16
14
  # Convert to Japanese era.
17
15
  # @param [String] format_string
18
16
  # Time#strftime format string can be used
@@ -28,12 +26,12 @@ module EraJa
28
26
  # this argument is same as one element of ERA_NAME_DEFAULTS.
29
27
  # @return [String]
30
28
  def to_era(format = "%o%E.%m.%d", era_names: ERA_NAME_DEFAULTS)
29
+ raise EraJa::DateOutOfRangeError unless era_convertible?
30
+
31
31
  @era_format = format.gsub(/%J/, "%J%")
32
32
  str_time = strftime(@era_format)
33
33
  if @era_format =~ /%([EOo]|1O)/
34
34
  case
35
- when self.to_time < ::Time.mktime(1868,9,8)
36
- raise ERR_DATE_OUT_OF_RANGE
37
35
  when self.to_time < ::Time.mktime(1912,7,30)
38
36
  str_time = era_year(year - 1867, :meiji, era_names)
39
37
  when self.to_time < ::Time.mktime(1926,12,25)
@@ -49,6 +47,10 @@ module EraJa
49
47
  str_time.gsub(/%J(\d+)/) { to_kanzi($1) }
50
48
  end
51
49
 
50
+ def era_convertible?
51
+ self.to_time >= ::Time.mktime(1868,9,8)
52
+ end
53
+
52
54
  private
53
55
  def era_year(year, era, era_names)
54
56
  strftime(@era_format).sub(/(%J)?%E/) { format_year(year, $1) }.sub(/%1?o/i) { format_era(era, era_names) }
@@ -0,0 +1,9 @@
1
+ module EraJa
2
+ class DateOutOfRangeError < StandardError
3
+ ERR_DATE_OUT_OF_RANGE = "#to_era only works on dates after 1868,9,8".freeze
4
+
5
+ def message
6
+ ERR_DATE_OUT_OF_RANGE
7
+ end
8
+ end
9
+ end
@@ -1,3 +1,3 @@
1
1
  module EraJa
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -5,6 +5,11 @@ require File.expand_path('spec_helper', File.dirname(__FILE__))
5
5
  RSpec.describe Date do
6
6
  describe "#to_era" do
7
7
 
8
+ context "date is 2020,1,1" do
9
+ subject { Date.new(2020,1,1) }
10
+ include_examples "2020,1,1"
11
+ end
12
+
8
13
  context "date is 2019,7,2" do
9
14
  subject { Date.new(2019,7,2) }
10
15
  include_examples "2019,7,2"
@@ -68,4 +73,20 @@ RSpec.describe Date do
68
73
 
69
74
  end
70
75
 
76
+ describe "#era_convertible?" do
77
+
78
+ context "when date is not convertible" do
79
+ it "returns false" do
80
+ expect(Date.new(1868,9,7).era_convertible?).to be false
81
+ end
82
+ end
83
+
84
+ context "when date is convertible" do
85
+ it "returns true" do
86
+ expect(Date.new(1868,9,8).era_convertible?).to be true
87
+ end
88
+ end
89
+
90
+ end
91
+
71
92
  end
@@ -1,5 +1,6 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  require 'rspec'
3
+ require File.expand_path('../lib/era_ja/error', File.dirname(__FILE__))
3
4
 
4
5
  RSpec.shared_examples "should equal 'R01.07.02'" do
5
6
  it { expect(subject.to_era).to eq "R01.07.02" }
@@ -23,12 +24,114 @@ RSpec.shared_examples "should equal 'R01.05.01'" do
23
24
  end
24
25
  end
25
26
 
27
+ RSpec.shared_examples "should equal 'r01.05.01'" do
28
+ context "with era_names option" do
29
+ context "when era_names is correct" do
30
+ let(:era_names) { { reiwa: ["r", "令和"] } }
31
+ it { expect(subject.to_era(era_names: era_names)).to eq "r01.05.01" }
32
+ end
33
+
34
+ context "when era_names have no reiwa values" do
35
+ let(:era_names) { { heisei: ["h", "平成"] } }
36
+ it { expect { subject.to_era(era_names: era_names) }.to raise_error(KeyError) }
37
+ end
38
+ end
39
+
40
+ context "with '%o%E.%m.%d' and era_names option" do
41
+ let(:era_names) { { reiwa: ["r", "令和"] } }
42
+ it { expect(subject.to_era("%o%E.%m.%d", era_names: era_names)).to eq "r01.05.01" }
43
+ end
44
+ end
45
+
26
46
  RSpec.shared_examples "should equal '令和01年05月01日'" do
27
47
  context "with '%O%E年%m月%d日'" do
28
48
  it { expect(subject.to_era("%O%E年%m月%d日")).to eq "令和01年05月01日" }
29
49
  end
30
50
  end
31
51
 
52
+ RSpec.shared_examples "should equal '令01年05月01日'" do
53
+ context "with '%1O%E年%m月%d日'" do
54
+ it { expect(subject.to_era("%1O%E年%m月%d日")).to eq "令01年05月01日" }
55
+ end
56
+ end
57
+
58
+ RSpec.shared_examples "should equal '01.05.01'" do
59
+ context "with '%E.%m.%d'" do
60
+ it { expect(subject.to_era("%E.%m.%d")).to eq "01.05.01" }
61
+ end
62
+ end
63
+
64
+ RSpec.shared_examples "should equal '0105'" do
65
+ context "with '%E%m'" do
66
+ it { expect(subject.to_era("%E%m")).to eq "0105" }
67
+ end
68
+ end
69
+
70
+ RSpec.shared_examples "should equal '令和010501'" do
71
+ context "with '%O%E%m%d'" do
72
+ it { expect(subject.to_era("%O%E%m%d")).to eq "令和010501" }
73
+ end
74
+ end
75
+
76
+ RSpec.shared_examples "should equal '令010501'" do
77
+ context "with '%O%E%m%d' and era_names option" do
78
+ let(:format) { "%O%E%m%d" }
79
+
80
+ context "when era_names is correct" do
81
+ let(:era_names) { { reiwa: ["R", "令"] } }
82
+ it { expect(subject.to_era(format, era_names: era_names)).to eq '令010501' }
83
+ end
84
+
85
+ context "when era_names have no heisei values" do
86
+ let(:era_names) { { heisei: ["H", "平"] } }
87
+ it { expect { subject.to_era(format, era_names: era_names) }.to raise_error(KeyError) }
88
+ end
89
+ end
90
+ end
91
+
92
+ RSpec.shared_examples "should equal '2019年05月01日'" do
93
+ context "with '%y年%m月%d日'" do
94
+ it { expect(subject.to_era('%Y年%m月%d日')).to eq '2019年05月01日' }
95
+ end
96
+ end
97
+
98
+ RSpec.shared_examples "should equal '令和元年五月一日'" do
99
+ context "with '%O%JE年%Jm月%Jd日'" do
100
+ it { expect(subject.to_era('%O%JE年%Jm月%Jd日')).to eq '令和元年五月一日' }
101
+ end
102
+ end
103
+
104
+ RSpec.shared_examples "should equal '二千十九年五月一日'" do
105
+ context "with '%JY年%Jm月%Jd日'" do
106
+ it { expect(subject.to_era('%JY年%Jm月%Jd日')).to eq '二千十九年五月一日' }
107
+ end
108
+ end
109
+
110
+
111
+ RSpec.shared_examples "should equal '令和'" do
112
+ context "with '%O'" do
113
+ it { expect(subject.to_era('%O')).to eq '令和' }
114
+ end
115
+ end
116
+
117
+ RSpec.shared_examples "should equal '令'" do
118
+ context "with '%1O'" do
119
+ it { expect(subject.to_era('%1O')).to eq '令' }
120
+ end
121
+ end
122
+
123
+ RSpec.shared_examples "should equal 'R'" do
124
+ context "with '%o'" do
125
+ it { expect(subject.to_era('%o')).to eq 'R' }
126
+ end
127
+ end
128
+
129
+ RSpec.shared_examples "should equal '令和二年一月一日'" do
130
+ context "with '%O%JE年%Jm月%Jd日'" do
131
+ it { expect(subject.to_era('%O%JE年%Jm月%Jd日')).to eq '令和二年一月一日' }
132
+ end
133
+ end
134
+
32
135
  RSpec.shared_examples "should equal '平成三十一年四月三十日'" do
33
136
  context "with '%O%JE年%Jm月%Jd日'" do
34
137
  it { expect(subject.to_era("%O%JE年%Jm月%Jd日")).to eq "平成三十一年四月三十日" }
@@ -209,7 +312,11 @@ RSpec.shared_examples "should equal 'M01.09.08'" do
209
312
  end
210
313
 
211
314
  RSpec.shared_examples "should raise error" do
212
- it { expect {subject.to_era}.to raise_error(RuntimeError, EraJa::Conversion::ERR_DATE_OUT_OF_RANGE) }
315
+ it { expect {subject.to_era}.to raise_error(EraJa::DateOutOfRangeError, EraJa::DateOutOfRangeError::ERR_DATE_OUT_OF_RANGE) }
316
+ end
317
+
318
+ RSpec.shared_examples "2020,1,1" do
319
+ include_examples "should equal '令和二年一月一日'"
213
320
  end
214
321
 
215
322
  RSpec.shared_examples "2019,7,2" do
@@ -219,7 +326,19 @@ end
219
326
 
220
327
  RSpec.shared_examples "2019,5,1" do
221
328
  include_examples "should equal 'R01.05.01'"
329
+ include_examples "should equal 'r01.05.01'"
222
330
  include_examples "should equal '令和01年05月01日'"
331
+ include_examples "should equal '令01年05月01日'"
332
+ include_examples "should equal '01.05.01'"
333
+ include_examples "should equal '0105'"
334
+ include_examples "should equal '令和010501'"
335
+ include_examples "should equal '令010501'"
336
+ include_examples "should equal '2019年05月01日'"
337
+ include_examples "should equal '令和元年五月一日'"
338
+ include_examples "should equal '二千十九年五月一日'"
339
+ include_examples "should equal '令和'"
340
+ include_examples "should equal '令'"
341
+ include_examples "should equal 'R'"
223
342
  end
224
343
 
225
344
  RSpec.shared_examples "2019,4,30" do
@@ -5,6 +5,11 @@ require File.expand_path('spec_helper', File.dirname(__FILE__))
5
5
  RSpec.describe Time do
6
6
  describe "#to_era" do
7
7
 
8
+ context 'time is 2020,1,1' do
9
+ subject { Time.mktime(2020,1,1) }
10
+ include_examples "2020,1,1"
11
+ end
12
+
8
13
  context 'time is 2019,7,2' do
9
14
  subject { Time.mktime(2019,7,2) }
10
15
  include_examples "2019,7,2"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: era_ja
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - tomi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-04-01 00:00:00.000000000 Z
11
+ date: 2019-04-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -115,6 +115,7 @@ files:
115
115
  - lib/era_ja.rb
116
116
  - lib/era_ja/conversion.rb
117
117
  - lib/era_ja/date.rb
118
+ - lib/era_ja/error.rb
118
119
  - lib/era_ja/time.rb
119
120
  - lib/era_ja/version.rb
120
121
  - spec/date_spec.rb
@@ -139,7 +140,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
139
140
  - !ruby/object:Gem::Version
140
141
  version: '0'
141
142
  requirements: []
142
- rubygems_version: 3.0.3
143
+ rubygems_version: 3.0.1
143
144
  signing_key:
144
145
  specification_version: 4
145
146
  summary: Convert Date or Time instance to String of Japanese era.