Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a file system clock API (or platform specific clock API) #56448

Open
myocytebd opened this issue Jan 3, 2025 · 2 comments
Open

Provide a file system clock API (or platform specific clock API) #56448

myocytebd opened this issue Jan 3, 2025 · 2 comments
Labels
feature request Issues that request new features to be added to Node.js.

Comments

@myocytebd
Copy link

What is the problem this feature will solve?

Sometimes comparing file system timestamp with "current time" is necessary. An example would be doing something like make.
To make it reliable, this ""current time" should be get from a clock source that is compatible with file system.

Currently, node doesn't seem to have such API.

  • process.hrtime() is a monotonic clock, which can drift from real time clock. (so it should be used even if the origin is synced with utc)
  • Date.now() goes through a series of indrects and ends up with v8/src/base/platform/time.cc: Time::Now(),
    which becomes gettimeofday(timezone=null) on linux, and an interpolated value between (rebased) QueryPerformanceCounter() and GetSystemTimeAsFileTime() on windows.

What is the feature you are proposing to solve the problem?

Add an API: fs.hrtime(), which uses:

  • clock_gettime(CLOCK_REALTIME_COARSE) on linux.
  • Use accurate file system clock if possible, on other platforms.
  • Fallback to libuv uv_clock_gettime(UV_CLOCK_REALTIME) otherwise,
    which ends up with clock_gettime(CLOCK_REALTIME) on posix, and GetSystemTimePreciseAsFileTime() on windows.

What alternatives have you considered?

Add a explicit clock API, maybe under process, which doesn't imply that it is a file system clock.
Expose the relevant platform specific clock sources under it:

  • Posix clock_gettime(CLOCK_REALTIME)
  • Linux clock_gettime(CLOCK_REALTIME_COARSE)
  • Windows GetSystemTimePreciseAsFileTime()
@myocytebd myocytebd added the feature request Issues that request new features to be added to Node.js. label Jan 3, 2025
@github-project-automation github-project-automation bot moved this to Awaiting Triage in Node.js feature requests Jan 3, 2025
@targos
Copy link
Member

targos commented Jan 3, 2025

@nodejs/libuv

@saghul
Copy link
Member

saghul commented Jan 3, 2025

Doesn't fs.stat return Date objects for access and modification time? Then you could compare those, can't you?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Issues that request new features to be added to Node.js.
Projects
Status: Awaiting Triage
Development

No branches or pull requests

3 participants