aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib
diff options
context:
space:
mode:
authorUlysse Carion <ulyssecarion@gmail.com>2014-05-07 09:12:25 -0700
committerAndrew White <andyw@pixeltrix.co.uk>2014-05-11 15:04:49 +0100
commit0e909cd3078bab352eb741426024334afe0d6820 (patch)
tree54e55a746a6f01d6b4b33bd9f9177d37a0aaf95c /activesupport/lib
parent65b9abf56114b629a3cd3f59f395d20b692bae89 (diff)
downloadrails-0e909cd3078bab352eb741426024334afe0d6820.tar.gz
rails-0e909cd3078bab352eb741426024334afe0d6820.tar.bz2
rails-0e909cd3078bab352eb741426024334afe0d6820.zip
Make TimeZone#parse behave more like Time#parse.
Namely, if the mday is omitted but any other upper components are, then instead of supplying the mday from the current time, it defaults to 1.
Diffstat (limited to 'activesupport/lib')
-rw-r--r--activesupport/lib/active_support/values/time_zone.rb13
1 files changed, 12 insertions, 1 deletions
diff --git a/activesupport/lib/active_support/values/time_zone.rb b/activesupport/lib/active_support/values/time_zone.rb
index 38f0d268f4..4c4c055252 100644
--- a/activesupport/lib/active_support/values/time_zone.rb
+++ b/activesupport/lib/active_support/values/time_zone.rb
@@ -282,14 +282,25 @@ module ActiveSupport
#
# Time.zone.now # => Fri, 31 Dec 1999 14:00:00 HST -10:00
# Time.zone.parse('22:30:00') # => Fri, 31 Dec 1999 22:30:00 HST -10:00
+ #
+ # However, if the date component is not provided, but any other upper
+ # components are supplied, then the day of the month defaults to 1:
+ #
+ # Time.zone.parse('Mar 2000') # => Wed, 01 Mar 2000 00:00:00 HST -10:00
def parse(str, now=now())
parts = Date._parse(str, false)
return if parts.empty?
+ default_mday = if parts[:year] || parts[:mon]
+ 1
+ else
+ now.day
+ end
+
time = Time.new(
parts.fetch(:year, now.year),
parts.fetch(:mon, now.month),
- parts.fetch(:mday, now.day),
+ parts.fetch(:mday, default_mday),
parts.fetch(:hour, 0),
parts.fetch(:min, 0),
parts.fetch(:sec, 0) + parts.fetch(:sec_fraction, 0),