Created
August 20, 2010 18:03
-
-
Save rkumar/540828 to your computer and use it in GitHub Desktop.
convert between timezones
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ruby -w | |
| # input may be given as "2:00 PM" or "14:00" | |
| # timeconverter.rb "2:00 PM" "5:00 PM" | |
| # timeconverter.rb --from="America/Denver" "2:00 PM" | |
| # http://tzinfo.rubyforge.org/doc/files/README.html | |
| require 'rubygems' | |
| require 'tzinfo' | |
| require 'time' | |
| require 'choice' | |
| module Choice | |
| def get_unparsed_args(args) | |
| flat=Choice.choices.to_a.flatten | |
| args.delete_if { |a| a[0].chr == '-' || a[0,1] == '--' || flat.include?(a) } | |
| args | |
| end | |
| end | |
| PROGRAM_VERSION = 0.1 | |
| Choice.options do | |
| header '' | |
| header 'Specific options:' | |
| #option :from, :required => true do | |
| option :from do | |
| short '-f' | |
| long '--from=timezone' | |
| desc 'The timezone of the time you are giving as argument. Default America/New_York' | |
| default 'America/New_York' | |
| end | |
| ## Added since this lib does not ARGV.shift | |
| #option :time, :required => true do | |
| option :time do | |
| short '-t' | |
| long '--time=time' | |
| desc 'The time of the place, to be converted. e.g. 12:00 PM or 23:34' | |
| end | |
| option :countries do | |
| long '--countries=[PATTERN]' | |
| desc 'List all countries, or those matching a pattern' | |
| action do |value| | |
| if value == true | |
| puts TZInfo::Country.all.sort | |
| else | |
| patt= value | |
| puts TZInfo::Country.all.sort.find_all{|item| item.name =~ /#{patt}/; } | |
| end | |
| exit | |
| end | |
| end | |
| option :countrycodes do | |
| short '-c' | |
| long '--countrycodes' | |
| desc 'List all country codes' | |
| action do | |
| puts TZInfo::Country.all_codes.sort | |
| exit | |
| end | |
| end | |
| option :listtimezones do | |
| long '--listtimezones=COUNTRY_CODE' | |
| desc 'List timezones for given country code. Use -c to fetch codes.' | |
| action do |value| | |
| patt= value | |
| cc = TZInfo::Country.get(patt) | |
| timezones = cc.zone_identifiers; | |
| puts timezones; | |
| exit | |
| end | |
| end | |
| separator '' | |
| separator 'Common options: ' | |
| option :help do | |
| long '--help' | |
| desc 'Show this message' | |
| end | |
| option :version do | |
| short '-v' | |
| long '--version' | |
| desc 'Show version' | |
| action do | |
| puts "timeconverter.rb v#{PROGRAM_VERSION}" | |
| exit | |
| end | |
| end | |
| end | |
| ### I am unable to get the balance arguments in ARGV since the options have not been shifted. | |
| puts "ARGS:" , Choice.args | |
| puts "END ARGS" | |
| unparsed=Choice.get_unparsed_args(ARGV) | |
| #timestr=Choice.choices[:time] | |
| tz = TZInfo::Timezone.get(Choice.choices[:from]) | |
| tz1 = TZInfo::Timezone.get('Asia/Calcutta') | |
| #local = tz.utc_to_local(Time.utc(2008,9,4,14,00,0)) | |
| unparsed.each {|timestr| | |
| puts timestr | |
| local = tz.local_to_utc(Time.parse(timestr)) | |
| puts tz1.utc_to_local(local) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment