Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What format is used for nodejs fs.utimes

In nodejs, the arguments of fs.utimes should be written in what format,e.g.atime,mtime.

API: fs.utimes(path, atime, mtime, callback)

like image 356
Yofine Avatar asked Jan 02 '14 04:01

Yofine


1 Answers

Those parameters are JavaScript Dates, not strings.

From the docs:

Please note that atime, mtime and ctime are instances of Date object and to compare the values of these objects you should use appropriate methods. For most general uses getTime() will return the number of milliseconds elapsed since 1 January 1970 00:00:00 UTC and this integer should be sufficient for any comparison, however there additional methods which can be used for displaying fuzzy information. More details can be found in the MDN JavaScript Reference page.

And from the source code:

fs.utimes = function(path, atime, mtime, callback) {
  callback = makeCallback(callback);
  if (!nullCheck(path, callback)) return;
  binding.utimes(pathModule._makeLong(path),
                 toUnixTimestamp(atime),
                 toUnixTimestamp(mtime),
                 callback);
};


// converts Date or number to a fractional UNIX timestamp
function toUnixTimestamp(time) {
  if (util.isNumber(time)) {
    return time;
  }
  if (util.isDate(time)) {
    // convert to 123.456 UNIX timestamp
    return time.getTime() / 1000;
  }
  throw new Error('Cannot parse time: ' + time);
}

Which shows that it can be a Javascript Date or Unix Style numeric date.

This line is REALLY important!!! return time.getTime() / 1000; It means that if you pass in a number you pass in a Unix style number where the milliseconds are represented in 1/1000s which is different than the integer returned from Date.getTime()

See this link on Unix Timestamps

like image 74
Nathaniel Johnson Avatar answered Oct 02 '22 02:10

Nathaniel Johnson