module TZInfo::WithOffset

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 {TimezoneOffset#abbreviation abbreviation} of the {TimezoneOffset} associated with a local time.

Public Instance Methods

Overrides the ‘Time`, `DateTime` or {Timestamp} version of `strftime`, replacing `%Z` with the {TimezoneOffset#abbreviation 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.

@param format [String] the format string. @return [String] the formatted time. @raise [ArgumentError] if ‘format` is `nil`.

Calls superclass method
# 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