japanese_calendar 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +1 -1
- data/Gemfile +1 -0
- data/README.md +18 -2
- data/Rakefile +1 -0
- data/bin/console +1 -0
- data/exe/japanese_calendar +1 -0
- data/japanese_calendar.gemspec +1 -0
- data/lib/japanese_calendar.rb +1 -0
- data/lib/japanese_calendar/core_ext/time.rb +2 -1
- data/lib/japanese_calendar/era.rb +47 -14
- data/lib/japanese_calendar/version.rb +2 -1
- 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: 24d6b788116b03f3cda9eb3777c06fd528990b9e
|
4
|
+
data.tar.gz: 7e51d4fa12dd788b786102db9a602ae5e76e8acc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3b778089d926467aae31b406e8f7ccef7512fd3e836078c398e6d609eed645a329fde41f41c8b5b5b5a512b1349f85f5d4d6d861b1396e683ebafb1b8bead51b
|
7
|
+
data.tar.gz: 37381cf4c47ca1cbfe0fbb9a2d54effa435dcd01e71cffa3aa0b88094580795ee8984bcb760028f43decca3454d9443949a3ead6368810009d10cc7fe27f742b
|
data/.rubocop.yml
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -20,13 +20,13 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
-
To get
|
23
|
+
To get a Japanese era name, use the `era_name` method:
|
24
24
|
|
25
25
|
```
|
26
26
|
Time.new(1989, 1, 8).era_name # => "平成"
|
27
27
|
Time.new(1926, 12, 25).era_name # => "昭和"
|
28
28
|
Time.new(1912, 7, 30).era_name # => "大正"
|
29
|
-
Time.new(
|
29
|
+
Time.new(1873, 1, 1).era_name # => "明治"
|
30
30
|
```
|
31
31
|
|
32
32
|
You can convert to a Japanese year with the `era_year` method:
|
@@ -38,6 +38,22 @@ Time.new(1926, 12, 24).era_year # => 15
|
|
38
38
|
Time.new(1912, 7, 29).era_year # => 45
|
39
39
|
```
|
40
40
|
|
41
|
+
To get a string representation of the Japanese era, use the `strftime` method:
|
42
|
+
|
43
|
+
```
|
44
|
+
time = Time.new(1989, 1, 1)
|
45
|
+
time.strftime("%K") # => "平成"
|
46
|
+
time.strftime("%O") # => "Heisei"
|
47
|
+
time.strftime("%^O") # => "HEISEI"
|
48
|
+
time.strftime("%o") # => "H"
|
49
|
+
time.strftime("%J") # => "01"
|
50
|
+
time.strftime("%-J") # => "1"
|
51
|
+
time.strftime("%_J") # => " 1"
|
52
|
+
time.strftime("%K%-J年%-m月%-d日") # => "平成1年1月1日"
|
53
|
+
time.strftime("%o%J.%m.%d") # => H01.01.01
|
54
|
+
time.strftime("%b %-d %O %-J") # => Jan 1 Heisei 1
|
55
|
+
```
|
56
|
+
|
41
57
|
## Development
|
42
58
|
|
43
59
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/Rakefile
CHANGED
data/bin/console
CHANGED
data/exe/japanese_calendar
CHANGED
data/japanese_calendar.gemspec
CHANGED
data/lib/japanese_calendar.rb
CHANGED
@@ -1,27 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
module JapaneseCalendar
|
2
3
|
module Era
|
3
|
-
|
4
|
-
{ name: "平成", beginning_of_period: Time.new(1989, 1, 8) },
|
5
|
-
{ name: "昭和", beginning_of_period: Time.new(1926, 12, 25) },
|
6
|
-
{ name: "大正", beginning_of_period: Time.new(1912, 7, 30) },
|
7
|
-
{ name: "明治", beginning_of_period: Time.new(1868, 1, 25) }
|
8
|
-
]
|
9
|
-
|
10
|
-
# Returns the Japanese era name (nengo) since the end of the Edo period.
|
4
|
+
# Returns the Japanese era name (nengo) since 1 January 1873 (Meiji 6).
|
11
5
|
#
|
12
6
|
# Time.new(1989, 1, 8).era_name # => "平成"
|
13
7
|
# Time.new(1926, 12, 25).era_name # => "昭和"
|
14
8
|
# Time.new(1912, 7, 30).era_name # => "大正"
|
15
|
-
# Time.new(
|
9
|
+
# Time.new(1873, 1, 1).era_name # => "明治"
|
16
10
|
#
|
17
11
|
# Raises an error when the Japanese era name cannot be found.
|
18
12
|
#
|
19
|
-
# Time.new(
|
20
|
-
def era_name
|
21
|
-
|
13
|
+
# Time.new(1872, 12, 31).era_name # => RuntimeError
|
14
|
+
def era_name(character = :kanji)
|
15
|
+
raise ArgumentError unless %i(kanji romaji).include?(character)
|
16
|
+
find_era[:name][character]
|
22
17
|
end
|
23
18
|
|
24
|
-
# Returns the Japanese year since
|
19
|
+
# Returns the Japanese year since 1 January 1873 (Meiji 6).
|
25
20
|
#
|
26
21
|
# Time.new(2016, 12, 11).era_year # => 28
|
27
22
|
# Time.new(1989, 1, 7).era_year # => 64
|
@@ -30,13 +25,51 @@ module JapaneseCalendar
|
|
30
25
|
#
|
31
26
|
# Raises an error when the Japanese year cannot be found.
|
32
27
|
#
|
33
|
-
# Time.new(
|
28
|
+
# Time.new(1872, 12, 31).era_year # => RuntimeError
|
34
29
|
def era_year
|
35
30
|
year - find_era[:beginning_of_period].year + 1
|
36
31
|
end
|
37
32
|
|
33
|
+
# Formats time according to the directives in the given format string.
|
34
|
+
#
|
35
|
+
# date_of_birth = Time.new(1978, 7, 19)
|
36
|
+
#
|
37
|
+
# date_of_birth.strftime("%K") # => "昭和"
|
38
|
+
# date_of_birth.strftime("%O") # => "Showa"
|
39
|
+
# date_of_birth.strftime("%^O") # => "SHOWA"
|
40
|
+
# date_of_birth.strftime("%o") # => "S"
|
41
|
+
# date_of_birth.strftime("%J") # => "53"
|
42
|
+
#
|
43
|
+
# date_of_birth.strftime("%K%-J年") # => "昭和53年"
|
44
|
+
#
|
45
|
+
# Raises an error when the Japanese year cannot be found.
|
46
|
+
#
|
47
|
+
# Time.new(1872, 12, 31).strftime("%K%-J年") # => RuntimeError
|
48
|
+
def strftime(format)
|
49
|
+
string = format.dup
|
50
|
+
string.gsub!(/%J/, "%02d" % era_year)
|
51
|
+
string.gsub!(/%-J/, "%d" % era_year)
|
52
|
+
string.gsub!(/%_J/, "%2d" % era_year)
|
53
|
+
string.gsub!(/%K/, era_name.to_s)
|
54
|
+
string.gsub!(/%O/, era_name(:romaji).to_s)
|
55
|
+
string.gsub!(/%\^O/, era_name(:romaji).upcase.to_s)
|
56
|
+
string.gsub!(/%o/, era_name(:romaji)[0].to_s)
|
57
|
+
super(string)
|
58
|
+
end
|
59
|
+
|
38
60
|
private
|
61
|
+
MEIJI_6 = Time.new(1873, 1, 1)
|
62
|
+
|
63
|
+
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) }
|
68
|
+
].freeze
|
69
|
+
|
39
70
|
def find_era
|
71
|
+
raise "#{self.class.name.downcase} out of range" if self < MEIJI_6
|
72
|
+
|
40
73
|
ifnone = proc { raise "#{self.class.name.downcase} out of range" }
|
41
74
|
PERIODS.find(ifnone) { |period| period[:beginning_of_period] <= self }
|
42
75
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: japanese_calendar
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryo Yamamoto
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-12-
|
11
|
+
date: 2016-12-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|