Skip to content

Instantly share code, notes, and snippets.

@rapha
Created April 7, 2009 02:31
Show Gist options
  • Save rapha/91065 to your computer and use it in GitHub Desktop.
Save rapha/91065 to your computer and use it in GitHub Desktop.
Ubiquity timezone command for Firefox
var noun_type_city = {
_name: "city",
_cities: undefined,
_fetchCityOffsets: function(callback, prefix) {
if (this._cities) {
this._suggestCities(callback, prefix)
} else {
var self = this
jQuery.get( 'http://www.raphscallion.com/clock/timezones.php', {}, function( response ) {
var lines = response.split('\n')
var offsets = lines.reduce(function(dict, line) {
var matches = /^\w+\/([\w_]+) (\d+)/.exec(line)
if (!matches) return dict
var city = matches[1].replace('_', ' ').toLowerCase()
var offset = parseInt(matches[2])
dict[city] = offset
return dict
}, {})
var cities = []
for (var city in offsets) {
cities.push(city)
}
self._cities = cities;
self._offsets = offsets
self._suggestCities(callback, prefix)
})
}
},
_suggestCities: function(callback, prefix) {
this._cities.filter(function(city) {
return city.indexOf(prefix.toLowerCase()) == 0
}).forEach(function(city) {
callback(CmdUtils.makeSugg(city))
})
},
getTimeAt: function(city) {
var gmtOffsetMinutes = this._offsets[city]
if (gmtOffsetMinutes == undefined) return 'unknown'
var timeHere = (new Date).getTime()
var timeThere = timeHere + gmtOffsetMinutes * 60 * 1000
return new Date(timeThere)
},
suggest: function (text, html, callback) {
this._fetchCityOffsets(callback, text)
return [CmdUtils.makeSugg()];
}
}
CmdUtils.CreateCommand({
name: "time",
takes: {"city": noun_type_city },
preview: function( previewBlock, city ) {
var template = "The current time in ${city} is ${time}"
var timeThere = noun_type_city.getTimeAt(city.text)
previewBlock.innerHTML = CmdUtils.renderTemplate( template, {'city': city.text, 'time': timeThere} )
},
execute: function(city) {
displayMessage( noun_type_city.getTimeAt(city.text).toString() );
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment