Is it possible to read a file's modification date with Ruby? - ruby

Is it possible to read a file's modification date with Ruby? I have successfully opened a text file and captured the contents of the file with
File.open("test.txt", "r").each do |line|"
but it would be very useful to read the modification date of the file.

Use mtime:
File.mtime("testfile")
=> 2014-04-13 16:00:23 -0300

"Returns the modification time for the named file as a Time object."
File.mtime("testfile") #=> Tue Apr 08 12:58:04 CDT 2003
Check this.

Related

Parse line for specific date format?

Writing a script using bash. I am trying to look through lines in a file for a specific date format:
date +"%a %b %d %T %Z %Y"
For example, if the line were
/foo/bar/foobar this 12 is 411 arbitrary stuff in the line Wed Jun 10 10:10:10 PST 2017
I would want to obtain Wed Jun 10 10:10:10 PST 2017.
Any way to search for specific date formats?
I'm not sure whether you'll agree with this approach. But if this is for some quick, non-recurring work, I won't look for a perfect solution that can handle all the scenarios.
To start with, you can use the following too generic pattern to match the part you want.
cat file | sed -n 's/.*\(... ... .. ..:..:.. ... ....\).*/\1/p'
Then you can enhance this further restricting the matches as you need.
E.g.
cat file | sed -n 's/.*\([a-Z]\{3\} [a-Z]\{3\} [0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9] [A-Z]\{3\} [0-9]\{4\}\).*/\1/p'
Note that this still is not perfect and can match invalid contents. If you find it still not good enough, you can further fine tune the pattern to the point you want.

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

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

Resources