Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fix 12am/12pm

  • Loading branch information...
commit c7d9591acf5179345cbc916bd509c48acee8e744 1 parent f4fef51
@mojombo mojombo authored
View
4 lib/chronic/repeaters/repeater_day_portion.rb
@@ -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,
View
17 lib/chronic/repeaters/repeater_time.rb
@@ -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
@@ -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
View
13 test/test_parsing.rb
@@ -1,4 +1,5 @@
require 'chronic'
+require 'time'
require 'test/unit'
class TestParsing < Test::Unit::TestCase
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.