Domanda

Ocaml's Unix module provides a stat function, which returns a record with a float st_mtime member.

This seems to always be second-level precision, even on Linux, which supports sub-second precision. The fact that it's a float and not an int gives me hope that it's possible to get a subsecond-precision time out of it (or something like it), but I don't know how.

I'm happy to use additional OPAM libraries. I'm using Batteries already, so I wouldn't want to also have to use e.g Jane Street Core - but smaller libraries are fine.

This is the script I used to test:

#!/usr/bin/env ocamlscript
Ocaml.packs := ["unix"]
--
open Unix
open Printf

let () =
    let this_file = Array.get Sys.argv 0 in
    let stats = Unix.stat this_file in
    printf "mtime: %f" stats.st_mtime

when run on my Linux (x86_64) machine, it prints:

mtime: 1388567583.000000

Python has no trouble getting a sub-second mtime, so my OS and FS definitely support it.

È stato utile?

Soluzione

The implementation of Unix.stat can be seen at the Github mirror of Inria's codebase. As you can see the portable second-precision time values are converted directly into doubles, ignoring the newer nanosecond precision values available in the struct stat.

Please consider submitting a bug report so that this can be improved.

In the meantime if you desperately need this functionality and simply can't wait, you could write your own improved stat_ext that returns sub-second time stamps. Of course it is best to avoid reimplementing the stdlib unless there is great need.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top