获取格式化的日期从TIMESTAMP WITH圆角毫秒的Bash shell脚本 [英] Get Formatted Date From Timestamp With Rounded Milliseconds Bash Shell Script
问题描述
我需要在一个特定的格式,以得到一个日期,但无法工作,如何做到这一点。
下面是我应得的日期的时刻。
日期-r$时间戳+%Y-%M-%胸苷%H:%M:%S,S'
然而,问题是毫秒有我需要的格式位数太多。我需要毫秒被限定于3位数。
任何想法,我可以做这样的事情?
当前解决方法
不准确,但它的工作原理。后来我计算毫秒,然后只取字符串的前三个字符。显然,这并不顾及一轮上涨。
date_string_one =`日期-r$时间戳+%Y-%M-%胸苷%H:%M:%S.'`
date_string_milli =`日期-r$时间戳+%s'`
DATE_STRING =$ date_string_one`printf的%.3s$ date_string_milli`
您可以简单地使用%3N
来截断纳秒到3个最显著位数:
$日期+%Y-%M-%D%H:%M:%S,%3N
少儿模特16:00:12746
或
$日期+%F%T,%3N
少儿模特16:00:12746
testet与»GNU bash的版本4.2.25(1) - 释放(i686的-PC-Linux的GNU)«
但要知道,这%N可以不依赖你的目标系统或bash版本上实现。
测试的嵌入式系统»GNU bash的版本4.2.37(2) - 释放(ARM-buildroot的-Linux的gnueabi)上«没有%N
:
日期+%F%T,%N
少儿模特16:44:47,%N
I need to get a date in a specific format but can't work out how to do it.
Here is how I'm getting the date at the moment.
date -r "$timestamp" +'%Y-%m-%dT%H:%M:%S.s'
However the issue is the milliseconds has too many digits for the format I need. I need the milliseconds to be limited to 3 digits.
Any idea how I can do such a thing?
Current Workaround
Not accurate but it works. I calculate the milliseconds afterwards and then just take the first three characters of the string. Obviously this doesn't take into account round up.
date_string_one=`date -r "$timestamp" +'%Y-%m-%dT%H:%M:%S.'`
date_string_milli=`date -r "$timestamp" +'%s'`
date_string="$date_string_one"`printf "%.3s" "$date_string_milli"`
You may simply use %3N
to truncate the nanoseconds to the 3 most significant digits:
$ date +"%Y-%m-%d %H:%M:%S,%3N"
2014-01-08 16:00:12,746
or
$ date +"%F %T,%3N"
2014-01-08 16:00:12,746
testet with »GNU bash, Version 4.2.25(1)-release (i686-pc-linux-gnu)«
But be aware, that %N may not implemented depending on your target system or bash version.
Tested on an embedded system »GNU bash, version 4.2.37(2)-release (arm-buildroot-linux-gnueabi)« there was no %N
:
date +"%F %T,%N"
2014-01-08 16:44:47,%N
这篇关于获取格式化的日期从TIMESTAMP WITH圆角毫秒的Bash shell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!