我如何在 bash 脚本中将 linux“date +%s”输出转换为更易读的格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4315382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can i convert linux "date +%s" output to a more readable format in bash script
提问by kamal
Here is the script:
这是脚本:
#!/bin/bash
i="0"
startTime=`date -u +%s`
startTime=$[$startTime++5]
echo ""
echo "##################"
echo "LAUNCHING REQUESTS"
echo " COUNT: "
echo " DELAY: 1 "
echo " EXECUTION: $startTime "
echo "##################"
echo ""
while [ -gt "$i" ]
do
i=$[$i+1]
php avtestTimed.php $startTime &
echo "QUEUEING REQUEST $i"
sleep 1
done
so i want to convert $startTime into a UTC format
所以我想将 $startTime 转换为 UTC 格式
回答by mishunika
Try:
尝试:
date -d @$(($startTime--5))
if you want to decode the first state of $startTime variable, or this:
如果你想解码 $startTime 变量的第一个状态,或者这个:
date -d @$startTime
and, you can add -uargument to get the UTC time... %)
并且,您可以添加-u参数以获取 UTC 时间... %)
read the man date
阅读 man date
回答by JJ.
What format are you looking for? Simply executing date with -u Will give you a readable output format of UTC time, ie:
你在找什么格式?简单地使用 -u 执行 date 将为您提供 UTC 时间的可读输出格式,即:
# date -u
Tue Nov 30 15:35:12 UTC 2010
回答by Benjamin Bannier
You can use -dto pass $startTimeback into datefor processing, just prefix it with @so it is recognized as seconds since the epoch.
您可以使用-d传递$startTime回date进行处理,只需在它前面加上前缀,@以便将其识别为自纪元以来的秒数。
$ date -d @$startTime
Once you have that down you can change the output format. I would suggest looking at the man pageor the info documentation of datefor that. For UTC output you would use
完成后,您可以更改输出格式。我建议查看手册页或信息文档date。对于 UTC 输出,您将使用
$ date -d @$startTime -u
回答by Marcin
I'd have another variable
我会有另一个变量
startTimeHuman=$(date -u -R)
startTimeHuman=$(date -u -R)
right next to where you set startTime. This way you'd have both variants, one for doing math, and one for humans to understand.
就在您设置 startTime 的位置旁边。这样你就会有两种变体,一种用于计算,一种用于人类理解。
回答by sorpigal
Got perl?
有perl吗?
human_time=$(perl -e 'print scalar gmtime(shift)' $startTime)

