Class: Stamp::Emitters::TwelveHour

Inherits:
Object
  • Object
show all
Defined in:
lib/stamp/emitters/twelve_hour.rb

Overview

Emits hours as a two-digit number with a leading zero if necessary.

Constant Summary collapse

LEADING_ZERO =
new(leading_zero: true)
NON_LEADING_ZERO =
new(leading_zero: false)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ TwelveHour

Returns a new instance of TwelveHour.

Parameters:

  • field (String|Symbol)

    the field to be formatted (e.g. :month, :year)

  • options (Hash) (defaults to: {})

    the options for output

Options Hash (options):

  • :leading_zero (String) — default: default: false

    pass true add a leading 0 to output



11
12
13
# File 'lib/stamp/emitters/twelve_hour.rb', line 11

def initialize(options = {})
  @leading_zero = options[:leading_zero]
end

Instance Attribute Details

#leading_zeroObject (readonly)

Returns the value of attribute leading_zero.



6
7
8
# File 'lib/stamp/emitters/twelve_hour.rb', line 6

def leading_zero
  @leading_zero
end

Instance Method Details

#fieldObject



26
27
28
# File 'lib/stamp/emitters/twelve_hour.rb', line 26

def field
  :hour
end

#format(target) ⇒ Object

Parameters:

  • target (Date|Time|DateTime)

    the date to be formatted



16
17
18
19
20
21
22
23
24
# File 'lib/stamp/emitters/twelve_hour.rb', line 16

def format(target)
  value = target.hour
  value = ((value - 1) % 12) + 1
  if leading_zero && (value < 10)
    "0#{value}"
  else
    value
  end
end

#strftime_directiveObject



30
31
32
# File 'lib/stamp/emitters/twelve_hour.rb', line 30

def strftime_directive
  leading_zero ? "%I" : "%-I"
end