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 +4 -4
- data/README.md +12 -3
- data/lib/japanese_calendar/era.rb +31 -16
- data/lib/japanese_calendar/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 32d4ce4f513c811d200136af280c962486e32bd5
|
4
|
+
data.tar.gz: 95395e12fd637cf92b479d1ee14cde9f090aa969
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
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,
|
7
|
-
#
|
8
|
-
#
|
9
|
-
#
|
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
|
-
|
16
|
-
|
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 -
|
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
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
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
|
71
|
-
raise "#{self.class.name.downcase} out of range"
|
72
|
-
|
73
|
-
|
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
|