Ticket #7473 (new bug)

Opened 7 months ago

Last modified 7 weeks ago

getModificationTime gives only second-level resolution

Reported by: duncan Owned by:
Priority: normal Milestone: 7.8.1
Component: libraries/directory Version: 7.6.1
Keywords: Cc: the.dead.shall.rise@…
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Difficulty: Unknown
Test Case: Blocked By:
Blocking: Related Tickets:

Description

The System.Directory.getModificationTime function only returns time with second-level granularity. This is no good for applications like ghc --make where we need to compare file timestamps.

The fix should be simple, here's a patch (compiles but not tested) that should fix it for Unix, for Windows we will need a different fix, probably using the Win32 API directly, rather than the mingw C api.

diff --git a/System/Directory.hs b/System/Directory.hs
index cfe7cd9..f27bfc4 100644
--- a/System/Directory.hs
+++ b/System/Directory.hs
@@ -995,14 +995,14 @@ The operation may fail with:
 getModificationTime :: FilePath -> IO UTCTime
 getModificationTime name = do
 #ifdef mingw32_HOST_OS
- -- ToDo: use Win32 API
+ -- ToDo: use Win32 API so we can get sub-second resolution
  withFileStatus "getModificationTime" name $ \ st -> do
  modificationTime st
 #else
   stat <- Posix.getFileStatus name
-  let mod_time :: Posix.EpochTime
-      mod_time = Posix.modificationTime stat
-  return $ posixSecondsToUTCTime $ realToFrac mod_time
+  let mod_time :: POSIXTime
+      mod_time = Posix.modificationTimeHiRes stat
+  return $! posixSecondsToUTCTime mod_time
 #endif
 
 #endif /* __GLASGOW_HASKELL__ */

Change History

Changed 7 months ago by duncan

  • status changed from new to patch

Changed 7 months ago by duncan

On the issue of Win32, Malcolm notes:

Open Shake implements the finer resolution timestamps needed using the
Win32 API.  You may want to copy/adapt the code from there:

 http://hackage.haskell.org/packages/archive/shake/0.3.7/doc/html/src/Development-Shake-FileTime.html#line-69

Changed 7 months ago by igloo

  • difficulty set to Unknown

Related: #6160

Changed 6 months ago by igloo

  • status changed from patch to new
  • milestone set to 7.8.1

Applied, thanks. I'll leave the ticket open for the Windows half.

Changed 7 weeks ago by refold

  • cc the.dead.shall.rise@… added

Changed 7 weeks ago by refold

I've added a version of Neil's code to Cabal:

 https://github.com/haskell/cabal/blob/master/cabal-install/Distribution/Compat/Time.hs#L58

Perhaps it can be reused for directory.

Note: See TracTickets for help on using tickets.