Skip to content

Commit

Permalink
fixed: when a empty string or an invalid date is passed, the current …
Browse files Browse the repository at this point in the history
…date is returned
  • Loading branch information
fmluizao committed Jun 19, 2009
1 parent 35648a3 commit f231775
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/delocalize/localized_date_time_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ def parse(datetime, type)
def default_parse(datetime, type)
begin
today = Date.current
# parse set default year, month and day if not found
parsed = Date._parse(datetime).reverse_merge(:year => today.year, :mon => today.mon, :mday => today.mday)
parsed = Date._parse(datetime)
return if parsed.empty? #the datetime value is invalid
# set default year, month and day if not found
parsed.reverse_merge!(:year => today.year, :mon => today.mon, :mday => today.mday)
datetime = Time.zone.local(*parsed.values_at(:year, :mon, :mday, :hour, :min, :sec))
Date == type ? datetime.to_date : datetime
rescue
Expand Down Expand Up @@ -62,4 +64,4 @@ def apply_regex(format)
end
end
end
end
end
9 changes: 9 additions & 0 deletions test/delocalize_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ def setup
@product.cant_think_of_a_sensible_time_field = '09:00'
assert_equal time, @product.cant_think_of_a_sensible_time_field
end

test "should return nil if the input is empty or invalid" do
@product.released_on = ""
assert_nil @product.released_on

@product.released_on = "aa"
assert_nil @product.released_on
end

end

class DelocalizeActionViewTest < ActionView::TestCase
Expand Down

0 comments on commit f231775

Please sign in to comment.