japanese_calendar 0.3.0 → 0.3.1

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
  SHA1:
3
- metadata.gz: 0d5904f38ba5b838626a46e31be45e2a73329a3d
4
- data.tar.gz: 814f791883585675c6799577b5a4c3d1bf0a0b62
3
+ metadata.gz: 32d4ce4f513c811d200136af280c962486e32bd5
4
+ data.tar.gz: 95395e12fd637cf92b479d1ee14cde9f090aa969
5
5
  SHA512:
6
- metadata.gz: 853d9e4c2dd6a751f0e45ba58fb432f6e346b4ffbdfdd77f5996ae694e72e2f16a398ae2e85a0b6e8496e8de80e43c249f024136cdf2993f2df8209febda7167
7
- data.tar.gz: 79dd32bb385d535d081a94400065cef92d7066d7e1d3097afc33473724e9b75d9db1c6580d2727e7717b2875ef10968176010a3a44fedbe19ed1e4e87bb1c0da
6
+ metadata.gz: a8665096c0364ab4a25ff043ba4e3a2ed23f60c8c7a7f5b641315621ff2a9a417375afead38a702727d1f5c8fc8a2da198ade9f4770a1b935a0f3828e2bba088
7
+ data.tar.gz: f2fadd2f711e4524bcb880e77bdd0d5bc4eb6ffe05e455a1bd19b84ea3d0ab3a9d5f996c87f1dfbff8db1992e8cb307785c5bec6765a410d68cb9ab00289b7d1
data/README.md CHANGED
@@ -20,7 +20,7 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
- To get a Japanese era name, use the `era_name` method:
23
+ To get the Japanese era name, use the `era_name` method:
24
24
 
25
25
  ```
26
26
  Time.new(1989, 1, 8).era_name # => "平成"
@@ -29,6 +29,15 @@ Time.new(1912, 7, 30).era_name # => "大正"
29
29
  Time.new(1873, 1, 1).era_name # => "明治"
30
30
  ```
31
31
 
32
+ If you want to get the Japanese era name in romaji, pass `:romaji`:
33
+
34
+ ```
35
+ Time.new(1989, 1, 8).era_name(:romaji) # => "Heisei"
36
+ Time.new(1926, 12, 25).era_name(:romaji) # => "Showa"
37
+ Time.new(1912, 7, 30).era_name(:romaji) # => "Taisho"
38
+ Time.new(1873, 1, 1).era_name(:romaji) # => "Meiji"
39
+ ```
40
+
32
41
  You can convert to a Japanese year with the `era_year` method:
33
42
 
34
43
  ```
@@ -58,8 +67,8 @@ time.strftime("%q") # => "日"
58
67
 
59
68
  # More examples
60
69
  time.strftime("%K%-J年%-m月%-d日(%q)") # => "平成1年1月1日(日)"
61
- time.strftime("%o%J.%m.%d") # => H01.01.01
62
- time.strftime("%B %-d, %-Y (%O %-J)") # => January 1, 1989 (Heisei 1)
70
+ time.strftime("%o%J.%m.%d") # => "H01.01.01"
71
+ time.strftime("%B %-d, %-Y (%O %-J)") # => "January 1, 1989 (Heisei 1)"
63
72
  ```
64
73
 
65
74
  ## Development
@@ -3,17 +3,31 @@ module JapaneseCalendar
3
3
  module Era
4
4
  # Returns the Japanese era name (nengo) since 1 January 1873 (Meiji 6).
5
5
  #
