Permalink
Browse files

parse time

  • Loading branch information...
1 parent 3de018c commit dd89e9ae0903042f98cef2a116462ea297dbe5a5 @bry4n committed May 4, 2012
Showing with 19 additions and 3 deletions.
  1. +4 −0 examples/time.rb
  2. +7 −2 lib/minute.rb
  3. +1 −1 minute.gemspec
  4. +7 −0 spec/format_spec.rb
View
@@ -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")
View
@@ -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.
#
@@ -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]
@@ -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
View
@@ -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
View
@@ -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.