[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

utime a dir and convert Date to Time

Matt Spendlove

1/15/2008 11:15:00 AM

Hi list

I am learning some core Ruby (non Rails env) by playing with some File
i/o stuff and running under JRuby

1) How do I utime a Dir? - Dir doesn't seem to have the method and
passing a dir to File.utime seems to just get ignored.

2) My script needs to compare the mtime of a file with a constant
offset. I ended up with this ugliness, turning both into Dates because
I couldn't figure out how to turn a TWO_WEEKS_AGO into a Time! :

====================================
TWO_WEEKS_AGO = Date.today - 14

if Date.parse(File.stat(path).mtime.to_s) > TWO_WEEKS_AGO
3 Answers

Tiziano Merzi

1/15/2008 2:44:00 PM

0

Hi,

Matt Spendlove wrote:
> Hi list
>
> I am learning some core Ruby (non Rails env) by playing with some File
> i/o stuff and running under JRuby
>
> 1) How do I utime a Dir? - Dir doesn't seem to have the method and
> passing a dir to File.utime seems to just get ignored.
>

File.utime works with C ruby and jruby 1.1 (I haven't try 1.0)
Note: in jruby if the path is absolute "\\temp" try with "c:\\temp"

> 2) My script needs to compare the mtime of a file with a constant
> offset. I ended up with this ugliness, turning both into Dates because
> I couldn't figure out how to turn a TWO_WEEKS_AGO into a Time! :
>
> ====================================
> TWO_WEEKS_AGO = Date.today - 14
>
> if Date.parse(File.stat(path).mtime.to_s) > TWO_WEEKS_AGO
> .
> .
> end
> ====================================
>
> Any suggestions welcome.
>

DAY_DURATION = 24*60*60
date = Time.now - 14*DAY_DURATION

puts File.mtime("c:/temp")
File.utime(date,date, "c:/temp")
puts File.mtime("c:/temp")

tiziano

--
Posted via http://www.ruby-....

Matt Spendlove

1/15/2008 5:41:00 PM

0

> File.utime works with C ruby and jruby 1.1 (I haven't try 1.0)
> Note: in jruby if the path is absolute "\\temp" try with "c:\\temp"

Yea, quite right. My simple mistake was to issue a FileUtils.mkdir
after the utime on a subdir.

> > 2) My script needs to compare the mtime of a file with a constant
> > offset. I ended up with this ugliness, turning both into Dates because
> > I couldn't figure out how to turn a TWO_WEEKS_AGO into a Time! :
> >
> > ====================================
> > TWO_WEEKS_AGO = Date.today - 14
> >
> > if Date.parse(File.stat(path).mtime.to_s) > TWO_WEEKS_AGO
> > .
> > .
> > end
> > ====================================
> >
> > Any suggestions welcome.
> >
>
> DAY_DURATION = 24*60*60
> date = Time.now - 14*DAY_DURATION

Right, I get this but there is no way to convert a Date to a Time? I
guess I was looking for timeInMillis or something. Perhaps just need
to loose the Java head ;)

Thanks.

Siep Korteling

1/15/2008 10:00:00 PM

0

Matt Spendlove wrote:
(...)
>> >
>>
>> DAY_DURATION = 24*60*60
>> date = Time.now - 14*DAY_DURATION
>
> Right, I get this but there is no way to convert a Date to a Time? I
> guess I was looking for timeInMillis or something. Perhaps just need
> to loose the Java head ;)
>
> Thanks.

timeInMillis would be stored in a fixnum. Hmm, let's try .to_f .

date_in_millis = Time.now.to_f - 14*DAY_DURATION
=>1199223885.312

To revert this:

puts Time.at(date_in_millis)
Tue Jan 01 22:52:52 +0100 2008

(I had to look this one up).

Regards,

Siep
--
Posted via http://www.ruby-....