Debugging Ruby Script For Changing Timezone Configuration File On Ubuntu
require 'java'
if ARGV.length == 0
puts "Usage: jruby change_timezone.rb America/Toronto"
exit
end
old_zone = File.read("../../../etc/timezone")
puts old_zone
time1 = Time.now
puts "Current Time:"+time1.localtime.to_s
new_zone = ARGV[0]
open('../../../etc/timezone','w') do |f|
f.puts new_zone.to_s
f.close
end
new_zone = File.read("../../../etc/timezone")
puts new_zone
time2 = Time.now
puts "Updated Time:"+time2.localtime.to_s
Above is a rub开发者_StackOverflow中文版y script I wrote to change the timezone configuration on ubuntu. It does change the configuration file properly, however, the output for the script is Not as expected.
Assume the default value for timezone is America/Toronto. Now run the command, jruby change_timezone.rb Asia/Chongqing, then here's the output:
America/Toronto
Current Time:Thu Jul 07 14:43:23 -0400 2011
Asia/Chongqing
Updated Time:Thu Jul 07 14:43:23 -0400 2011 (My Note: +0800 expected!!!)
Continue with the command, jruby change_timezone.rb Europe/Amsterdam, end up with the following:
Asia/Chongqing
Current Time:Fri Jul 08 03:18:25 +0800 2011 (My Note: it actually got updated from last run!!!)
Europe/Amsterdam
Updated Time:Fri Jul 08 03:18:25 +0800 2011 (My Note: +0200 expected!!!)
Go further with, jruby change_timezone.rb Europe/Amsterdam (My Note: in effect repeating the last command), and get the following:
Europe/Amsterdam
Current Time:Thu Jul 07 21:21:27 +0200 2011
Europe/Amsterdam
Updated Time:Thu Jul 07 21:21:27 +0200 2011
Can someone figure out why it didn't work as expected?
For almost any Linux distribution '/etc/localtime' is a symlink to the correct timezone file or is the effective timezone file. The file '/etc/timezone' is used by 'dpkg-reconfigure tzdata' command to generate (or symlink) the effective timezone file at '/etc/localtime'. Finally, timezone files are located at '/usr/share/zoneinfo/'. In summary I think you miss one final step after the file '/etc/timezone' get changed. It's to run:
$ dpkg-reconfigure tzdata
精彩评论