Debugging Ruby Script For Changing Timezone Configuration File On Ubuntu - ruby

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 ruby 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

Related

How to silence `Shell` (ruby stdlib) `notify` feature?

require 'shell'
sh = Shell.new
sh.verbose = false
sh.debug = false
print sh.system('date')
Expected output
Wed Nov 10 13:08:09 EST 2021
Observed output
shell(#<Th:0x00007fd016064058 run>): /bin/date
Wed Nov 10 13:08:09 EST 2021
The unwanted line comes from Shell::CommandProcessor#notify
After some investigation there seems to be a confusing design flaw (or feature?) in Shell. Initally it seems you should be able to mutate verbose and debug on an instance of Shell, but it turns out the root notify method itself lives in the class, not the instance.
Hence instance level settings will only have limited effect, as the class method Shell.notify will be unaware of instance settings.
Solution - use class-level settings instead:
Shell.verbose = false
Shell.debug = false
Output will now be only:
Wed Nov 10 13:08:09 EST 2021

How to set the correct local time zone in git bash?

I am using git-bash on a Windows system.
The Windows clock shows local time, but inside git-bash everything is in GMT time:
$ date
Mon Mar 31 16:08:57 GMT 2014
Also setting TZ will not change things:
$ TZ="Europe/Berlin" date
Mon Mar 31 16:09:01 GMT 2014
Similarly all times it git log are GMT only.
Is there a way to set the correct timezone in git-bash?
On Windows the TZ variable seems to work differently.
To get the German timezone you have to write:
TZ=GST-1GDT date
If you set it to some "invalid" value like "Europe/Berlin" it will default to GMT.
The same seems to happen on my system when TZ is not set at all.
With the above setting I get Thu Apr 17 16:23:23 GDT 2014 which is not exactly the same as Thu Apr 17 16:23:23 CEST 2014, but at least the time looks right.
Same problem here in my script. Windows was showing 15:47 and the "date" command in gitbash was answering 13:47.
export TZ="CEST-2"
This fixed the problem for me. I wanted Paris time.

Rails getting the current time without the date

I know there are a lot of questions regarding date and time for Rails, but I can't seem to solve the problem I have.
I'm probably just being stupid about this.. anyway, my problem is that I want to get the current time without the date attached.
For example, when I use the following I get:
Time.now => 2012-06-29 09:23:04 -0400
or
Time.current => Fri, 29 Jun 2012 09:23:23 EDT -04:00
I would like to obtain the current time without having the date. i.e. just
09:23:04 -0400
or
09:23:23 EDT -04:00
If there are questions out there that already addresses this, I'd appreciate being pointed to them.
Thanks!
Use strftime to get the bits of the time that you want, in the format you want them.
puts Time.now.strftime("%I:%M:%S %z") # "09:33:00 -0400"
puts Time.now.strftime("%I:%M:%S %Z %z") # "09:33:00 EDT -0400"
Time.now.strftime("%H:%M:%S") # "18:33:00"
Don't use %I without %p (won't know am/pm)

Any way to get dir entries without changing atime in Ruby?

It looks Dir.entries("dir") updates the dir's atime on Linux.
irb(main):042:0> File::Stat.new("/tmp/tmp2").atime
=> Thu Aug 25 09:16:36 -0700 2011
irb(main):043:0> File::Stat.new("/tmp/tmp2").atime
=> Thu Aug 25 09:16:36 -0700 2011
irb(main):044:0> Dir.entries("/tmp/tmp2")
=> ["file1", "..", ".", "dir1"]
irb(main):045:0> File::Stat.new("/tmp/tmp2").atime
=> Thu Aug 25 09:16:49 -0700 2011
Is it possible to get the dir entries without changing the atime of itself in Ruby?
There's no way to do it in Ruby, or even in general. Reading any file or directory, by any method, will always update its atime. (Unless the whole filesystem is set to not use atimes with a mount flag, like noatime or relatime.)

How can I use the Chronic natural language date/time parser to parse "12:00" as 12:00 PM?

I am using the ruby gem "Chronic" to parse four digit strings as DateTime objects. I am using time in military format (ie: "0800") which seems from the documentaion to be a valid format.
In most cases, Chronic parses time in this format correctly - however it always parses a four digit string beginning with "12" as 00:XX AM of the next day, never as 12:XX PM of the current day.
For example:
>> Chronic.parse("1234")
=> Thu Sep 17 00:34:00 -0600 2009
I see that if I put a colon between the hours and minutes I get the desired output:
>> Chronic.parse("12:34")
=> Wed Sep 16 12:34:00 -0600 2009
I am however wanting to pass the value without a colon, like this:
>> Chronic.parse("1234")
=> Wed Sep 16 12:34:00 -0600 2009
What string do I have to pass to the parser in order for Chronic to interpret "1234" as 12:34 PM of the current day?
I'm not certain, but it looks like it might be a bug. My guess is you're ending up in this corner of the code:
http://github.com/mojombo/chronic/commit/c7d9591acf5179345cbc916bd509c48acee8e744

Resources