era_ja 1.2.2 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bcf0a0de20e3f4e090835b70d95bd314e00969368a7d11930d4f138b10a5a479
4
- data.tar.gz: 3faa10bd05537ebe7ac6bd279e32576701b8369ad1ac72c8198f9073fe0a1aa2
3
+ metadata.gz: 6a552f23df1a4319bbe9585c7b9f9645690b90ee082dffad9fc0f0efa6030627
4
+ data.tar.gz: ce4b3e65b6c73991907adf6d06fb588b62362514cc573dd6abdc11e30530fb7b
5
5
  SHA512:
6
- metadata.gz: fbbb0bf965c38428ba6c63057ab1c49b1dad5912c46480440386e6c5ce01e39185fa4e65b9de8ac990928d818308fb6183b42b1327192ae52b37da7f578a99bf
7
- data.tar.gz: d2a6df2467587f964a74d869de4e59cae8da84ca062b3dda6f61074be7420d212c97d4111d697046f7b92a551b2059b90b9a4e29975520e0b37bb5c074b47ec6
6
+ metadata.gz: b4e7947f800cab967377dd3d46fcd337c7f6e2e058b2bdf86c111d0aec972992c84527925cdde5e922729f534cb8dfdcb3fd62095594036c1dc266c481f1c04f
7
+ data.tar.gz: bba31ebdfe4b9cc95016501f1173d5316ad5da385cda6703e73edaeb28bb5a367a3c6067a5863df8480156092922a964758e9732d80f93a86eeb700537967d5f
data/README.ja.md CHANGED
@@ -35,6 +35,7 @@ EraJa#to_eraメソッドでの追加のフォーマット文字列は以下の
35
35
  * %1O: 元号(漢字1文字)
36
36
  * %E: 元号の年
37
37
  * %-E: 元号の年(1から9まで1ケタ)
38
+ * %K: 元号の年(漢数字) 2年目以降は数字
38
39
  * %J: 漢数字
39
40
 
40
41
  ### Timeインスタンスを和暦に変換します。
@@ -63,6 +64,15 @@ Time.mktime(2012,4,29).to_era("%JY年%Jm月%Jd日") # => "二千十二年四月
63
64
  Date.new(2012,4,29).to_era("%JY年%Jm月%Jd日") # => "二千十二年四月二十九日"
64
65
  ```
65
66
 
67
+ もし1年目の場合は `元` 2年目以降を数字にしたい場合は `%K` を使用することができます。
68
+
69
+ ```ruby
70
+ Time.mktime(2019,5,1).to_era("%O%KE年%m月%d日") # => "令和元年05月01日"
71
+ Time.mktime(2020,5,1).to_era("%O%KE年%m月%d日") # => "令和02年05月01日"
72
+ Date.new(2019,5,1).to_era("%O%KE年%m月%d日") # => "令和元年05月01日"
73
+ Date.new(2020,5,1).to_era("%O%KE年%m月%d日") # => "令和02年05月01日"
74
+ ```
75
+
66
76
  ### Era names ###
67
77
 
68
78
  もし1桁の元号文字列でコンバートしたい場合、 `%1O` を使用することができます。
data/README.md CHANGED
@@ -35,6 +35,7 @@ Extra format strings provided by EraJa#to_era are:
35
35
  * %1O: era(single kanzi)
36
36
  * %E: era year
37
37
  * %-E: era year(single digit format from 1 to 9)
38
+ * %K: era year(kanzi numeral) After the second year, make it a numeral
38
39
  * %J: kanzi numeral
39
40
 
40
41
  ### Time instance to Japanese era
@@ -62,6 +63,15 @@ Time.mktime(2012,4,29).to_era("%JY年%Jm月%Jd日") # => "二千十二年四月
62
63
  Date.new(2012,4,29).to_era("%JY年%Jm月%Jd日") # => "二千十二年四月二十九日"
63
64
  ```
64
65
 
66
+ The first year to be `元` and the second and subsequent years to be numbers, you can use `%K`.
67
+
68
+ ```ruby
69
+ Time.mktime(2019,5,1).to_era("%O%KE年%m月%d日") # => "令和元年05月01日"
70
+ Time.mktime(2020,5,1).to_era("%O%KE年%m月%d日") # => "令和02年05月01日"
71
+ Date.new(2019,5,1).to_era("%O%KE年%m月%d日") # => "令和元年05月01日"
72
+ Date.new(2020,5,1).to_era("%O%KE年%m月%d日") # => "令和02年05月01日"
73
+ ```
74
+
65
75
  ### Era names ###
66
76
 
67
77
  To convert to single kanji era strings, you can use `%1O`.
@@ -11,6 +11,11 @@ module EraJa
11
11
  reiwa: ["R", "令和"]
12
12
  }.freeze