6
- # Time.new(1989, 1, 8).era_name # => "平成"
7
- # Time.new(1926, 12, 25).era_name # => "昭和"
8
- # Time.new(1912, 7, 30).era_name # => "大正"
9
- # Time.new(1873, 1, 1).era_name # => "明治"
6
+ # heisei = Time.new(1989, 1, 8) # => 1989-01-08 00:00:00 +0900
7
+ # heisei.era_name # => "平成"
8
+ # heisei.era_name(:romaji) # => "Heisei"
9
+ #
10
+ # showa = Time.new(1926, 12, 25) # => 1926-12-25 00:00:00 +0900
11
+ # showa.era_name # => "昭和"
12
+ # showa.era_name(:romaji) # => "Showa"
13
+ #
14
+ # taisho = Time.new(1912, 7, 30) # => 1912-07-30 00:00:00 +0900
15
+ # taisho.era_name # => "大正"
16
+ # taisho.era_name(:romaji) # => "Taisho"
17
+ #
18
+ # meiji = Time.new(1873, 1, 1) # => 1873-01-01 00:00:00 +0900
19
+ # meiji.era_name # => "明治"
20
+ # meiji.era_name(:romaji) # => "Meiji"
10
21
  #
11
22
  # Raises an error when the Japanese era name cannot be found.
12
23
  #
13
24
  # Time.new(1872, 12, 31).era_name # => RuntimeError
14
25
  def era_name(character = :kanji)
15
- raise ArgumentError unless %i(kanji romaji).include?(character)
16
- find_era[:name][character]
26
+ unless %i(kanji romaji).include?(character)
27
+ raise ArgumentError, "invalid character"
28
+ end
29
+
30
+ current_era.send("#{character}_name")
17
31
  end
18
32
 
19
33
  # Returns the Japanese year since 1 January 1873 (Meiji 6).
@@ -27,7 +41,7 @@ module JapaneseCalendar
27
41
  #
28
42
  # Time.new(1872, 12, 31).era_year # => RuntimeError
29
43
  def era_year
30
- year - find_era[:beginning_of_period].year + 1
44
+ year - current_era.beginning_of_period.year + 1
31
45
  end
32
46
 
33
47
  # Formats time according to the directives in the given format string.
@@ -58,20 +72,21 @@ module JapaneseCalendar
58
72
  end
59
73
 
60
74
  private
75
+ Period = Struct.new(:beginning_of_period, :kanji_name, :romaji_name)
76
+
61
77
  MEIJI_6 = Time.new(1873, 1, 1)
62
78
 
63
79
  PERIODS = [
64
- { name: { kanji: "平成", romaji: "Heisei" }, beginning_of_period: Time.new(1989, 1, 8) },
65
- { name: { kanji: "昭和", romaji: "Showa" }, beginning_of_period: Time.new(1926, 12, 25) },
66
- { name: { kanji: "大正", romaji: "Taisho" }, beginning_of_period: Time.new(1912, 7, 30) },
67
- { name: { kanji: "明治", romaji: "Meiji" }, beginning_of_period: Time.new(1868, 1, 25) }
80
+ Period.new(Time.new(1989, 1, 8), "平成", "Heisei").freeze,
81
+ Period.new(Time.new(1926, 12, 25), "昭和", "Showa" ).freeze,
82
+ Period.new(Time.new(1912, 7, 30), "大正", "Taisho").freeze,
83
+ Period.new(Time.new(1868, 1, 25), "明治", "Meiji" ).freeze
68
84
  ].freeze
69
85
 
70
- def find_era
71
- raise "#{self.class.name.downcase} out of range" if self < MEIJI_6
72
-
73
- ifnone = proc { raise "#{self.class.name.downcase} out of range" }
74
- PERIODS.find(ifnone) { |period| period[:beginning_of_period] <= self }
86
+ def current_era
87
+ error_proc = proc { raise "#{self.class.name.downcase} out of range" }
88
+ error_proc.call if self < MEIJI_6
89
+ PERIODS.find(error_proc) { |period| period.beginning_of_period <= self }
75
90
  end
76
91
  end
77
92
  end
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module JapaneseCalendar
3
- VERSION = "0.3.0"
3
+ VERSION = "0.3.1"
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: japanese_calendar
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Yamamoto