在使用 linux 命令“date”时添加一些特定时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16142973/
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
Add some specific time while using the linux command "date"
提问by waitingkuo
In linux, date
can help me to print the current time. If want to print the current time + 1 hour, what option should I give?
在linux下,date
可以帮我打印当前时间。如果要打印当前时间 + 1 小时,我应该给出什么选项?
采纳答案by fedorqui 'SO stop harming'
On Linux
在 Linux 上
Just use -d
(or --date
) to do some math with the dates:
只需使用-d
(或--date
) 对日期进行一些数学运算:
date -d '+1 hour' '+%F %T'
# ^^^^^^^^^^^^
For example:
例如:
$ date '+%F %T'
2013-04-22 10:57:24
$ date -d '+1 hour' '+%F %T'
2013-04-22 11:57:24
# ^
On Mac OS
在 Mac 操作系统上
Warning, the above only works on Linux, not on Mac OS.
警告,以上仅适用于 Linux,不适用于 Mac OS。
On Mac OS, the equivalent command is
在 Mac OS 上,等效的命令是
date -v+1H
回答by Glitch Desire
TZ=Asia/Calcutta date
should work (obviously this will get you the time in Calcutta).
TZ=Asia/Calcutta date
应该工作(显然这会让你有时间在加尔各答)。
This changes your TimeZone (TZ
) environment variable for this operation only, it won't permanently set you to the new time zone you specify.
这TZ
仅会为此操作更改您的 TimeZone ( ) 环境变量,它不会将您永久设置为您指定的新时区。
回答by Sir l33tname
回答by Garima Nathani
We can get it using below code, i was handling a case wherein i required to traverse dates from last 3 months, i used below code to traverse the same
我们可以使用下面的代码来获取它,我正在处理一个案例,其中我需要遍历过去 3 个月的日期,我使用下面的代码来遍历相同的
!/bin/sh
!/bin/sh
for i in {90..1}
do
st_dt=$(date +%F' 00:00:00' -d "-$i days")
echo $st_dt
j=$((i-1))
end_dt=$(date +%F' 00:00:00' -d "-$j days")
echo $end_dt
done
回答by Sakthivel
In shell script, if we need to add time then use below command and date format(PUT TIME before DATE string)
在shell脚本中,如果我们需要添加时间,则使用下面的命令和日期格式(在日期字符串之前放置时间)
date -d"11:15:10 2017-02-05 +2 hours" +"%Y-%m-%d %H:%M:%S"
this will output
2017-02-05 13:15:10
date -d"11:15:10 2017-02-05 +2 hours" +"%Y-%m-%d %H:%M:%S"
这将输出
2017-02-05 13:15:10
THis does not result in correct date without UTC it does not work
这不会导致没有UTC的正确日期它不起作用
回答by Bruno Bronosky
Linux and macOS in a single command
单个命令中的 Linux 和 macOS
If you need to write scripts that work on both Linux servers and macOS workstations, you can silence the error of the first date
call and 'OR' it (||
) with the other. It doesn't matter which comes first.
如果您需要编写可在 Linux 服务器和 macOS 工作站上运行的脚本,您可以消除第一次date
调用的错误并将其 ( ||
) 与另一个调用“或” 。哪个先到并不重要。
date -u -d "+${max_age}Seconds" +"%Y-%m-%dT%H:%M:%SZ" 2>/dev/null || \
date -u -v "+${max_age}S" +"%Y-%m-%dT%H:%M:%SZ"
For example, this bash function uploads a file to AWS S3 and sets a Expires:
and Cache-Control:
headers.
例如,此 bash 函数将文件上传到 AWS S3 并设置 aExpires:
和Cache-Control:
标头。
s3_upload_cache_control(){
local max_age_seconds="" ;shift # required
local bucket_path="" ;shift # required
local filename="" ;shift # required
local remote_filename="/${1:-${filename}}" # optional
local fmt="+%Y-%m-%dT%H:%M:%SZ"
aws s3 cp \
"${filename}" \
"s3://${bucket_path}${remote_filename}" \
--expires "$( \
date -u -d "+${max_age_seconds}Seconds" $fmt 2>/dev/null || \
date -u -v "+${max_age_seconds}S" $fmt )" \
--cache-control max-age=$max_age,public \
--acl public-read
}