Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…te_time@352 20afb1e0-9c0e-0410-9884-91ed27886737
  • Loading branch information
jonathan committed Dec 11, 2007
1 parent 96e7a22 commit 511f86b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
[28 September]
[11 December 2007]

* Parse microseconds from time and datetime strings.

[28 September 2007]

* Preserve ActiveRecord::Validations::DateTime.us_date_format for compatibility.

Expand Down
10 changes: 5 additions & 5 deletions lib/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def string_to_dummy_time(value)
return value if value.is_a?(Time) || value.is_a?(DateTime)
return value.to_time(ActiveRecord::Base.default_timezone) if value.is_a?(Date)

hour, minute, second = case value.strip
hour, minute, second, microsecond = case value.strip
# 12 hours with minute and second
when /\A(\d{1,2})[\. :](\d{2})[\. :](\d{2})\s?(am|pm)\Z/i
[full_hour($1, $4), $2, $3]
Expand All @@ -44,14 +44,14 @@ def string_to_dummy_time(value)
when /\A(\d{1,2})\s?(am|pm)\Z/i
[full_hour($1, $2)]
# 24 hour: 22:30, 03.10, 12 30
when /\A(\d{2})[\. :](\d{2})([\. :](\d{2}))?\Z/
[$1, $2, $4]
when /\A(\d{2})[\. :](\d{2})([\. :](\d{2})(\.(\d{6}))?)?\Z/
[$1, $2, $4, $6]
# Not a valid time string
else
return nil
end

Time.send(ActiveRecord::Base.default_timezone, 2000, 1, 1, hour.to_i, minute.to_i, second.to_i) rescue nil
Time.send(ActiveRecord::Base.default_timezone, 2000, 1, 1, hour.to_i, minute.to_i, second.to_i, microsecond.to_i) rescue nil
end

def string_to_time(value)
Expand All @@ -77,7 +77,7 @@ def string_to_time(value)
time = string_to_dummy_time(value.last(value.size - split_index))
return if time.nil?

time_array = [date.year, date.month, date.day, time.hour, time.min, time.sec]
time_array = [date.year, date.month, date.day, time.hour, time.min, time.sec, time.usec]

# From schema_definitions.rb
begin
Expand Down
5 changes: 5 additions & 0 deletions test/date_time_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ def test_various_formats
end
end

def test_date_time_with_microseconds
assert_update_and_match /Mar 20 09:22:50 [\+-]?[\w ]+ 2007/, :date_and_time_of_birth => "20 Mar 07 09:22:50.987654"
assert_equal 987654, p.date_and_time_of_birth.usec
end

def test_invalid_formats
['29 Feb 06 1am', '1 Jan 06', '7pm'].each do |value|
assert_invalid_and_errors_match /date time/, :date_and_time_of_birth => value
Expand Down
5 changes: 5 additions & 0 deletions test/time_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ def test_24_hour
end
end

def test_24_hour_with_microseconds
assert_update_and_match /12:23:56/, :time_of_birth => "12:23:56.169732"
assert_equal 169732, p.time_of_birth.usec
end

def test_time_objects
{ Time.gm(2006, 2, 2, 22, 30) => /22:30:00/, '2pm' => /14:00:00/, Time.gm(2006, 2, 2, 1, 3) => /01:03:00/ }.each do |value, result|
assert_update_and_match result, :time_of_birth => value
Expand Down

0 comments on commit 511f86b

Please sign in to comment.