This repository has been archived by the owner. 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

Comments

Projects
None yet
4 participants
@nakedible

nakedible commented May 18, 2012

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

This comment has been minimized.

tjfontaine commented Mar 11, 2013

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

This comment has been minimized.

Member

bnoordhuis commented Mar 11, 2013

@tjfontaine

This comment has been minimized.

tjfontaine commented Apr 1, 2013

Landed in 51f128d

@tjfontaine tjfontaine closed this Apr 1, 2013

@bpasero

This comment has been minimized.

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

This comment has been minimized.

Member

bnoordhuis commented Oct 26, 2016

@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

This comment has been minimized.

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

This comment has been minimized.

Member

bnoordhuis commented Oct 26, 2016

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.