Class: DateTime

Inherits:
Object
  • Object
show all
Defined in:
lib/veritas/sql/generator/core_ext/date_time.rb

Overview

Extend DateTime with methods available in ruby 1.9

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#iso8601(time_scale = 0) ⇒ #to_s

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

TODO:

Remove once backports adds this method

Return the DateTime in ISO8601 date-time format

Parameters:

  • time_scale (Integer) (defaults to: 0)

    the number of significant digits to use for fractional seconds

Returns:

  • (#to_s)


18
19
20
# File 'lib/veritas/sql/generator/core_ext/date_time.rb', line 18

def iso8601(time_scale = 0)
  super() + iso8601_timediv(time_scale)
end

#iso8601_timediv(time_scale) ⇒ #to_s

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return the time with fraction seconds

Parameters:

  • time_scale (Integer)

    the number of significant digits to use for fractional seconds

Returns:

  • (#to_s)


32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/veritas/sql/generator/core_ext/date_time.rb', line 32

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