Skip to content

Commit

Permalink
Keep sub-second resolution when wrapping a DateTime value
Browse files Browse the repository at this point in the history
Add `DateTime#usec` and `DateTime#nsec` so that `ActiveSupport::TimeWithZone`
keeps sub-second resolution when wrapping a `DateTime` value.

Fixes #10855
  • Loading branch information
pixeltrix committed Jun 13, 2013
1 parent b3bc3aa commit 17f5d8e
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 5 deletions.
6 changes: 6 additions & 0 deletions activesupport/CHANGELOG.md
@@ -1,3 +1,9 @@
* Add `DateTime#usec` and `DateTime#nsec` so that `ActiveSupport::TimeWithZone` keeps
sub-second resolution when wrapping a `DateTime` value.
Fixes #10855

*Andrew White*

* Fix `ActiveSupport::Dependencies::Loadable#load_dependency` calling * Fix `ActiveSupport::Dependencies::Loadable#load_dependency` calling
`#blame_file!` on Exceptions that do not have the Blamable mixin `#blame_file!` on Exceptions that do not have the Blamable mixin


Expand Down
10 changes: 10 additions & 0 deletions activesupport/lib/active_support/core_ext/date_time/conversions.rb
Expand Up @@ -80,6 +80,16 @@ def to_i
seconds_since_unix_epoch.to_i seconds_since_unix_epoch.to_i
end end


# Returns the fraction of a second as microseconds
def usec
(sec_fraction * 1_000_000).to_i
end

# Returns the fraction of a second as nanoseconds
def nsec
(sec_fraction * 1_000_000_000).to_i
end

private private


def offset_in_seconds def offset_in_seconds
Expand Down
6 changes: 1 addition & 5 deletions activesupport/lib/active_support/time_with_zone.rb
Expand Up @@ -292,18 +292,14 @@ def advance(options)
end end
end end


%w(year mon month day mday wday yday hour min sec to_date).each do |method_name| %w(year mon month day mday wday yday hour min sec usec nsec to_date).each do |method_name|
class_eval <<-EOV, __FILE__, __LINE__ + 1 class_eval <<-EOV, __FILE__, __LINE__ + 1
def #{method_name} # def month def #{method_name} # def month
time.#{method_name} # time.month time.#{method_name} # time.month
end # end end # end
EOV EOV
end end


def usec
time.respond_to?(:usec) ? time.usec : 0
end

def to_a def to_a
[time.sec, time.min, time.hour, time.day, time.mon, time.year, time.wday, time.yday, dst?, zone] [time.sec, time.min, time.hour, time.day, time.mon, time.year, time.wday, time.yday, dst?, zone]
end end
Expand Down
10 changes: 10 additions & 0 deletions activesupport/test/core_ext/date_time_ext_test.rb
Expand Up @@ -327,6 +327,16 @@ def test_to_i
assert_equal 946684800, DateTime.civil(1999,12,31,19,0,0,Rational(-5,24)).to_i assert_equal 946684800, DateTime.civil(1999,12,31,19,0,0,Rational(-5,24)).to_i
end end


def test_usec
assert_equal 0, DateTime.civil(2000).usec
assert_equal 500000, DateTime.civil(2000, 1, 1, 0, 0, Rational(1,2)).usec
end

def test_nsec
assert_equal 0, DateTime.civil(2000).nsec
assert_equal 500000000, DateTime.civil(2000, 1, 1, 0, 0, Rational(1,2)).nsec
end

protected protected
def with_env_tz(new_tz = 'US/Eastern') def with_env_tz(new_tz = 'US/Eastern')
old_tz, ENV['TZ'] = ENV['TZ'], new_tz old_tz, ENV['TZ'] = ENV['TZ'], new_tz
Expand Down
5 changes: 5 additions & 0 deletions activesupport/test/core_ext/time_with_zone_test.rb
Expand Up @@ -445,6 +445,11 @@ def test_usec_returns_0_when_datetime_is_wrapped
assert_equal 0, twz.usec assert_equal 0, twz.usec
end end


def test_usec_returns_sec_fraction_when_datetime_is_wrapped
twz = ActiveSupport::TimeWithZone.new(DateTime.civil(2000, 1, 1, 0, 0, Rational(1,2)), @time_zone)
assert_equal 500000, twz.usec
end

def test_utc_to_local_conversion_saves_period_in_instance_variable def test_utc_to_local_conversion_saves_period_in_instance_variable
assert_nil @twz.instance_variable_get('@period') assert_nil @twz.instance_variable_get('@period')
@twz.time @twz.time
Expand Down

0 comments on commit 17f5d8e

Please sign in to comment.