From db1fae02053ec1a891e37498e1a5c0fd28043823 Mon Sep 17 00:00:00 2001 From: Giuseppe Bilotta Date: Wed, 26 Aug 2009 20:52:31 +0200 Subject: Utils: time parsing routines Add time parsing routines to Utils, to be used for human-to-computer conversion of time offsets. Refactored and enhanced from the remind plugin. --- data/rbot/plugins/remind.rb | 96 +-------------------------------------------- 1 file changed, 1 insertion(+), 95 deletions(-) (limited to 'data/rbot') diff --git a/data/rbot/plugins/remind.rb b/data/rbot/plugins/remind.rb index 804e3f0b..1c1a21a9 100644 --- a/data/rbot/plugins/remind.rb +++ b/data/rbot/plugins/remind.rb @@ -5,101 +5,7 @@ class RemindPlugin < Plugin # # Throws:: RunTimeError "invalid time string" on parse failure def timestr_offset(timestr) - case timestr - when (/^(\S+)\s+(\S+)$/) - mult = $1 - unit = $2 - if(mult =~ /^([\d.]+)$/) - num = $1.to_f - raise "invalid time string" unless num - else - case mult - when(/^(one|an|a)$/) - num = 1 - when(/^two$/) - num = 2 - when(/^three$/) - num = 3 - when(/^four$/) - num = 4 - when(/^five$/) - num = 5 - when(/^six$/) - num = 6 - when(/^seven$/) - num = 7 - when(/^eight$/) - num = 8 - when(/^nine$/) - num = 9 - when(/^ten$/) - num = 10 - when(/^fifteen$/) - num = 15 - when(/^twenty$/) - num = 20 - when(/^thirty$/) - num = 30 - when(/^sixty$/) - num = 60 - else - raise "invalid time string" - end - end - case unit - when (/^(s|sec(ond)?s?)$/) - return num - when (/^(m|min(ute)?s?)$/) - return num * 60 - when (/^(h|h(ou)?rs?)$/) - return num * 60 * 60 - when (/^(d|days?)$/) - return num * 60 * 60 * 24 - else - raise "invalid time string" - end - when (/^(\d+):(\d+)(?:\:(\d+))?$/) - hour = $1.to_i - min = $2.to_i - sec = $3.to_i - now = Time.now - later = Time.mktime(now.year, now.month, now.day, hour, min, sec) - - # if the given hour is earlier than current hour, given timestr - # must have been meant to be in the future - if hour < now.hour || hour <= now.hour && min < now.min - later += 60*60*24 - end - - return later - now - when (/^(\d+):(\d+)(am|pm)$/) - hour = $1.to_i - min = $2.to_i - ampm = $3 - if ampm == "pm" - hour += 12 - end - now = Time.now - later = Time.mktime(now.year, now.month, now.day, hour, min, now.sec) - return later - now - when (/^(\S+)$/) - num = 1 - unit = $1 - case unit - when (/^(s|sec(ond)?s?)$/) - return num - when (/^(m|min(ute)?s?)$/) - return num * 60 - when (/^(h|h(ou)?rs?)$/) - return num * 60 * 60 - when (/^(d|days?)$/) - return num * 60 * 60 * 24 - else - raise "invalid time string" - end - else - raise "invalid time string" - end + Utils.parse_time_offset(timestr) end def initialize -- cgit v1.2.3