era_ja 0.4.1 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -0
- data/README.ja.md +7 -0
- data/README.md +23 -15
- data/lib/era_ja/conversion.rb +18 -12
- data/lib/era_ja/version.rb +1 -1
- data/spec/spec_helper.rb +25 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3dc968407d2aa1d8d89f19820529c1ec39b646bf
|
4
|
+
data.tar.gz: 7c695884e466ccb5cd18fcdb60a5ac3b87bdda1f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 058f66ea666f5f3a158061d26fcb0a7f696ae1973cbc3b0adb2150bddb2e0f0b1ff03d34247375df55a6974cc99e61fbbb13c76d5657213cdeb44445a0d2e1c3
|
7
|
+
data.tar.gz: 21f3aa1bd3ab10df432c69d827d6624d8f1df1b548ae562c0304577ec3f04cf6d944f9e0084ec06780af0d7be6ae8ad79fd453b7811fd6e7471c9bdc2e152ca6
|
data/.travis.yml
CHANGED
data/README.ja.md
CHANGED
@@ -62,6 +62,13 @@ Date.new(2012,4,29).to_era("%Jy年%Jm月%Jd日") # => "二千十二年四月
|
|
62
62
|
|
63
63
|
### Era names ###
|
64
64
|
|
65
|
+
もし1桁の元号文字列でコンバートしたい場合、 `%1O` を使用することができます。
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
require 'era_ja'
|
69
|
+
Time.mktime(2012,4,29).to_era("%1O%E年%m月%d日") # => "平24年4月29日"
|
70
|
+
```
|
71
|
+
|
65
72
|
もしデフォルトの元号文字列以外の文字列でコンバートしたい場合、 `era_names` を使用することができます。
|
66
73
|
|
67
74
|
```ruby
|
data/README.md
CHANGED
@@ -26,16 +26,17 @@ See [.travis.yml](https://github.com/tomiacannondale/era_ja/blob/master/.travis.
|
|
26
26
|
|
27
27
|
### format string
|
28
28
|
|
29
|
-
|
29
|
+
strftime format string can be used as an argument.
|
30
30
|
|
31
|
-
|
31
|
+
Extra format strings provided by EraJa#to_era are:
|
32
32
|
|
33
|
-
* %o:
|
34
|
-
* %O:
|
35
|
-
* %
|
36
|
-
* %
|
33
|
+
* %o: era(alphabet)
|
34
|
+
* %O: era(kanzi)
|
35
|
+
* %1O: era(single kanzi)
|
36
|
+
* %E: era year
|
37
|
+
* %J: kanzi numeral
|
37
38
|
|
38
|
-
### Time instance to
|
39
|
+
### Time instance to Japanese era
|
39
40
|
|
40
41
|
```ruby
|
41
42
|
require 'era_ja'
|
@@ -44,7 +45,7 @@ Time.mktime(2012,4,29).to_era("%O%E年%m月%d日") # => "平成24年4月29日"
|
|
44
45
|
Time.mktime(2012,4,29).to_era("%O%JE年%Jm月%Jd日") # => "平成二十四年四月二十九日"
|
45
46
|
```
|
46
47
|
|
47
|
-
### Date instance to
|
48
|
+
### Date instance to Japanese era
|
48
49
|
|
49
50
|
```ruby
|
50
51
|
require 'era_ja'
|
@@ -53,7 +54,7 @@ Date.new(2012,4,29).to_era("%O%E年%m月%d日") # => "平成24年4月29日"
|
|
53
54
|
Date.new(2012,4,29).to_era("%O%JE年%Jm月%Jd日") # => "平成二十四年四月二十九日"
|
54
55
|
```
|
55
56
|
|
56
|
-
###
|
57
|
+
### Convert numerals to kanzi ###
|
57
58
|
|
58
59
|
```ruby
|
59
60
|
Time.mktime(2012,4,29).to_era("%Jy年%Jm月%Jd日") # => "二千十二年四月二十九日"
|
@@ -62,7 +63,14 @@ Date.new(2012,4,29).to_era("%Jy年%Jm月%Jd日") # => "二千十二年四月
|
|
62
63
|
|
63
64
|
### Era names ###
|
64
65
|
|
65
|
-
|
66
|
+
To convert to single kanji era strings, you can use `%1O`.
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
require 'era_ja'
|
70
|
+
Time.mktime(2012,4,29).to_era("%1O%E年%m月%d日") # => "平24年4月29日"
|
71
|
+
```
|
72
|
+
|
73
|
+
To convert to custom era strings, you can set `era_names` .
|
66
74
|
|
67
75
|
```ruby
|
68
76
|
require 'era_ja'
|
@@ -82,14 +90,14 @@ Report issues and feature requests to github Issues. https://github.com/tomiacan
|
|
82
90
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
83
91
|
3. Commit your changes (`git commit -am 'Added some feature'`)
|
84
92
|
4. Push to the branch (`git push origin my-new-feature`)
|
85
|
-
5. Create new Pull Request
|
93
|
+
5. Create a new Pull Request
|
86
94
|
|
87
95
|
## Note
|
88
96
|
|
89
|
-
|
97
|
+
The following issues exist regarding era:
|
90
98
|
|
91
|
-
* The
|
92
|
-
*
|
99
|
+
* The Meiji era starts at September 8, 1866, but that date is in the old Japanese lunar calendar.
|
100
|
+
* The start dates of the Taisho and Showa eras are the same as the end dates of the Meiji and Taisho eras respectively.
|
93
101
|
* [昭和ト改元](http://ja.wikisource.org/wiki/%e6%98%ad%e5%92%8c%e3%83%88%e6%94%b9%e5%85%83)
|
94
102
|
* [大正十五年十二月ニ十五日以後ヲ改メテ昭和元年ト爲ス](http://ja.wikisource.org/wiki/%e5%a4%a7%e6%ad%a3%e5%8d%81%e4%ba%94%e5%b9%b4%e5%8d%81%e4%ba%8c%e6%9c%88%e3%83%8b%e5%8d%81%e4%ba%94%e6%97%a5%e4%bb%a5%e5%be%8c%e3%83%b2%e6%94%b9%e3%83%a1%e3%83%86%e6%98%ad%e5%92%8c%e5%85%83%e5%b9%b4%e3%83%88%e7%88%b2%e3%82%b9)
|
95
103
|
* [明治45年(1912)7月|大正と改元:日本のあゆみ](http://www.archives.go.jp/ayumi/kobetsu/m45_1912_01.html)
|
@@ -100,4 +108,4 @@ tomi tomiacannondale@gmail.com
|
|
100
108
|
|
101
109
|
## License
|
102
110
|
|
103
|
-
MIT License. For more
|
111
|
+
MIT License. For more information, please see LICENSE.
|
data/lib/era_ja/conversion.rb
CHANGED
@@ -10,6 +10,8 @@ module EraJa
|
|
10
10
|
heisei: ["H", "平成"]
|
11
11
|
}
|
12
12
|
|
13
|
+
ERR_DATE_OUT_OF_RANGE = "#to_era only works on dates after 1868,9,8"
|
14
|
+
|
13
15
|
# Convert to Japanese era.
|
14
16
|
# @param [String] format_string
|
15
17
|
# Time#strftime format string can be used
|
@@ -19,22 +21,23 @@ module EraJa
|
|
19
21
|
# * %E - era year
|
20
22
|
# * %J - kanzi number
|
21
23
|
# @param [Hash] era_names
|
22
|
-
# If you want to convert custom era
|
24
|
+
# If you want to convert custom to era strings (eg `平`, `h`), you can set this argument.
|
23
25
|
# key is `:meiji' or `:taisho' or `:showa` or `:heisei`.
|
24
|
-
# value is ["alphabet era name"(ex `h`, `s`)(related to `%o`), "multibyte
|
26
|
+
# value is ["alphabet era name"(ex `h`, `s`)(related to `%o`), "multibyte era name"(eg `平`, `昭`)(related to `%O`)].
|
25
27
|
# this argument is same as one element of ERA_NAME_DEFAULTS.
|
26
28
|
# @return [String]
|
27
29
|
def to_era(format = "%o%E.%m.%d", era_names: ERA_NAME_DEFAULTS)
|
28
30
|
@era_format = format.gsub(/%J/, "%J%")
|
29
31
|
str_time = strftime(@era_format)
|
30
32
|
if @era_format =~ /%E/
|
31
|
-
|
32
|
-
|
33
|
-
|
33
|
+
case
|
34
|
+
when self.to_time < ::Time.mktime(1868,9,8)
|
35
|
+
raise ERR_DATE_OUT_OF_RANGE
|
36
|
+
when self.to_time < ::Time.mktime(1912,7,30)
|
34
37
|
str_time = era_year(year - 1867, :meiji, era_names)
|
35
|
-
|
38
|
+
when self.to_time < ::Time.mktime(1926,12,25)
|
36
39
|
str_time = era_year(year - 1911, :taisho, era_names)
|
37
|
-
|
40
|
+
when self.to_time < ::Time.mktime(1989,1,8)
|
38
41
|
str_time = era_year(year - 1925, :showa, era_names)
|
39
42
|
else
|
40
43
|
str_time = era_year(year - 1988, :heisei, era_names)
|
@@ -45,14 +48,17 @@ module EraJa
|
|
45
48
|
|
46
49
|
private
|
47
50
|
def era_year(year, era, era_names)
|
48
|
-
strftime(@era_format).sub(/(%J)?%E/) { format_year(year, $1) }.sub(/%o/i) { format_era(era, era_names) }
|
51
|
+
strftime(@era_format).sub(/(%J)?%E/) { format_year(year, $1) }.sub(/%1?o/i) { format_era(era, era_names) }
|
49
52
|
end
|
50
53
|
|
51
54
|
def format_era(era, era_names)
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
55
|
+
case
|
56
|
+
when @era_format =~ /%o/
|
57
|
+
era_names.fetch(era)[0]
|
58
|
+
when @era_format =~ /%1O/
|
59
|
+
era_names.fetch(era)[1][0]
|
60
|
+
when @era_format =~ /%O/
|
61
|
+
era_names.fetch(era)[1]
|
56
62
|
end
|
57
63
|
end
|
58
64
|
|
data/lib/era_ja/version.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -18,7 +18,7 @@ RSpec.shared_examples "should equal 'h24.04.29'" do
|
|
18
18
|
|
19
19
|
context "when era_names have no heisei values" do
|
20
20
|
let(:era_names) { { showa: ["s", "昭和"] } }
|
21
|
-
it { expect { subject.to_era(era_names: era_names) }.to raise_error }
|
21
|
+
it { expect { subject.to_era(era_names: era_names) }.to raise_error(KeyError) }
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
@@ -34,6 +34,12 @@ RSpec.shared_examples "should equal '平成24年04月29日'" do
|
|
34
34
|
end
|
35
35
|
end
|
36
36
|
|
37
|
+
RSpec.shared_examples "should equal '平24年04月29日'" do
|
38
|
+
context "with '%1O%E年%m月%d日'" do
|
39
|
+
it { expect(subject.to_era("%1O%E年%m月%d日")).to eq "平24年04月29日" }
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
37
43
|
RSpec.shared_examples "should equal '24.04.29'" do
|
38
44
|
context "with '%E.%m.%d'" do
|
39
45
|
it { expect(subject.to_era("%E.%m.%d")).to eq "24.04.29" }
|
@@ -63,7 +69,7 @@ RSpec.shared_examples "should equal '平240429'" do
|
|
63
69
|
|
64
70
|
context "when era_names have no heisei values" do
|
65
71
|
let(:era_names) { { showa: ["S", "昭"] } }
|
66
|
-
it { expect { subject.to_era(format, era_names: era_names) }.to raise_error }
|
72
|
+
it { expect { subject.to_era(format, era_names: era_names) }.to raise_error(KeyError) }
|
67
73
|
end
|
68
74
|
end
|
69
75
|
end
|
@@ -104,6 +110,10 @@ RSpec.shared_examples "should equal '昭和64.01.07'" do
|
|
104
110
|
it { expect(subject.to_era("%O%E.%m.%d")).to eq "昭和64.01.07" }
|
105
111
|
end
|
106
112
|
|
113
|
+
RSpec.shared_examples "should equal '昭64.01.07'" do
|
114
|
+
it { expect(subject.to_era("%1O%E.%m.%d")).to eq "昭64.01.07" }
|
115
|
+
end
|
116
|
+
|
107
117
|
RSpec.shared_examples "should equal 'S01.12.25'" do
|
108
118
|
it { expect(subject.to_era).to eq "S01.12.25" }
|
109
119
|
end
|
@@ -120,6 +130,10 @@ RSpec.shared_examples "should equal '大正01.07.30'" do
|
|
120
130
|
it { expect(subject.to_era("%O%E.%m.%d")).to eq "大正01.07.30" }
|
121
131
|
end
|
122
132
|
|
133
|
+
RSpec.shared_examples "should equal '大01.07.30'" do
|
134
|
+
it { expect(subject.to_era("%1O%E.%m.%d")).to eq "大01.07.30" }
|
135
|
+
end
|
136
|
+
|
123
137
|
RSpec.shared_examples "should equal 'M45.07.29'" do
|
124
138
|
it { expect(subject.to_era).to eq "M45.07.29" }
|
125
139
|
end
|
@@ -128,18 +142,23 @@ RSpec.shared_examples "should equal '明治45.07.29'" do
|
|
128
142
|
it { expect(subject.to_era("%O%E.%m.%d")).to eq "明治45.07.29" }
|
129
143
|
end
|
130
144
|
|
145
|
+
RSpec.shared_examples "should equal '明45.07.29'" do
|
146
|
+
it { expect(subject.to_era("%1O%E.%m.%d")).to eq "明45.07.29" }
|
147
|
+
end
|
148
|
+
|
131
149
|
RSpec.shared_examples "should equal 'M01.09.08'" do
|
132
150
|
it { expect(subject.to_era).to eq "M01.09.08" }
|
133
151
|
end
|
134
152
|
|
135
153
|
RSpec.shared_examples "should raise error" do
|
136
|
-
it { expect {subject.to_era}.to raise_error(RuntimeError,
|
154
|
+
it { expect {subject.to_era}.to raise_error(RuntimeError, EraJa::Conversion::ERR_DATE_OUT_OF_RANGE) }
|
137
155
|
end
|
138
156
|
|
139
157
|
RSpec.shared_examples "2012,4,29" do
|
140
158
|
include_examples "should equal 'H24.04.29'"
|
141
159
|
include_examples "should equal 'h24.04.29'"
|
142
160
|
include_examples "should equal '平成24年04月29日'"
|
161
|
+
include_examples "should equal '平24年04月29日'"
|
143
162
|
include_examples "should equal '24.04.29'"
|
144
163
|
include_examples "should equal '2404'"
|
145
164
|
include_examples "should equal '平成240429'"
|
@@ -156,6 +175,7 @@ end
|
|
156
175
|
RSpec.shared_examples "1989,1,7" do
|
157
176
|
include_examples "should equal 'S64.01.07'"
|
158
177
|
include_examples "should equal '昭和64.01.07'"
|
178
|
+
include_examples "should equal '昭64.01.07'"
|
159
179
|
end
|
160
180
|
|
161
181
|
RSpec.shared_examples "1926,12,25" do
|
@@ -169,11 +189,13 @@ end
|
|
169
189
|
RSpec.shared_examples "1912,7,30" do
|
170
190
|
include_examples "should equal 'T01.07.30'"
|
171
191
|
include_examples "should equal '大正01.07.30'"
|
192
|
+
include_examples "should equal '大01.07.30'"
|
172
193
|
end
|
173
194
|
|
174
195
|
RSpec.shared_examples "1912,7,29" do
|
175
196
|
include_examples "should equal 'M45.07.29'"
|
176
197
|
include_examples "should equal '明治45.07.29'"
|
198
|
+
include_examples "should equal '明45.07.29'"
|
177
199
|
end
|
178
200
|
|
179
201
|
RSpec.shared_examples "1868,9,8" do
|
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: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- tomi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-02-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|