# Utime a dir and convert Date to Time

**URL:** <https://www.ruby-forum.com/t/utime-a-dir-and-convert-date-to-time/126094>\
**Category:** Ruby\
**Created:** [January 15, 2008, 12:15pm UTC](https://www.ruby-forum.com/t/utime-a-dir-and-convert-date-to-time/126094 "2008-01-15T12:15:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![matthew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@matthew](https://www.ruby-forum.com/u/matthew)\
**Post date:** [January 15, 2008, 12:15pm UTC](https://www.ruby-forum.com/t/utime-a-dir-and-convert-date-to-time/126094/1 "2008-01-15T12:15:07Z")

</div>

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

Any suggestions welcome.

Yours ignorantly…

---

<div class="post-metadata">

**Author:** ![matthew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@matthew](https://www.ruby-forum.com/u/matthew)\
**Post date:** [January 15, 2008, 3:44pm UTC](https://www.ruby-forum.com/t/utime-a-dir-and-convert-date-to-time/126094/2 "2008-01-15T15:44:19Z")

</div>

Hi,

Matt S. 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”

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

---

<div class="post-metadata">

**Author:** ![matthew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@matthew](https://www.ruby-forum.com/u/matthew)\
**Post date:** [January 15, 2008, 6:43pm UTC](https://www.ruby-forum.com/t/utime-a-dir-and-convert-date-to-time/126094/3 "2008-01-15T18:43:31Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![matthew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@matthew](https://www.ruby-forum.com/u/matthew)\
**Post date:** [January 15, 2008, 10:57pm UTC](https://www.ruby-forum.com/t/utime-a-dir-and-convert-date-to-time/126094/4 "2008-01-15T22:57:46Z")

</div>

Matt S. 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
