aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext/date_time
diff options
context:
space:
mode:
authorAndrew White <andrew.white@unboxed.co>2017-03-01 18:13:15 +0000
committerAndrew White <andrew.white@unboxed.co>2017-03-02 06:02:41 +0000
commitb5af751508281483845d4323177b0aec98a11880 (patch)
tree56f4a6c7af3a0ed4934e89dbfc73e55fadbdf314 /activesupport/lib/active_support/core_ext/date_time
parentb61a56541aecd7ac685d4f19d943177a3f1b465a (diff)
downloadrails-b5af751508281483845d4323177b0aec98a11880.tar.gz
rails-b5af751508281483845d4323177b0aec98a11880.tar.bz2
rails-b5af751508281483845d4323177b0aec98a11880.zip
Update `DateTime#change` to support usec and nsec
Adding support for these options now allows us to update the `DateTime#end_of` methods to match the equivalent `Time#end_of` methods, e.g: datetime = DateTime.now.end_of_day datetime.nsec == 999999999 # => true Fixes #21424.
Diffstat (limited to 'activesupport/lib/active_support/core_ext/date_time')
-rw-r--r--activesupport/lib/active_support/core_ext/date_time/calculations.rb18
1 files changed, 14 insertions, 4 deletions
diff --git a/activesupport/lib/active_support/core_ext/date_time/calculations.rb b/activesupport/lib/active_support/core_ext/date_time/calculations.rb
index 70d5c9af8e..7a9eb8c266 100644
--- a/activesupport/lib/active_support/core_ext/date_time/calculations.rb
+++ b/activesupport/lib/active_support/core_ext/date_time/calculations.rb
@@ -47,13 +47,23 @@ class DateTime
# DateTime.new(2012, 8, 29, 22, 35, 0).change(year: 1981, day: 1) # => DateTime.new(1981, 8, 1, 22, 35, 0)
# DateTime.new(2012, 8, 29, 22, 35, 0).change(year: 1981, hour: 0) # => DateTime.new(1981, 8, 29, 0, 0, 0)
def change(options)
+ if new_nsec = options[:nsec]
+ raise ArgumentError, "Can't change both :nsec and :usec at the same time: #{options.inspect}" if options[:usec]
+ new_fraction = Rational(new_nsec, 1000000000)
+ else
+ new_usec = options.fetch(:usec, (options[:hour] || options[:min] || options[:sec]) ? 0 : Rational(nsec, 1000))
+ new_fraction = Rational(new_usec, 1000000)
+ end
+
+ raise ArgumentError, "argument out of range" if new_fraction >= 1
+
::DateTime.civil(
options.fetch(:year, year),
options.fetch(:month, month),
options.fetch(:day, day),
options.fetch(:hour, hour),
options.fetch(:min, options[:hour] ? 0 : min),
- options.fetch(:sec, (options[:hour] || options[:min]) ? 0 : sec + sec_fraction),
+ options.fetch(:sec, (options[:hour] || options[:min]) ? 0 : sec) + new_fraction,
options.fetch(:offset, offset),
options.fetch(:start, start)
)
@@ -122,7 +132,7 @@ class DateTime
# Returns a new DateTime representing the end of the day (23:59:59).
def end_of_day
- change(hour: 23, min: 59, sec: 59)
+ change(hour: 23, min: 59, sec: 59, usec: Rational(999999999, 1000))
end
alias :at_end_of_day :end_of_day
@@ -134,7 +144,7 @@ class DateTime
# Returns a new DateTime representing the end of the hour (hh:59:59).
def end_of_hour
- change(min: 59, sec: 59)
+ change(min: 59, sec: 59, usec: Rational(999999999, 1000))
end
alias :at_end_of_hour :end_of_hour
@@ -146,7 +156,7 @@ class DateTime
# Returns a new DateTime representing the end of the minute (hh:mm:59).
def end_of_minute
- change(sec: 59)
+ change(sec: 59, usec: Rational(999999999, 1000))
end
alias :at_end_of_minute :end_of_minute