Class: Formatter::Date

Inherits:
Object
  • Object
show all
Defined in:
lib/formatter/date.rb,
lib/formatter/date/version.rb

Overview

Date formatter with time zone support.

Defined Under Namespace

Classes: TimeOrDateTime

Constant Summary collapse

VERSION =

Gem version

'0.1.0'

Instance Method Summary collapse

Constructor Details

#initialize(time_zone, format = :iso8601, fraction = 0) ⇒ undefined

Initialize a formatter with the desired options.

:time_zone specifies which time zone to use when formatting.

:format specifies which format to use, it can either be a symbol of a predefined format or a string with a custom format which will be delegated to DateTime#strftime.

:fraction is length of fractional seconds to be used with predefined formats.

Parameters:

  • time_zone (String|TZInfo::Timezone)
  • format (Symbol|String) (defaults to: :iso8601)
  • fraction (Integer) (defaults to: 0)

    (0)



44
45
46
47
# File 'lib/formatter/date.rb', line 44

def initialize(time_zone, format = :iso8601, fraction = 0)
  self.time_zone = time_zone
  @formatter = formatter_for format, fraction
end

Instance Method Details

#format(time_or_datetime) ⇒ String

Format Time or DateTime with formatter

Parameters:

  • time_or_datetime (Time|DateTime)

Returns:

  • (String)


56
57
58
# File 'lib/formatter/date.rb', line 56

def format(time_or_datetime)
  formatter.call with_offset(time_or_datetime)
end

#identifierString

Configured time zone identifier

Returns:

  • (String)


65
66
67
# File 'lib/formatter/date.rb', line 65

def identifier
  time_zone.identifier
end

#offsetRational

Configured time zone offset

Returns:

  • (Rational)


74
75
76
# File 'lib/formatter/date.rb', line 74

def offset
  time_zone.current_period.utc_total_offset_rational
end