Module: JapaneseCalendar::Era

Includes:
Deprecator
Included in:
Date, DateTime, Time
Defined in:
lib/japanese_calendar/era.rb,
lib/japanese_calendar/era/deprecator.rb

Defined Under Namespace

Modules: Deprecator

Instance Method Summary collapse

Instance Method Details

#era_name(character = :kanji) ⇒ Object

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

reiwa = Time.new(2019, 5, 1)   # => 2019-05-01 00:00:00 +0900
reiwa.era_name                 # => "令和"
reiwa.era_name(:romaji)        # => "Reiwa"

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


68
69
70
71
72
73
74
# File 'lib/japanese_calendar/era.rb', line 68

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(2019,  5,  1).era_year # => 1
Time.new(2019,  4, 30).era_year # => 31
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


87
88
89
# File 'lib/japanese_calendar/era.rb', line 87

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("%JN")  # => "昭和"
date_of_birth.strftime("%JR")  # => "Showa"
date_of_birth.strftime("%^JR") # => "SHOWA"
date_of_birth.strftime("%Jr")  # => "S"
date_of_birth.strftime("%Jy")  # => "53"

date_of_birth.strftime("%JN%-Jy年")  # => "昭和53年"

Raises an error when the Japanese year cannot be found.

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


38
39
40
41
# File 'lib/japanese_calendar/era.rb', line 38

def strftime(format)
  string = format.gsub(era_pattern, era_conversion)
  super(string)
end