Skip to content

Instantly share code, notes, and snippets.

@fridim
Forked from vivien/timezone.rb
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fridim/9ccd597de24441e663cc to your computer and use it in GitHub Desktop.
Save fridim/9ccd597de24441e663cc to your computer and use it in GitHub Desktop.
Script to toggle timezones in i3blocks
#!/bin/ruby
#
# Toggle timezones in i3blocks
# Author: Vivien Didelot <vivien.didelot@gmail.com>
#
# Fill the ZONES array and define a block like this:
#
# [timezone]
# command=THIS_SCRIPT
# interval=5
ZONES = [
"Europe/Paris",
"America/Montreal",
"Indian/Reunion",
]
CACHE = "/tmp/tz"
def datetime zone, format
%x(TZ=#{zone} date '+#{format}')
end
if File.exist? CACHE
index = File.read(CACHE).to_i
else
index = 0
end
# toggle on click
if ENV["BLOCK_BUTTON"].to_i > 0
# cache the index
index = (index + 1) % ZONES.size
File.open(CACHE, 'w') { |f| f.write(index) }
end
zone = ZONES[index]
city = zone.split('/').last
puts "#{city}: #{datetime(zone, "%Y-%m-%d %T")}"
puts "#{city.chars.first}: #{datetime(zone, "%T")}"
puts "#77FF77"
# vim: ts=2 sw=2 et
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment