Add milliseconds to timestamp (bash, unix)

霸气de小男生 提交于 2019-12-13 14:16:14

问题


EDIT: This is apparently not possible, see Barmars answer below.

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. 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

回答1:


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.

Furthermore, the input to strftime is a 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.




回答2:


%N gives you nanoseconds and you could divide that by 1000000




回答3:


In general you can't, but I thought I'd mention that in ts, you can use "%.S" for millisecond precision. Just in case you were using date to timestamp output lines.



来源:https://stackoverflow.com/questions/17307086/add-milliseconds-to-timestamp-bash-unix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!