Skip to content

Commit

Permalink
Support (mm/yy), :endian_precedence == :month_year
Browse files Browse the repository at this point in the history
  • Loading branch information
davispuh committed Sep 27, 2017
1 parent 345e9f4 commit 8e243c6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
12 changes: 10 additions & 2 deletions lib/chronic/definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def definitions
[[MonthName, [SeparatorSpace, SeparatorDash], ScalarDay, [SeparatorSpace, Unit, :none]], :handle_mn_sd],
[[MonthName, SeparatorSpace, SeparatorApostrophe, ScalarYear], :handle_mn_sy],
[[MonthName, SeparatorSpace, ScalarYear], :handle_mn_sy],
[[ScalarYear, [SeparatorDash, SeparatorSlash], ScalarMonth], :handle_sy_sm],
[[ScalarFullYear, [SeparatorDash, SeparatorSlash], ScalarMonth], :handle_sy_sm],
[[ScalarFullYear, SeparatorSpace, MonthName], :handle_sy_mn],
[[OrdinalDay, SeparatorSpace, MonthName], :handle_od_mn],
[[ScalarDay, [SeparatorSpace, SeparatorDash, :optional], MonthName], :handle_sd_mn],
Expand Down Expand Up @@ -177,7 +177,13 @@ def self.little
[[ScalarDay, SeparatorSlash, ScalarMonth, SeparatorSlash, ScalarYear, [SeparatorDot, Scalar, :none]], :handle_sd_sm_sy],
[[ScalarDay, [SeparatorSlash, SeparatorDash, SeparatorDot], ScalarMonth, [SeparatorSlash, :none]], :handle_sd_sm]
]
end
end

def self.month_year
[
[[ScalarMonth, SeparatorSlash, ScalarYear], :handle_sm_sy]
]
end

def prefered_endian
options[:endian_precedence] ||= [:middle, :little]
Expand All @@ -189,6 +195,8 @@ def prefered_endian
definition_list += self.class.little
when :middle
definition_list += self.class.middle
when :month_year
definition_list += self.class.month_year
else
raise ArgumentError, "Unknown endian option '#{endian}'"
end
Expand Down
9 changes: 8 additions & 1 deletion lib/chronic/handlers/date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,14 @@ def handle_mn_sy
@precision = :month
end


# Handle scalar-month/scalar-year
# formats: mm/yy
def handle_sm_sy
handle_sm
next_tag
handle_sy
@precision = :month
end

# Handle scalar-month/scalar-day
# formats: mm/dd
Expand Down
20 changes: 10 additions & 10 deletions test/test_parsing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -442,16 +442,16 @@ def test_handle_sm_sd
assert_equal Time.local(2006, 1, 1, 12), time
end

# def test_handle_sm_sy
# time = parse_now("05/06")
# assert_equal Time.local(2006, 5, 16, 12), time
#
# time = parse_now("12/06")
# assert_equal Time.local(2006, 12, 16, 12), time
#
# time = parse_now("13/06")
# assert_nil time
# end
def test_handle_sm_sy
time = parse_now("05/06", :endian_precedence => :month_year)
assert_equal Time.local(2006, 5, 16, 12), time

time = parse_now("12/06", :endian_precedence => :month_year)
assert_equal Time.local(2006, 12, 16, 12), time

time = parse_now("13/06", :endian_precedence => :month_year)
assert_nil time
end

def test_handle_sy_sm
time = parse_now("2012-06")
Expand Down

0 comments on commit 8e243c6

Please sign in to comment.