Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

fs.stat calls should return sub-second precision timestamps #3284

Closed
nakedible opened this issue May 18, 2012 · 7 comments
Closed

fs.stat calls should return sub-second precision timestamps #3284

nakedible opened this issue May 18, 2012 · 7 comments
Labels

Comments

@nakedible
Copy link

Currently, fs.stat returns timestamps with only seconds, the milliseconds being always zero. However, many filesystems record and expose sub-second timestamps. It would be nice if nodejs exposed these, atleast up to the millisecond resolution.

Concretely, the stats object is filled by:

stats->Set(mtime_symbol, NODE_UNIXTIME_V8(s->st_mtime));

The nanosecond timestamps should be accessible as s->st_mtimensec, or possibly s->st_mtime.nsec. There are probably some platform differences.

@tjfontaine
Copy link

Linux fstat manpage suggests that this will be enabled with _XOPEN_SOURCE=700 or _POSIX_C_SOURCE= 200809

It seems like we could support this on the platforms that have access to it. This will likely need a tracking issue in joyent/libuv as well

@bnoordhuis
Copy link
Member

For reference: joyent/libuv@5ff2b61 and joyent/libuv@7ca524e.

@tjfontaine
Copy link

Landed in 51f128d

@bpasero
Copy link

bpasero commented Oct 26, 2016

@bnoordhuis is there another issue with this? On Mac (El Capitan) using node.js 6.5.0 I always get 1s resolution for stats.mtime:

image

@bnoordhuis
Copy link
Member

@bpasero OS X generally seems to round or truncate to the nearest second. What does stat -x or stat -f %.2m print for that file?

@bpasero
Copy link

bpasero commented Oct 26, 2016

@bnoordhuis

~/Desktop> stat -x untitled\ folder/
  File: "untitled folder/"
  Size: 68           FileType: Directory
  Mode: (0755/drwxr-xr-x)         Uid: (  501/ bpasero)  Gid: (   20/   staff)
Device: 1,4   Inode: 35462942    Links: 2
Access: Wed Oct 26 13:25:55 2016
Modify: Wed Oct 26 13:25:55 2016
Change: Wed Oct 26 13:25:55 2016
~/Desktop> stat -f %.2m untitled\ folder/
1477481155
~/Desktop> 

@bnoordhuis
Copy link
Member

There you go. You can double-check with the program below but it will probably only confirm what stat(1) says.

#include <stdio.h>
#include <sys/stat.h>

int main(int argc, char **argv) {
  struct stat s;
  int i;

  for (i = 1; i < argc; i++)
    if (stat(argv[i], &s))
      perror(argv[i]);
    else
      printf("%s %lu.%lu %lu.%lu %lu.%lu\n",
          argv[i],
          s.st_atimespec.tv_sec, s.st_atimespec.tv_nsec,
          s.st_ctimespec.tv_sec, s.st_ctimespec.tv_nsec,
          s.st_mtimespec.tv_sec, s.st_mtimespec.tv_nsec);

  return 0;
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants