structure FileIO :> FILE_IO = struct

val mostRecentModTimeRef = ref (Time.zeroTime)

fun checkFileModTime fname =
  let val mtime = OS.FileSys.modTime fname in
      if Time.compare (mtime, !mostRecentModTimeRef) = GREATER andalso
         Time.compare (mtime, Globals.getResetTime ()) = LESS
      then mostRecentModTimeRef := mtime
      else ()
  end

fun mostRecentModTime () =
  if Time.compare (!mostRecentModTimeRef, Time.zeroTime) = EQUAL
  then Globals.getResetTime ()
  else !mostRecentModTimeRef

fun txtOpenIn fname =
  let
      val inf = TextIO.openIn fname
      val () = checkFileModTime fname
  in
      inf
  end

fun binOpenIn fname =
  let
      val inf = BinIO.openIn fname
      val () = checkFileModTime fname
  in
      inf
  end

end

structure FileIO :> FILE_IO = struct

val mostRecentModTimeRef = ref (Time.zeroTime)

fun checkFileModTime fname =
  let
      val mtime = OS.FileSys.modTime fname
      val mostRecentMod = !mostRecentModTimeRef
      val resetTime = Globals.getResetTime ()
      fun lessThan (a, b) = LargeInt.compare (Time.toSeconds a, Time.toSeconds b) = LESS
      infix lessThan
  in
      if mostRecentMod lessThan mtime andalso mtime lessThan resetTime
      then mostRecentModTimeRef := mtime
      else ()
  end

fun mostRecentModTime () =
  if Time.compare (!mostRecentModTimeRef, Time.zeroTime) = EQUAL
  then Globals.getResetTime ()
  else !mostRecentModTimeRef

fun txtOpenIn fname =
  let
      val inf = TextIO.openIn fname
      val () = checkFileModTime fname
  in
      inf
  end

fun binOpenIn fname =
  let
      val inf = BinIO.openIn fname
      val () = checkFileModTime fname
  in
      inf
  end

end

Use 1s precision when comparing file mtimes
Use 1s precision when comparing file mtimes

HTTP Last-Modified caching depends on mtimes of files read during
compilation. Only mtimes before the compiler's start (reset) time are
considered. On filesystems with lower mtime precision than Standard
ML's Time.now, a temporary file generated by the compiler may have an
mtime preceding the reset time even though it was modified after. The
fix here is to compare times using 1s precision, the most granular
used by filesystems.
