tags:

views:

32

answers:

2

I want to subtract the result of File.mtime and Date.today. Time and weekends are ignored. I am after something like

Date.today - File.mtime

File.mtime gives me Fri Oct 08 11:00:18 +1100 2010 and Date.today 2010-10-11

I thought that to_s() would work for me but File.mtime(filename).to_s gives me the same result like File.mtime(filename)

any idea how I can get the desired date format from File.mtime? And why to_s is not working?

+1  A: 

Something like this?

irb(main):001:0> File.mtime("file")
=> 2010-10-08 17:56:10 +0800
irb(main):002:0> File.mtime("file").year
=> 2010
irb(main):003:0> File.mtime("file").month
=> 10
irb(main):004:0> File.mtime("file").day
=> 8

Similarly with Date

irb(main):001:0> require 'date'         
=> true                                 
irb(main):002:0> Date.today         
=> #<Date: 2010-10-11 (4910961/2,0,2299161)>
irb(main):003:0> Date.today.year            
=> 2010                                     
irb(main):004:0> Date.today.month
=> 10                            
irb(main):005:0> Date.today.day
=> 11     

Or you can use strftime

irb(main):001:0> File.mtime("file").strftime("%Y-%m-%d")
=> "2010-10-08"
ghostdog74
@ghostdog74: yes, something like that. Do I have to use .year .month and .day for File.mtime or is there any way how I can use one command/methond
Radek
see my edit for another way
ghostdog74
yes, it works. One more and last question. Any easier way how to write `puts ( (Date.strptime(File.mtime("file").strftime("%Y-%m-%d")))..(Date.today)).select {|d| (1..5).include?(d.wday) }.size - 1` it does what I want but it seems to me written too complicated
Radek
+1  A: 

Try using Time.now and convert to days manually:

age = Time.now - File.mtime(filename)
age_in_days = (age / 24*60*60).to_i
Lars Haugseth
not sure if I would be able to use .wday I am only after week days
Radek
@Radek: Fair enough, though that was hard to guess from the way you posed your question.
Lars Haugseth
@Lars Haugseth: you're right. It wasn't in my original question. I updated it.
Radek