13
13
 
14
+ FORMAT_STRING_MAP = {
15
+ '%J' => '%J%',
16
+ '%K' => '%K%'
17
+ }.freeze
18
+
14
19
  # Convert to Japanese era.
15
20
  # @param [String] format_string
16
21
  # Time#strftime format string can be used
@@ -19,6 +24,7 @@ module EraJa
19
24
  # * %O - era(kanzi)
20
25
  # * %E - era year
21
26
  # * %J - kanzi number
27
+ # * %K - kanzi number(After the second year, make it a number)
22
28
  # @param [Hash] era_names
23
29
  # If you want to convert custom to era strings (eg `平`, `h`), you can set this argument.
24
30
  # key is `:meiji' or `:taisho' or `:showa` or `:heisei` or `:reiwa`.
@@ -28,7 +34,7 @@ module EraJa
28
34
  def to_era(format = "%o%E.%m.%d", era_names: ERA_NAME_DEFAULTS)
29
35
  raise EraJa::DateOutOfRangeError unless era_convertible?
30
36
 
31
- @era_format = format.gsub(/%J/, "%J%")
37
+ @era_format = format.gsub(Regexp.new(FORMAT_STRING_MAP.keys.join('|')), FORMAT_STRING_MAP)
32
38
  str_time = strftime(@era_format)
33
39
  if @era_format =~ /%(-?E|[Oo]|1O)/
34
40
  case
@@ -53,7 +59,9 @@ module EraJa
53
59
 
54
60
  private
55
61
  def era_year(year, era, era_names)
56
- strftime(@era_format).sub(/(%J)?(%-?E)/) { format_year(year, $1, $2) }.sub(/%1?o/i) { format_era(era, era_names) }
62
+ strftime(@era_format)
63
+ .sub(Regexp.new("(#{FORMAT_STRING_MAP.keys.join('|')})?(%-?E)")) { format_year(year, $1, $2) }
64
+ .sub(/%1?o/i) { format_era(era, era_names) }
57
65
  end
58
66
 
59
67
  def format_era(era, era_names)
@@ -70,8 +78,10 @@ module EraJa
70
78
  def format_year(year, match, digit)
71
79
  era_year = sprintf("%02d", year)
72
80
 
73
- if match == "%J"
74
- return year == 1 ? "元" : to_kanzi(era_year)
81
+ if %w(%J %K).include?(match)
82
+ return "元" if year == 1
83
+
84
+ return to_kanzi(era_year) if match == "%J"
75
85
  end
76
86
 
77
87
  era_year = if digit == "%-E"
@@ -1,3 +1,3 @@
1
1
  module EraJa
2
- VERSION = "1.2.2"
2
+ VERSION = "1.3.0"
3
3
  end
data/spec/spec_helper.rb CHANGED
@@ -30,6 +30,10 @@ RSpec.shared_examples "should equal '令和01年07月02日'" do
30
30
  context "with '%-E年%-m月%-d日'" do
31
31
  it { expect(subject.to_era("%-E年%-m月%-d日")).to eq "1年7月2日" }
32
32
  end
33
+
34
+ context "with '%O%K%-E年%m月%d日'" do
35
+ it { expect(subject.to_era("%O%K%-E年%m月%d日")).to eq "令和元年07月02日" }
36
+ end
33
37
  end
34
38
 
35
39
  RSpec.shared_examples "should equal 'R01.05.01'" do
@@ -63,6 +67,10 @@ RSpec.shared_examples "should equal '令和01年05月01日'" do
63
67
  context "with '%O%E年%m月%d日'" do
64
68
  it { expect(subject.to_era("%O%E年%m月%d日")).to eq "令和01年05月01日" }
65
69
  end
70
+
71
+ context "with '%O%K%E年%m月%d日'" do
72
+ it { expect(subject.to_era("%O%K%E年%m月%d日")).to eq "令和元年05月01日" }
73
+ end
66
74
  end
67
75
 
68
76
  RSpec.shared_examples "should equal '令01年05月01日'" do
@@ -146,6 +154,10 @@ RSpec.shared_examples "should equal '令和二年一月一日'" do
146
154
  context "with '%O%JE年%Jm月%Jd日'" do
147
155
  it { expect(subject.to_era('%O%JE年%Jm月%Jd日')).to eq '令和二年一月一日' }
148
156
  end
157
+
158
+ context "with '%O%KE年%m月%d日'" do
159
+ it { expect(subject.to_era('%O%KE年%m月%d日')).to eq '令和02年01月01日' }
160
+ end
149
161
  end
150
162
 
151
163
  RSpec.shared_examples "should equal '平成三十一年四月三十日'" 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: 1.2.2
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - tomi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-10-28 00:00:00.000000000 Z
11
+ date: 2023-01-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake