Linux 在solaris中获取昨天的日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11855008/
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
Get Yesterday's date in solaris
提问by AKIWEB
I am running SunOS.
我正在运行 SunOS。
bash-3.00$ uname -a
SunOS lvsaishdc3in0001 5.10 Generic_142901-02 i86pc i386 i86pc
I need to find Yesterday's date in linux
with the proper formatting passed from command prompt. When I tried like this on my shell prompt-
我需要找到Yesterday's date in linux
从命令提示符传递的正确格式。当我在 shell 提示下尝试这样时-
bash-3.00$ date --date='yesterday' '+%Y%m%d'
date: illegal option -- date=yesterday
usage: date [-u] mmddHHMM[[cc]yy][.SS]
date [-u] [+format]
date -a [-]sss[.fff]
I always get date illegal option
, why is it so?
Is there anything wrong I am doing?
我总是约会illegal option
,为什么会这样?我做错了什么吗?
Update:-
更新:-
bash-3.00$ date --version
date: illegal option -- version
usage: date [-u] mmddHHMM[[cc]yy][.SS]
date [-u] [+format]
date -a [-]sss[.fff]
采纳答案by arsenal
Try this below thing. It should work
试试这个下面的东西。它应该工作
YESTERDAY=`TZ=GMT+24 date +%Y%m%d`; echo $YESTERDAY
回答by George Karanikas
Try this one out:
试试这个:
DATE_STAMP=`TZ=GMT+24 date +%Y%m%d`
where GMT is the time zone and you might need to alter the 24 according to the hours difference you have from GMT. Either that or you can change GMT to a time zone more comfortable to you e.g. CST
其中 GMT 是时区,您可能需要根据与 GMT 的时差来更改 24。或者您可以将 GMT 更改为对您来说更舒适的时区,例如 CST
回答by amdn
As larsks suggested, you can use perl:
正如 larsks 建议的那样,您可以使用 perl:
perl -e 'use POSIX qw(strftime); print strftime "%a %b %e %H:%M:%S %Y",localtime(time()- 3600*24);'
Slightly modified from
稍微修改自
http://blog.rootshell.be/2006/05/04/solaris-yesterday-date/
http://blog.rootshell.be/2006/05/04/solaris-yesterday-date/
To get YYYYMMDD format use this
要获得 YYYYMMDD 格式,请使用此
perl -e 'use POSIX qw(strftime); print strftime "%Y%m%d",localtime(time()- 3600*24);'
This link explains how to format date and time with strftime
此链接解释了如何使用 strftime 格式化日期和时间
回答by olivecoder
A pure bash solution
一个纯粹的 bash 解决方案
#!/bin/bash
# get and split date
today=`date +%Y%m%d`
year=${today:0:4}
month=${today:4:2}
day=${today:6:2}
# avoid octal mismatch
if (( ${day:0:1} == 0 )); then day=${day:1:1}; fi
if (( ${month:0:1} == 0 )); then month=${month:1:1}; fi
# calc
day=$((day-1))
if ((day==0)); then
month=$((month-1))
if ((month==0)); then
year=$((year-1))
month=12
fi
last_day_of_month=$((((62648012>>month*2&3)+28)+(month==2 && y%4==0)))
day=$last_day_of_month
fi
# format result
if ((day<10)); then day="0"$day; fi
if ((month<10)); then month="0"$month; fi
yesterday="$year$month$day"
echo $yesterday
回答by user2622547
Playing on Solaris10 with non-GMT environment, I'm getting this:
在具有非 GMT 环境的 Solaris10 上玩游戏,我得到了这个:
# date
Fri Jul 26 13:09:38 CEST 2013 (OK)
# (TZ=CEST+24 date)
Thu Jul 25 11:09:38 CEST 2013 (ERR)
# (TZ=GMT+24 date)
Thu Jul 25 11:09:38 GMT 2013 (OK)
# (TZ=CEST+$((24-$((`date "+%H"`-`date -u "+%H"`)))) date)
Thu Jul 25 13:09:38 CEST 2013 (OK)
As You colud see, I have and I want to get CEST , but TZ=CEST+24 giving me wrong CEST data; GMT+24 giving me correct data, but unusable.
正如你所看到的,我已经并且我想得到 CEST ,但是 TZ=CEST+24 给了我错误的 CEST 数据;GMT+24 给我正确的数据,但无法使用。
To get the proper result, I has to use GMT+22 (wrong command, correct result) or CEST+22 (wrong value, but finnaly correct result for correct TZ)
为了获得正确的结果,我必须使用 GMT+22(错误的命令,正确的结果)或 CEST+22(错误的值,但最终正确的 TZ 的正确结果)
回答by Arun
TZ=$TZ+24 date +'%Y/%m/%d'
in SunOS.
TZ=$TZ+24 date +'%Y/%m/%d'
在 SunOS 中。
回答by Arun
A pure bash solution given by @olivecoderis very reliable compared to any other solution but there is a mistake to be corrected. when the day fall on 1st of the month the script is failing with date saying "last_day_of_month
" in day value. @olivecoderhas missed $in
day=last_day_of_month
, that it should be
与任何其他解决方案相比,@olivecoder给出的纯 bash 解决方案非常可靠,但有一个错误需要纠正。当一天落在一个月的 1 号时,脚本失败,日期last_day_of_month
在日值中显示为“ ”。@olivecoder错过了$in
day=last_day_of_month
,应该是
day=$last_day_of_month;
After this correction it works very good.
经过这次修正后,效果非常好。
Using Timezone -24is having some issue based on time when use it. in some cases it goes to day before yesterday. So I think its not reliable.
使用Timezone -24 时会遇到一些基于时间的问题。在某些情况下,它会持续到前天。所以我认为它不可靠。