nodejs fs.utimes使用什么格式 [英] What format is used for nodejs fs.utimes
问题描述
在nodejs中, fs.utimes
的参数应以什么格式编写,例如 atime
, mtime
。
In nodejs, the arguments of fs.utimes
should be written in what format,e.g.atime
,mtime
.
API: fs.utimes(路径,atime,mtime,回调)
推荐答案
这些参数是JavaScript 日期
s,不是字符串。
Those parameters are JavaScript Date
s, not strings.
请注意,atime,mtime和ctime是Date对象的实例,为了比较这些对象的值,您应该使用适当的方法。对于大多数常规用途,getTime()将返回自1970年1月1日00:00:00 UTC以来经过的毫秒数,并且此整数应足以进行任何比较,但是还有其他方法可用于显示模糊信息。更多细节可以在MDN JavaScript Reference页面找到。
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.
并且来自源代码:
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);
}
这表明它可以是Javascript Date或Unix Style数字日期。
Which shows that it can be a Javascript Date or Unix Style numeric date.
这一行非常重要! return time.getTime()/ 1000;
这意味着如果传入一个数字,则传入一个Unix样式号,其中毫秒数以1/1000表示,这是不同的比从 Date.getTime()返回的整数
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()
这篇关于nodejs fs.utimes使用什么格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!