Module: JapaneseCalendar::Era

Included in:
Time
Defined in:
lib/japanese_calendar/era.rb

Defined Under Namespace

Classes: Period

Instance Method Summary collapse

Instance Method Details

#era_name(character = :kanji) ⇒ Object

Returns the Japanese era name (nengo) since 1 January 1873 (Meiji 6).

heisei = Time.new(1989, 1, 8)  # => 1989-01-08 00:00:00 +0900
heisei.era_name                # => "平成"
heisei.era_name(:romaji)       # => "Heisei"

showa = Time.new(1926, 12, 25) # => 1926-12-25 00:00:00 +0900
showa.era_name                 # => "昭和"
showa.era_name(:romaji)        # => "Showa"

taisho = Time.new(1912, 7, 30) # => 1912-07-30 00:00:00 +0900
taisho.era_name                # => "大正"
taisho.era_name(:romaji)       # => "Taisho"

meiji = Time.new(1873, 1, 1)   # => 1873-01-01 00:00:00 +0900
meiji.era_name                 # => "明治"
meiji.era_name(:romaji)        # => "Meiji"

Raises an error when the Japanese era name cannot be found.

Time.new(1872, 12, 31).era_name # => RuntimeError


25
26
27
28
29
30
31
# File 'lib/japanese_calendar/era.rb', line 25

def era_name(character = :kanji)
  unless %i(kanji romaji).include?(character)
    raise ArgumentError, "invalid character"
  end

  current_era.send("#{character}_name")
end

#era_yearObject

Returns the Japanese year since 1 January 1873 (Meiji 6).

Time.new(2016, 12, 11).era_year # => 28
Time.new(1989,  1,  7).era_year # => 64
Time.new(1926, 12, 24).era_year # => 15
Time.new(1912,  7, 29).era_year # => 45

Raises an error when the Japanese year cannot be found.

Time.new(1872, 12, 31).era_year # => RuntimeError


43
44
45
# File 'lib/japanese_calendar/era.rb', line 43

def era_year
  year - current_era.beginning_of_period.year + 1
end

#strftime(format) ⇒ Object

Formats time according to the directives in the given format string.

date_of_birth = Time.new(1978, 7, 19)

date_of_birth.strftime("%K")  # => "昭和"
date_of_birth.strftime("%O")  # => "Showa"
date_of_birth.strftime("%^O") # => "SHOWA"
date_of_birth.strftime("%o")  # => "S"
date_of_birth.strftime("%J")  # => "53"

date_of_birth.strftime("%K%-J年")  # => "昭和53年"

Raises an error when the Japanese year cannot be found.

Time.new(1872, 12, 31).strftime("%K%-J年") # => RuntimeError


62
63
64
65
66
67
68
69
70
71
72
# File 'lib/japanese_calendar/era.rb', line 62

def strftime(format)
  string = format.dup
  string.gsub!(/%J/,   "%02d" % era_year)
  string.gsub!(/%-J/,  "%d"   % era_year)
  string.gsub!(/%_J/,  "%2d"  % era_year)
  string.gsub!(/%K/,   era_name.to_s)
  string.gsub!(/%O/,   era_name(:romaji).to_s)
  string.gsub!(/%\^O/, era_name(:romaji).upcase.to_s)
  string.gsub!(/%o/,   era_name(:romaji)[0].to_s)
  super(string)
end