4 # :title: Time Zone Plugin for rbot
6 # Author:: Ian Monroe <ian@monroe.nu>
7 # Author:: Raine Virta <raine.virta@gmail.com>
8 # Copyright:: (C) 2006 Ian Monroe
9 # Copyright:: (C) 2010 Raine Virta
10 # License:: MIT license
14 class TimePlugin < Plugin
15 def help(plugin, topic="")
18 _("usage: time set <Continent>/<City> -- setting your location allows the bot to calibrate time replies into your time zone, and other people to figure out what time it is for you")
20 _("usage: time <timestamp|time zone|nick> -- %{b}timestamp%{b}: get info about a specific time, relative to your own time zone | %{b}time zone%{b}: get local time of a certain location, <time zone> can be '<Continent>/<City>' or a two character country code | %{b}nick%{b}: get local time of another person, given they have set their location | see `%{prefix}help time set` on how to set your location") % {
22 :prefix => @bot.config['core.address_prefix'].first
29 # this plugin only wants to store strings
41 if zone.length == 2 then #country code
43 zone = 'GB' if zone == 'UK' #country doesn't know its own name
45 nationZones = TZInfo::Country.get(zone).zone_identifiers
46 if nationZones.size == 1 then
49 m.reply "#{zone} has the cities of #{nationZones.join( ', ' )}."
51 rescue TZInfo::InvalidCountryCode
52 m.reply "#{zone} is not a valid country code."
55 ['/', '_'].each { |sp|
57 zone.split(sp).each{ |s|
59 s[1, s.length] = s[1, s.length].downcase if sp == '/'
64 tz = TZInfo::Timezone.get( zone )
65 "#{tz.friendly_identifier} - #{tz.now.strftime( '%a %b %d %H:%M' )} #{tz.current_period.abbreviation}"
68 def showTime(m, params)
69 zone = params[:where].join('_')
70 if params[:where].size > 0 then
72 m.reply getTime( m, zone )
73 rescue TZInfo::InvalidTimezoneIdentifier
74 if @registry.has_key?( zone ) then
75 zone = @registry[ zone ]
76 m.reply getTime( m, zone )
82 if @registry.has_key?( m.sourcenick) then
83 zone = @registry[ m.sourcenick ]
84 m.reply "#{m.sourcenick}: #{getTime( m, zone )}"
86 m.reply "#{m.sourcenick}: use time set <Continent>/<City> to set your time zone."
91 def setUserZone( m, params )
92 if params[:where].size > 0 then
93 s = setZone( m, m.sourcenick, params[:where].join('_') )
95 m.reply "Requires <Continent>/<City> or country code"
99 def resetUserZone( m, params )
100 s = resetZone( m, m.sourcenick)
103 def setAdminZone( m, params )
104 if params[:who] and params[:where].size > 0 then
105 s = setZone( m, params[:who], params[:where].join('_') )
107 m.reply "Requires a nick and the <Continent>/<City> or country code"
111 def resetAdminZone( m, params )
113 s = resetZone( m, params[:who])
115 m.reply "Requires a nick"
119 def setZone( m, user, zone )
122 rescue TZInfo::InvalidTimezoneIdentifier
123 m.reply "#{zone} is an invalid time zone. Format is <Continent>/<City> or a two character country code."
126 @registry[ user ] = zone
127 m.reply "Ok, I'll remember that #{user} is on the #{zone} time zone"
130 def resetZone( m, user )
131 @registry.delete(user)
132 m.reply "Ok, I've forgotten #{user}'s time zone"
137 str = params[:where].to_s
142 if zone = @registry[m.sourcenick]
149 rescue ArgumentError => e
150 # Handle 28/9/1978, which is a valid date representation at least in Italy
151 if e.message == 'argument out of range'
159 offset = (time - now).abs
160 raise if offset < 0.1
162 if str.match(/^\d+$/)
163 time = Time.at(str.to_i)
165 m.reply _("unintelligible time")
170 if zone = @registry[m.sourcenick]
171 time = time.convert_zone(zone)
174 m.reply _("%{time} %{w} %{str}") % {
175 :time => time.strftime(_("%a, %d %b %Y %H:%M:%S %Z %z")),
176 :str => Utils.timeago(time),
177 :w => time >= now ? _("is") : _("was")
181 def on_timezone(to_zone)
182 original_zone = ENV["TZ"]
185 ENV["TZ"] = original_zone
191 def convert_zone(to_zone)
192 original_zone = ENV["TZ"]
193 utc_time = dup.gmtime
195 to_zone_time = utc_time.localtime
196 ENV["TZ"] = original_zone
201 plugin = TimePlugin.new
203 plugin.default_auth('admin', false)
205 plugin.map 'time set [time][zone] [to] *where', :action=> 'setUserZone', :defaults => {:where => false}
206 plugin.map 'time reset [time][zone]', :action=> 'resetUserZone'
207 plugin.map 'time admin set [time][zone] [for] :who [to] *where', :action=> 'setAdminZone', :defaults => {:who => false, :where => false}
208 plugin.map 'time admin reset [time][zone] [for] :who', :action=> 'resetAdminZone', :defaults => {:who => false}
209 plugin.map 'time *where', :action => 'showTime', :defaults => {:where => false}