diff options
author | Raine Virta <rane@kapsi.fi> | 2010-09-07 21:00:45 +0300 |
---|---|---|
committer | Giuseppe Bilotta <giuseppe.bilotta@gmail.com> | 2010-09-10 19:07:13 +0200 |
commit | a194d08bf86c52de87187b9477045f024f1659dd (patch) | |
tree | ea3c3c805bca575e9bcc8b0239c8b23d665c5b70 /data | |
parent | 036dbad139010cf965dbf66310e425007eb3f8e3 (diff) |
time: add timestamp parser as a fallback to the default functionality
Diffstat (limited to 'data')
-rw-r--r-- | data/rbot/plugins/time.rb | 49 |
1 files changed, 48 insertions, 1 deletions
diff --git a/data/rbot/plugins/time.rb b/data/rbot/plugins/time.rb index 190abd6a..5a1a579c 100644 --- a/data/rbot/plugins/time.rb +++ b/data/rbot/plugins/time.rb @@ -66,7 +66,7 @@ class TimePlugin < Plugin zone = @registry[ zone ] m.reply getTime( m, zone ) else - m.reply "#{zone} is an unknown time." + parse(m, params) end end else @@ -122,6 +122,53 @@ class TimePlugin < Plugin @registry.delete(user) m.reply "Ok, I've forgotten #{user}'s timezone" end + + def parse(m, params) + require 'time' + str = params[:where].to_s + now = Time.now + + begin + time = begin + Time.parse str + rescue ArgumentError => e + # Handle 28/9/1978, which is a valid date representation at least in Italy + if e.message == 'argument out of range' + str.tr!('/', '-') + Time.parse str + else + raise + end + end + + offset = (time - now).abs + raise if offset < 0.1 + rescue => e + m.reply _("unintelligible time") + return + end + + if zone = @registry[m.sourcenick] + time = time.convert_zone(zone) + end + + m.reply _("%{time} %{w} %{str}") % { + :time => time.strftime(_("%a, %d %b %Y %H:%M:%S %Z %z")), + :str => Utils.timeago(time), + :w => time >= now ? _("is") : _("was") + } + end +end + +class ::Time + def convert_zone(to_zone) + original_zone = ENV["TZ"] + utc_time = dup.gmtime + ENV["TZ"] = to_zone + to_zone_time = utc_time.localtime + ENV["TZ"] = original_zone + return to_zone_time + end end plugin = TimePlugin.new |