date --set="2011-12-07 01:20:15.962" && date --rfc-3339=ns
我可以用毫秒来得到date,如下所示:
date +"%S.%N" date --rfc-3339=ns
根据man date我希望能够这样设置它:
date --set=%s.%N +`date +"%S.%N"` date --set="%s.%N" "+1323217126.085882000"
根据我的谷歌search,我希望能够像这样设置date(与上面相反):
date +%s.%N -s `date +"%S.%N"` date +"%s.%N" -s "1323217126.085882000"
既没有工作。 有人可以提出这个问题吗?
PS不,我不需要纳秒级的分辨率。 是的,我知道bash执行需要几毫秒。 我真正需要的是第二分辨率,十分之一秒就足够了。
这是一个解决scheme( Linux,不是Unix ):
date --set="2011-12-07 01:20:15.962" && date --rfc-3339=ns
请注意延迟:
CURTIME=`date --rfc-3339=ns` date --set="${CURTIME}" NEWTIME=`date --rfc-3339=ns` echo ${CURTIME} echo ${NEWTIME} 2011-12-07 01:48:54.687216122+00:00 2011-12-07 01:48:54.720541318+00:00
你会注意到, 整个毫秒的延迟被引入。 这是由于初始化内存和加载date二进制文件所花费的date 。 对于所有shell和插入更高级别语言的exec都是如此
但是,如果您只需要在十分之一秒的范围内进行亚秒级分辨率,那么在很多情况下就足够了。
如果你的目标是设置时间间隔亚秒,那么如果我正确地阅读源,date似乎是错误的命令。
假设你使用的是date的coreutils版本,那么在我看来when.tv_nsec = 0行是将timepec结构的whenvariables的纳秒部分设置为零。 即使你能说服date接受一个更准确的价值,它在我看来是没有意义的。
# time.h struct timespec { __time_t tv_sec; /* Seconds. */ long int tv_nsec; /* Nanoseconds. */ }; # date.c struct timespec when; # ... valid_date = posixtime (&when.tv_sec, datestr, (PDS_TRAILING_YEAR | PDS_CENTURY | PDS_SECONDS)); when.tv_nsec = 0; /* FIXME: posixtime should set this. */ # ... /* Set the system clock to the specified date, then regardless of the success of that operation, format and print that date. */ if (settime (&when) != 0) { error (0, errno, _("cannot set date")); ok = false; }