Any way to get dir entries without changing atime in Ruby? - 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.)

Related

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)

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

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

The right ISO 8601 format

I am refactoring some code for a Ruby library. This code includes a Date parser.
One of the tests was to parse this string "2008-02-20T8:05:00-010:00" which is supposed to be ISO 8601.
The previous code would actually output: "Wed Feb 20 18:05:00 UTC 2008".
My new code outputs that: "Wed Feb 20 16:05:00 UTC 2008".
My question is: which one is the right one?
Time.parse in Ruby gives the second one. But again, I want to be 100% sure that the previous code AND test were buggy.
Which one is right? (By maybe parsing the string with a library in another language? - I only know Ruby.)
The correct UTC time is 1805. The time group indicates 0805 in zone -10, so to get UTC add the 10 to the given time. Thus 1805. Since 1805 is less than 2400 it's the same day.
If your code is giving 1605, then you almost certainly have the timezone set incorrectly to zone -8, which happens to be Pacific Standard Time.
Aha, looks like your input format is messed up. Observe:
irb(main):003:0> Time.parse("2008-02-20T8:05:00-010:00")
=> Wed Feb 20 08:05:00 -0700 2008
I happen to be in zone -7 so it's suiting that to my locale. But
irb(main):004:0> t=Time.parse("2008-02-20T8:05:00-010:00")
=> Wed Feb 20 08:05:00 -0700 2008
irb(main):005:0> t
=> Wed Feb 20 08:05:00 -0700 2008
irb(main):006:0> t.getutc
=> Wed Feb 20 15:05:00 UTC 2008
I'm getting an unexpected result. Now observe:
irb(main):007:0> t=Time.parse("2008-02-20T8:05:00-10:00")
=> Wed Feb 20 11:05:00 -0700 2008
irb(main):008:0> t.getutc
=> Wed Feb 20 18:05:00 UTC 2008
There's the expected result. See the difference? First example vs second:
irb(main):004:0> t=Time.parse("2008-02-20T8:05:00-010:00")
irb(main):007:0> t=Time.parse("2008-02-20T8:05:00-10:00")
I took the spurious extra 0 out (which I certainly didn't notice either) and whoosh, it works.
I know this is pretty old, but I just ran across it.
I'll bet that something somewhere is interpreting 010 as an octal number with the value 8. Perhaps it's a bug in the implementation of Time.parse()?

Resources