bash 如何在bash下从unix时间戳设置日期/时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15021668/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:38:22  来源:igfitidea点击:

How to set date/time from unix timestamp under bash

bashdatetimeunix-timestampbusybox

提问by luzik

How to set date/time from unix timestamp under bash

如何在bash下从unix时间戳设置日期/时间

root@M501 />date
Thu Jan  1 00:10:49 UTC 1970
root@M501 />date +%s
652
root@M501 />date +%s -s "`date +%s`"
date: invalid date `662'

as You can see date +%s -s "2323123" do not work :/

因为你可以看到 date +%s -s "2323123" 不起作用:/

[SOLVED] ..under bash i can use

[已解决] ..在 bash 下我可以使用

date +%s -s "@`date +%s`"

or

或者

date -s @1361529589

Thanks!

谢谢!

Question #2 How to achieve this under busybox?

问题#2 如何在busybox 下实现这一点?

root@M501 />date -s @1361529589
date: invalid date `@1361529589'

maybe there is way like

也许有这样的方式

echo '1361529589' > /dev/unix_time_stamp_or_whatever ? :)

回答by dogbane

You need to prefix the number with the @symbol so that the datecommand knows that it represents the number of seconds since the Epoch. Try this:

您需要在数字前加上@符号,以便date命令知道它代表自 Epoch 以来的秒数。尝试这个:

date +%s -s @`date +%s`

回答by Amin Mozafari

Use something like this:

使用这样的东西:

date -s @435456646