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 =

TODO: remove the rbx guard when fractional seconds are handled properly in 1.9 mode

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)


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

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)


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

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