Module: TZInfo::WithOffset

Included in:
DateTimeWithOffset, TimeWithOffset, TimestampWithOffset
Defined in:
lib/tzinfo/with_offset.rb

Overview

The WithOffset module is included in TimeWithOffset, DateTimeWithOffset and TimestampWithOffset. It provides an override for the #strftime method that handles expanding the %Z directive according to the abbreviation of the TimezoneOffset associated with a local time.

Instance Method Summary collapse

Instance Method Details

#strftime(format) ⇒ String

Overrides the Time, DateTime or Timestamp version of strftime, replacing %Z with the abbreviation of the associated TimezoneOffset. If there is no associated offset, %Z is expanded by the base class instead.

All the format directives handled by the base class are supported.

Parameters:

  • format (String)

    the format string.

Returns:

  • (String)

    the formatted time.

Raises:

  • (ArgumentError)

    if format is nil.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/tzinfo/with_offset.rb', line 21

def strftime(format)
  raise ArgumentError, 'format must be specified' unless format

  if_timezone_offset do |o|
    abbreviation = nil

    format = format.gsub(/%(%*)Z/) do
      if $1.length.odd?
        # Return %%Z so the real strftime treats it as a literal %Z too.
        "#$1%Z"
      else
        "#$1#{abbreviation ||= o.abbreviation.gsub(/%/, '%%')}"
      end
    end
  end

  super
end