# File.mtime: Bug on Windows?

**URL:** <https://www.ruby-forum.com/t/file-mtime-bug-on-windows/56445>\
**Category:** Ruby\
**Created:** [March 31, 2006, 8:14am UTC](https://www.ruby-forum.com/t/file-mtime-bug-on-windows/56445 "2006-03-31T08:14:22Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![worldfox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e480ec/32.png) [@worldfox](https://www.ruby-forum.com/u/worldfox)\
**Post date:** [March 31, 2006, 8:14am UTC](https://www.ruby-forum.com/t/file-mtime-bug-on-windows/56445/1 "2006-03-31T08:14:22Z")

</div>

Hello,

I think, there is a bug in File.mtime, unfortunately:

Using:  
ruby 1.8.4 (2005-12-24) [i386-mswin32]  
Windows XP with NTFS filesystem

When reading File.mtime( ‘any existing filename’),  
the result is different by one hour, when doing this with the  
computer clock set to a daylight saving time (DST) and doing this  
with the computer clock set to a standard time.

A simple test can be done with this:

fn = ‘any existing filename’ # change this!  
p Time.now

t = File.mtime( fn )  
puts "File.mtime = " + t.to\_s  
puts "File.mtime.to\_i = " + t.to\_i.to\_s

t.utc  
puts "utc: " + t.to\_s  
puts "utc.to\_i = " + t.to\_i.to\_s

On Windows 98SE with FAT32 filesystem, I do _not_ get this bug.

I supose, this is related to what is descriped here:  
  
[http://search.cpan.org/~shay/Win32-UTCFileTime-](http://search.cpan.org/~shay/Win32-UTCFileTime-)  
1.45/lib/Win32/UTCFileTime.pm

(Thank you very much to Daniel B. for pointing me to this!)

This made it difficult for me to compare filetimes.

I like programming with Ruby very much, hence I would be glad to have  
this working OK.

Axel

PS.:  
If you like to have a more elaborated code for checking this, you may  
take this:

puts \<\<-HERE  
Step 1: Set computer-clock to Summer Time (DST), manualy.  
Step 2: Create 2 files, first file with mtime set  
to Winter Time, second file with mtime set to Summer time.  
Step 3: Set computer-clock to Winter Time (not DST), manualy.  
Step 4: Create another 2 files, first file with mtime set  
to Winter Time, second file with mtime set to Summer time.  
Read File.mtime and File.mtime.to\_i from all 4 files  
Keep a copy of the result.  
Step 5: Set computer-clock to Summer Time (DST);  
Read File.mtime and File.mtime.to\_i from all 4 files  
Step 6: Compare the result by visual inspection  
Step 7: Delete the obsolet files

HERE

$stdout.sync = true  
puts "Enter step No. please [1…7]: "  
step = gets.chomp

case step.to\_i  
when 1  
puts “Set computer-clock to Summer Time (DST) now!”  
puts "Computer is set to: " + Time.now.to\_s  
when 2  
puts “Creating files:”  
p timeInWinter = Time.mktime( 2005, 12 )  
p timeInSummer = Time.mktime( 2005, 7 )

filename = ‘c:/SetInSummer2TimeInWinter.txt’  
File.open( filename, ‘w’ ) { }  
File.utime(timeInWinter, timeInWinter, filename )

filename = ‘c:/SetInSummer2TimeInSummer.txt’  
File.open( filename, ‘w’ ) { }  
File.utime(timeInSummer, timeInSummer, filename )  
when 3  
puts “Set computer-clock to Winter Time (not DST) now!”  
puts "Now: " + Time.now.to\_s  
when 4  
puts “Creating files:”  
p timeInWinter = Time.mktime( 2005, 12 )  
p timeInSummer = Time.mktime( 2005, 7 )

filename = ‘c:/SetInWinter2TimeInWinter.txt’  
File.open( filename, ‘w’ ) { }  
File.utime(timeInWinter, timeInWinter, filename )

filename = ‘c:/SetInWinter2TimeInSummer.txt’  
File.open( filename, ‘w’ ) { }  
File.utime(timeInSummer, timeInSummer, filename )

puts "Now reading file times: "  
puts "Computer is set to: " + Time.now.to\_s

p filename = ‘c:/SetInSummer2TimeInWinter.txt’  
puts “File.mtime” + File.mtime( filename ).to\_s  
puts "File.mtime.to\_i = " + File.mtime( filename ).to\_i.to\_s  
t = File.mtime( filename )  
t.utc  
puts "utc: " + t.to\_s  
puts "utc.to\_i = " + t.to\_i.to\_s  
puts

p filename = ‘c:/SetInSummer2TimeInSummer.txt’  
puts “File.mtime” + File.mtime( filename ).to\_s  
puts "File.mtime.to\_i = " + File.mtime( filename ).to\_i.to\_s  
t = File.mtime( filename )  
t.utc  
puts "utc: " + t.to\_s  
puts "utc.to\_i = " + t.to\_i.to\_s  
puts

p filename = ‘c:/SetInWinter2TimeInWinter.txt’  
puts “File.mtime” + File.mtime( filename ).to\_s  
puts "File.mtime.to\_i = " + File.mtime( filename ).to\_i.to\_s  
t = File.mtime( filename )  
t.utc  
puts "utc: " + t.to\_s  
puts "utc.to\_i = " + t.to\_i.to\_s  
puts

p filename = ‘c:/SetInWinter2TimeInSummer.txt’  
puts “File.mtime” + File.mtime( filename ).to\_s  
puts "File.mtime.to\_i = " + File.mtime( filename ).to\_i.to\_s  
t = File.mtime( filename )  
t.utc  
puts "utc: " + t.to\_s  
puts "utc.to\_i = " + t.to\_i.to\_s  
puts  
when 5  
puts “Set computer-clock to Summer Time (DST) now!”  
puts "Computer is set to: " + Time.now.to\_s  
when 6  
puts "Now reading file times: "  
puts "Computer is set to: " + Time.now.to\_s

p filename = ‘c:/SetInSummer2TimeInWinter.txt’  
puts “File.mtime” + File.mtime( filename ).to\_s  
puts "File.mtime.to\_i = " + File.mtime( filename ).to\_i.to\_s  
t = File.mtime( filename )  
t.utc  
puts "utc: " + t.to\_s  
puts "utc.to\_i = " + t.to\_i.to\_s  
puts

p filename = ‘c:/SetInSummer2TimeInSummer.txt’  
puts “File.mtime” + File.mtime( filename ).to\_s  
puts "File.mtime.to\_i = " + File.mtime( filename ).to\_i.to\_s  
t = File.mtime( filename )  
t.utc  
puts "utc: " + t.to\_s  
puts "utc.to\_i = " + t.to\_i.to\_s  
puts

p filename = ‘c:/SetInWinter2TimeInWinter.txt’  
puts “File.mtime” + File.mtime( filename ).to\_s  
puts "File.mtime.to\_i = " + File.mtime( filename ).to\_i.to\_s  
t = File.mtime( filename )  
t.utc  
puts "utc: " + t.to\_s  
puts "utc.to\_i = " + t.to\_i.to\_s  
puts

p filename = ‘c:/SetInWinter2TimeInSummer.txt’  
puts “File.mtime” + File.mtime( filename ).to\_s  
puts "File.mtime.to\_i = " + File.mtime( filename ).to\_i.to\_s  
t = File.mtime( filename )  
t.utc  
puts "utc: " + t.to\_s  
puts "utc.to\_i = " + t.to\_i.to\_s  
puts  
when 7  
puts “Deleting all files…”  
p filename = ‘c:/SetInSummer2TimeInWinter.txt’  
File.delete( filename )  
p filename = ‘c:/SetInSummer2TimeInSummer.txt’  
File.delete( filename )  
p filename = ‘c:/SetInWinter2TimeInWinter.txt’  
File.delete( filename )  
p filename = ‘c:/SetInWinter2TimeInSummer.txt’  
File.delete( filename )  
else  
puts “Choose a number from 1 to 7”  
end
