简体   繁体   English

将毫秒添加到时间戳(bash、unix)

[英]Add milliseconds to timestamp (bash, unix)

EDIT: This is apparently not possible, see Barmars answer below.编辑:这显然是不可能的,请参阅下面的 Barmars 回答。

I use the command below a lot to create a timestamp for archiving purposes.我经常使用下面的命令来创建用于存档目的的时间戳。 I'd like to add milliseconds to the string.我想在字符串中添加毫秒。 Is that possible?那可能吗? I use this script on both osx and linux, so a solution that works on both would be nice.我在 osx 和 linux 上都使用这个脚本,所以一个适用于两者的解决方案会很好。 Are seconds ever rounded up or down with or without milliseconds?秒是否有或没有毫秒向上或向下舍入? (Are minutes rounded up or down?) (分钟是向上还是向下四舍五入?)

date -n +%Y%m%d%H%M%S

I don't think there's a way to do this portably.我不认为有一种方法可以便携地做到这一点。 See the POSIX strftime specification , it doesn't mention anything about milliseconds or nanoseconds.请参阅POSIX strftime 规范,它没有提到有关毫秒或纳秒的任何内容。

Furthermore, the input to strftime is a struct tm .此外, strftime的输入是一个struct tm The specification of this structure is here and it doesn't include anything more precise than seconds.这个结构的规范在这里,它不包括比秒更精确的东西。 Nanoseconds are in the timespec structure, but this is mostly used for time periods, not clock time, and there's no requirement for the system to maintain the clock time to more than second resolution.纳秒在timespec结构中,但这主要用于时间段,而不是时钟时间,并且不需要系统将时钟时间保持在超过秒的分辨率。

In general you can't, but I thought I'd mention that in ts , you can use "%.S" for millisecond precision.一般来说你不能,但我想我会提到在ts ,你可以使用“%.S”来表示毫秒精度。 Just in case you were using date to timestamp output lines.以防万一您使用date为输出行添加时间戳。

%N 为您提供纳秒,您可以将其除以 1000000

您可以尝试%3N ,它适用于 Windows 和 Debian 9 的 git bash。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM