Sha256: 96b788db02ede630a84b0eea81c0ea63663913f2d19e9eaf34131eaa42101803

Contents?: true

Size: 1.11 KB

Versions: 3

Compression:

Stored size: 1.11 KB

Contents

# encoding: utf-8

# Extend DateTime with methods available in ruby 1.9
class DateTime

  SEC_FRACTION_MULTIPLIER = RUBY_VERSION < '1.9' ? 60 * 60 * 24 : 1

  # Return the DateTime in ISO8601 date-time format
  #
  # @param [Integer] time_scale
  #   the number of significant digits to use for fractional seconds
  #
  # @return [#to_s]
  #
  # @todo Remove once backports adds this method
  #
  # @api private
  def iso8601(time_scale = 0)
    super() + iso8601_timediv(time_scale)
  end unless method_defined?(:iso8601) && instance_method(:iso8601).arity == 1

private

  # Return the time with fraction seconds
  #
  # @param [Integer] time_scale
  #   the number of significant digits to use for fractional seconds
  #
  # @return [#to_s]
  #
  # @api private
  def iso8601_timediv(time_scale)
    date_time = frozen? ? dup : self

    fractional_seconds = unless time_scale.zero?
      '.%0*d' % [
        time_scale,
        date_time.sec_fraction * SEC_FRACTION_MULTIPLIER * 10**time_scale
      ]
    end

    date_time.strftime("T%T#{fractional_seconds}%Z")
  end unless method_defined? :iso8601_timediv

end # class Date

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
veritas-sql-generator-0.0.6 lib/veritas/sql/generator/core_ext/date_time.rb
veritas-sql-generator-0.0.5 lib/veritas/sql/generator/core_ext/date_time.rb
veritas-sql-generator-0.0.4 lib/veritas/sql/generator/core_ext/date_time.rb