Skip to content

Commit

Permalink
fix 12am/12pm
Browse files Browse the repository at this point in the history
  • Loading branch information
mojombo committed Jun 16, 2007
1 parent f4fef51 commit c7d9591
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
4 changes: 2 additions & 2 deletions lib/chronic/repeaters/repeater_day_portion.rb
Expand Up @@ -10,8 +10,8 @@ def initialize(type)
if type.kind_of? Integer
@range = (@type * 60 * 60)..((@type + 12) * 60 * 60)
else
lookup = {:am => 1..(12 * 60 * 60),
:pm => (12 * 60 * 60)..(24 * 60 * 60),
lookup = {:am => 0..(12 * 60 * 60 - 1),
:pm => (12 * 60 * 60)..(24 * 60 * 60 - 1),
:morning => @@morning,
:afternoon => @@afternoon,
:evening => @@evening,
Expand Down
17 changes: 10 additions & 7 deletions lib/chronic/repeaters/repeater_time.rb
Expand Up @@ -28,17 +28,20 @@ def initialize(time, options = {})
t = time.gsub(/\:/, '')
@type =
if (1..2) === t.size
Tick.new(t.to_i * 60 * 60, true)
hours = t.to_i
hours == 12 ? Tick.new(0 * 60 * 60, true) : Tick.new(hours * 60 * 60, true)
elsif t.size == 3
Tick.new((t[0..0].to_i * 60 * 60) + (t[1..2].to_i * 60), true)
elsif t.size == 4
ambiguous = time =~ /:/ && t[0..0].to_i != 0 && t[0..1].to_i <= 12
Tick.new(t[0..1].to_i * 60 * 60 + t[2..3].to_i * 60, ambiguous)
hours = t[0..1].to_i
hours == 12 ? Tick.new(0 * 60 * 60 + t[2..3].to_i * 60, ambiguous) : Tick.new(hours * 60 * 60 + t[2..3].to_i * 60, ambiguous)
elsif t.size == 5
Tick.new(t[0..0].to_i * 60 * 60 + t[1..2].to_i * 60 + t[3..4].to_i, true)
elsif t.size == 6
ambiguous = time =~ /:/ && t[0..0].to_i != 0 && t[0..1].to_i <= 12
Tick.new(t[0..1].to_i * 60 * 60 + t[2..3].to_i * 60 + t[4..5].to_i, ambiguous)
hours = t[0..1].to_i
hours == 12 ? Tick.new(0 * 60 * 60 + t[2..3].to_i * 60 + t[4..5].to_i, ambiguous) : Tick.new(hours * 60 * 60 + t[2..3].to_i * 60 + t[4..5].to_i, ambiguous)
else
raise("Time cannot exceed six digits")
end
Expand All @@ -65,21 +68,21 @@ def next(pointer)
if pointer == :future
if @type.ambiguous?
[midnight + @type, midnight + half_day + @type, tomorrow_midnight + @type].each do |t|
(@current_time = t; throw :done) if t > @now
(@current_time = t; throw :done) if t >= @now
end
else
[midnight + @type, tomorrow_midnight + @type].each do |t|
(@current_time = t; throw :done) if t > @now
(@current_time = t; throw :done) if t >= @now
end
end
else # pointer == :past
if @type.ambiguous?
[midnight + half_day + @type, midnight + @type, yesterday_midnight + @type * 2].each do |t|
(@current_time = t; throw :done) if t < @now
(@current_time = t; throw :done) if t <= @now
end
else
[midnight + @type, yesterday_midnight + @type].each do |t|
(@current_time = t; throw :done) if t < @now
(@current_time = t; throw :done) if t <= @now
end
end
end
Expand Down
13 changes: 13 additions & 0 deletions test/test_parsing.rb
@@ -1,4 +1,5 @@
require 'chronic'
require 'time'
require 'test/unit'

class TestParsing < Test::Unit::TestCase
Expand Down Expand Up @@ -208,6 +209,18 @@ def test_parse_guess_rr
time = parse_now("4 am", :ambiguous_time_range => :none)
assert_equal Time.local(2006, 8, 16, 4), time

time = parse_now("12 pm")
assert_equal Time.local(2006, 8, 16, 12), time

time = parse_now("12:01 pm")
assert_equal Time.local(2006, 8, 16, 12, 1), time

time = parse_now("12:01 am")
assert_equal Time.local(2006, 8, 16, 0, 1), time

time = parse_now("12 am")
assert_equal Time.local(2006, 8, 16), time

time = parse_now("4:00 in the morning")
assert_equal Time.local(2006, 8, 16, 4), time

Expand Down

0 comments on commit c7d9591

Please sign in to comment.