Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

File.lastModified() on windows vs linux

 
rajesh chalavadi
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
File.lastModified() javadoc says - A long value representing the time the file was last modified, measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970), or 0L if the file does not exist or if an I/O error occurs

But this method is rounding off the value on the Linux, to its nearest second unlike windows.
(for example if a file is modified at 1173423665215 msec, the above method is returning the value 1173423665000 on linux.)

1) Is there a way to avoid this rounding off on linux? OR
2) Are there any other methods available to get the file modified time in milliseconds.
3) Any method to know what is the precision of platform ( 1msec on windows and 1sec on linux.)

Any help is highly appreciated.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wikipedia tells me that for ext3 filesystems the timeresolution is 1s.
http://en.wikipedia.org/wiki/Ext3
The same for reiserfs.
http://en.wikipedia.org/wiki/Reiserfs
For vfat, no information.
ntfs: 100ns
http://en.wikipedia.org/wiki/Ntfs

Other documentations of the the filesystems should include that info as well.

But:
When I use
while /mnt/c is a mounted ntfs volume, I get a nanotime, and
which points to a reiserfs gives me an time in seconds, a small Java-test-program produces times of even granularity.


Do you like to have an look into the sources, to find out why?
 
bart zagers
Ranch Hand
Posts: 234
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question is, do you really need millisecond precision?
As far as I know, even Windows does not give you really millisecond precision, but only an "approximation".
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by bart zagers:
The question is, do you really need millisecond precision?
As far as I know, even Windows does not give you really millisecond precision, but only an "approximation".


It might not be of much relevance to Rajesh, while his question doesn't support that idea too much, but the java documentation claims:

Returns:
A long value representing the time the file was last modified,
measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970),
or 0L if the file does not exist or if an I/O error occurs

which is simply wrong.
It doesn't.

java.io.File.lastModified itself calls java.io.FileSystem.getLastModifiedTime (File f)
But I don't have the source of the native implementation.
Perhaps I should
[ February 15, 2008: Message edited by: Stefan Wagner ]
 
rajesh chalavadi
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stefan and bart for your replies.

I am not worrying about millisecond precision. But looking for a way to get the same behavior across the platforms.

As part of my requirements, am storing the file fetched time (System.currentTimeinMillis() ) and before updating the file, am comparing the fetched time with file.lastModifiedTime() to know whether the file got updated from the point we read it.

But because of precision problem, the code behaves in different ways on different platforms!.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47270
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
time / 1000 * 1000?
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if ((Math.abs (modified[0]-modified[1]) > 1000)
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic