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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3c2955f1d5776dec68bc1620dae1b8315c4c694e
4
- data.tar.gz: fe0a89815a5329db733182628d778f61c352e20d
3
+ metadata.gz: 24d6b788116b03f3cda9eb3777c06fd528990b9e
4
+ data.tar.gz: 7e51d4fa12dd788b786102db9a602ae5e76e8acc
5
5
  SHA512:
6
- metadata.gz: 2f4c1c8e048cbf17cd5f509222523e5e67bcc12cbaba2986cbdda1fba3b6963f4b3ad196765adec045c7e4c387a09025f0714c2ba7e2ca3a5f17674385def892
7
- data.tar.gz: c6b360e001018b91c86e9f27bf906d26ccfb98f1f784431a63d48f73190b1485edf59db8e9840d1afea875605983c40d6f1ab6d1e26195a1948ae2861f3941ef
6
+ metadata.gz: 3b778089d926467aae31b406e8f7ccef7512fd3e836078c398e6d609eed645a329fde41f41c8b5b5b5a512b1349f85f5d4d6d861b1396e683ebafb1b8bead51b
7
+ data.tar.gz: 37381cf4c47ca1cbfe0fbb9a2d54effa435dcd01e71cffa3aa0b88094580795ee8984bcb760028f43decca3454d9443949a3ead6368810009d10cc7fe27f742b
data/.rubocop.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  AllCops:
2
- DisabledByDefault: true
2
+ TargetRubyVersion: 2.3
3
3
 
4
4
  #################### Lint ################################
5
5
 
data/Gemfile CHANGED
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  source 'https://rubygems.org'
2
3
 
3
4
  # Specify your gem's dependencies in japanese_calendar.gemspec
data/README.md CHANGED
@@ -20,13 +20,13 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
- To get the Japanese era name, use the `era_name` method:
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(1868, 1, 25).era_name # => "明治"
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
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require "bundler/gem_tasks"
2
3
  require "rspec/core/rake_task"
3
4
 
data/bin/console CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
4
  require "bundler/setup"
4
5
  require "japanese_calendar"
@@ -1,3 +1,4 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
4
  require "japanese_calendar"
@@ -1,4 +1,5 @@
1
1
  # coding: utf-8
2
+ # frozen_string_literal: true
2
3
  lib = File.expand_path('../lib', __FILE__)
3
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
5
  require 'japanese_calendar/version'
@@ -1,2 +1,3 @@
1
+ # frozen_string_literal: true
1
2
  require "japanese_calendar/version"
2
3
  require "japanese_calendar/core_ext/time"
@@ -1,5 +1,6 @@
1
+ # frozen_string_literal: true
1
2
  require "japanese_calendar/era"
2
3
 
3
4
  class Time
4
- include JapaneseCalendar::Era
5
+ prepend JapaneseCalendar::Era
5
6
  end
@@ -1,27 +1,22 @@
1
+ # frozen_string_literal: true
1
2
  module JapaneseCalendar
2
3
  module Era
3
- PERIODS = [
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(1868, 1, 25).era_name # => "明治"
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(1868, 1, 24).era_name # => RuntimeError
20
- def era_name
21
- find_era[:name]
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 the end of the Edo period.
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(1868, 1, 24).era_year # => RuntimeError
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
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module JapaneseCalendar
2
- VERSION = "0.1.1"
3
+ VERSION = "0.2.0"
3
4
  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.1.1
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-18 00:00:00.000000000 Z
11
+ date: 2016-12-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler