Forum: Ruby File.mtime: Bug on Windows?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Axel F. (Guest)
on 2006-03-31 10:14
(Received via mailing list)
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:
<longline>
http://search.cpan.org/~shay/Win32-UTCFileTime-
1.45/lib/Win32/UTCFileTime.pm
</longline>

(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
This topic is locked and can not be replied to.