Skip to content

Commit

Permalink
parse time
Browse files Browse the repository at this point in the history
  • Loading branch information
bry4n committed May 4, 2012
1 parent 3de018c commit dd89e9a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
4 changes: 4 additions & 0 deletions examples/time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@
p Minute.parse("RubyConf sold 1000 tickets on June 29 2001")
p Minute.parse("Come to my house on 1 Jan 2001")
p Minute.parse("Homework due on 2011-11-3")
p Minute.parse("next Tuesday")
p Minute.parse("homework due by the end of day")
p Minute.parse("give me by the end of month")
p Minute.parse("8:30 AM")
9 changes: 7 additions & 2 deletions lib/minute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ class Minute

VERSION = "0.1"

MONTHS = Date::MONTHNAMES.zip(Date::ABBR_MONTHNAMES).flatten.compact.map(&:downcase).freeze

MONTHS = Date::MONTHNAMES.zip(Date::ABBR_MONTHNAMES).flatten.compact.map(&:downcase).freeze
DAYS = Date::DAYNAMES.zip(Date::ABBR_DAYNAMES).flatten.compact.map(&:downcase).freeze

DATE_WITH_ORDINAL = /(\d{1,2})(st|nd|rd|th)?\s+(#{MONTHS.join("|")})(\s+\d{4})?/
ENGLISH_DATE = /(#{MONTHS.join("|")}),?\s(\d{1,2})(\s+\d{4})?/
DATE = /(\d{1,4})-?(\d{1,4})-?(\d{1,4})?/
TIME = /(\d{1,2}:\d{1,2})(:\d{1,2})?\s+?(am|pm)/i

# Public: Create a match pattern to capture date/time strings.
#
Expand Down Expand Up @@ -79,6 +81,8 @@ def patterns
match 'tomorrow', lambda {|time| time + 1.day }
match 'yesterday', lambda {|time| time - 1.day }
match 'today', lambda {|time| time }

match /(beginning|end)\s+of\s+(year|month|week|day)/, lambda {|time,m| time.send(:"#{m[1]}_of_#{m[2]}") }

match /(last|next|\d+)\s+(year|month|week|day|hour|minute|second)s?(\s+ago)?/ do |time, m|
case m[1]
Expand All @@ -96,6 +100,7 @@ def patterns
match DATE, lambda {|time, m| Time.parse(m[1..-1].compact.join("-")) }
match ENGLISH_DATE, lambda {|time, m| Time.parse(m[1..-1].compact.join(" ")) }
match DATE_WITH_ORDINAL, lambda {|time, m| Time.parse(m[1..-1].compact.join(" ")) }
match TIME, lambda {|time, m| Time.parse(m[1..-1].compact.join) }

# TODO
# this december
Expand Down
2 changes: 1 addition & 1 deletion minute.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ Gem::Specification.new do |gem|
gem.email = "bryann83@gmail.com"
gem.homepage = "https://github.com/bry4n/minute"
gem.files = Dir['README.md', 'LICENSE', 'lib/**/*.rb']
gem.add_dependency "small", "0.3"
gem.add_dependency "small", "0.4"
end
7 changes: 7 additions & 0 deletions spec/format_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,11 @@
assert_equal time.month, 12
assert_equal time.year, 2001
end

it "parses 8:30 AM" do
time = Minute.parse("8:30 AM")
assert_equal 8, time.hour
assert_equal 30, time.min
end

end

0 comments on commit dd89e9a

Please sign in to comment.