era_ja 1.2.1 → 1.3.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: cd72f6ec4123617ec6561a3b45e15eb7d36f9af1b630ea41dd2d50bf5fb2f25d
4
- data.tar.gz: 62a3839d6169ef0e5c729ccccab005d64546abfb1a26124d2009662dfa7c6efe
3
+ metadata.gz: 6a552f23df1a4319bbe9585c7b9f9645690b90ee082dffad9fc0f0efa6030627
4
+ data.tar.gz: ce4b3e65b6c73991907adf6d06fb588b62362514cc573dd6abdc11e30530fb7b
5
5
  SHA512:
6
- metadata.gz: 6650611f6f15bcf1c2e65824b288600be79a2e75cd56689ca075ba4de9729072f6da7751df2ef45eaba31588fee1ca21bb0301d1c965b698be5355a5198e97ee
7
- data.tar.gz: 5928320797dcbde74fbef0556ec33879b4cad7ae87e73c449dce515a42ab13c0caf654bfba4eb5fc598784cca949540efc7a1ceb97807baaeb4b6f1097259699
6
+ metadata.gz: b4e7947f800cab967377dd3d46fcd337c7f6e2e058b2bdf86c111d0aec972992c84527925cdde5e922729f534cb8dfdcb3fd62095594036c1dc266c481f1c04f
7
+ data.tar.gz: bba31ebdfe4b9cc95016501f1173d5316ad5da385cda6703e73edaeb28bb5a367a3c6067a5863df8480156092922a964758e9732d80f93a86eeb700537967d5f
data/README.ja.md CHANGED
@@ -26,7 +26,7 @@
26
26
 
27
27
  ### フォーマット文字列
28
28
 
29
- 引数としてstftimeのフォーマット文字列を使用出来ます。
29
+ 引数としてstrftimeのフォーマット文字列を使用出来ます。
30
30
 
31
31
  EraJa#to_eraメソッドでの追加のフォーマット文字列は以下の通りです。
32
32
 
@@ -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,9 +34,9 @@ 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
- if @era_format =~ /%([EOo]|1O)/
39
+ if @era_format =~ /%(-?E|[Oo]|1O)/
34
40
  case
35
41
  when self.to_time < ::Time.mktime(1912,7,30)
36
42
  str_time = era_year(year - 1867, :meiji, era_names)
@@ -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.1"
2
+ VERSION = "1.3.0"
3
3
  end
data/spec/spec_helper.rb CHANGED
@@ -26,6 +26,14 @@ RSpec.shared_examples "should equal '令和01年07月02日'" do
26
26
  context "with '%O%J%-E年%m月%d日'" do
27
27
  it { expect(subject.to_era("%O%J%-E年%m月%d日")).to eq "令和元年07月02日" }
28
28
  end
29
+
30
+ context "with '%-E年%-m月%-d日'" do
31
+ it { expect(subject.to_era("%-E年%-m月%-d日")).to eq "1年7月2日" }
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
29
37
  end
30
38
 
31
39
  RSpec.shared_examples "should equal 'R01.05.01'" do
@@ -59,6 +67,10 @@ RSpec.shared_examples "should equal '令和01年05月01日'" do
59
67
  context "with '%O%E年%m月%d日'" do
60
68
  it { expect(subject.to_era("%O%E年%m月%d日")).to eq "令和01年05月01日" }
61
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
62
74
  end
63
75
 
64
76
  RSpec.shared_examples "should equal '令01年05月01日'" do
@@ -142,6 +154,10 @@ RSpec.shared_examples "should equal '令和二年一月一日'" do
142
154
  context "with '%O%JE年%Jm月%Jd日'" do
143
155
  it { expect(subject.to_era('%O%JE年%Jm月%Jd日')).to eq '令和二年一月一日' }
144
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
145
161
  end
146
162
 
147
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.1
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - tomi
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-03-23 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
@@ -125,7 +125,7 @@ homepage: https://github.com/tomiacannondale/era_ja
125
125
  licenses:
126
126
  - MIT
127
127
  metadata: {}
128
- post_install_message:
128
+ post_install_message:
129
129
  rdoc_options: []
130
130
  require_paths:
131
131
  - lib
@@ -140,8 +140,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
140
140
  - !ruby/object:Gem::Version
141
141
  version: '0'
142
142
  requirements: []
143
- rubygems_version: 3.1.2
144
- signing_key:
143
+ rubygems_version: 3.1.4
144
+ signing_key:
145
145
  specification_version: 4
146
146
  summary: Convert Date or Time instance to String of Japanese era.
147
147
  test_files: