Get Formatted Date From Timestamp With Rounded Milliseconds Bash Shell Script

故事扮演 提交于 2019-12-31 12:02:34

问题


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"`

回答1:


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



回答2:


Million ways to do this.. one simple way is to remove the trailing numbers...

date +'%Y-%m-%d %H:%M:%S.%N' | sed 's/[0-9][0-9][0-9][0-9][0-9][0-9]$//g'

or

date +'%Y-%m-%d %H:%M:%S.%N' | sed 's/......$//g'



回答3:


I can't help offering a "cheap" solution...

echo $(date +"%Y-%m-%d-%H:%M:%S.")$((RANDOM%1000))

After all, if accuracy is less important than uniqueness, as it is in my case... here you have it.




回答4:


Best way would be shelling out a python command.

$ python -c "from datetime import datetime; print datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]"
2017-02-15 15:03:03.496

You can then create an alias in you ~/.profile

$ alias datetime='python -c python -c "from datetime import datetime;...'

Or even create a shell function

datetime(){
  python -c python -c "from datetime import datetime;...
}



回答5:


One problem: %s is not the date format for milliseconds, it is seconds of the current epoch. %N gives nanoseconds, I do not know of a specified date format for GNU date that gives milliseconds.

Try date --help to see all of the format options.




回答6:


Can't think of a way to do it without a couple of steps, but this will get you there:

d=$(date +'%Y-%m-%d %H:%M:%S|%N')
ms=$(( ${d#*|}/1000000 ))
d="${d%|*}.$ms"
echo $d

2013-04-16 08:51:48.874

Since all the components are taken from a single call to date they'll be consistent.




回答7:


str="2013-04-05 13:33:53.180"

dateStr1=`date -d "$str" +'%Y-%m-%d %H:%M:%S.%N'`;

echo $dateStr1

dateStr2=`date -d "$dateStr1" +'%s'`

echo $dateStr2

dateStr3="$dateStr2"`echo ${dateStr1:20:3}`

echo $dateStr3

============================

[Output]
2013-04-05 13:33:53.180000000
1365140033
1365140033180

(above consider the datestr in UTC)




回答8:


Use printf to do the actual formatting; use date to produce the set of arguments printf needs to fill the format string.

printf '%04d-%02d-%02dT%02d:%02d:%02d.%03d' \
        $(date -r "${timestamp%.*}" +"%Y %m %d %H %M %S")\
        $(( ${timestamp#*.} / 1000 ))

In the above, I assume your timestamp looks something like "blahblahblah.728239". The result is a set of arguments for printf like 2013 04 16 11 03 17 728 to produce 2013-04-16T11:03:17.728.



来源:https://stackoverflow.com/questions/16036763/get-formatted-date-from-timestamp-with-rounded-milliseconds-bash-shell-script

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