Chinaunix

标题: shell 如何得到前一天的日期 [打印本页]

作者: wazhl    时间: 2010-01-12 10:01
标题: shell 如何得到前一天的日期
网上虽然有很多这种方法.

但都不符合我的要求.

我需要的是在unix下运行的shell, 格式需要是 yyyymmdd

如 20100112 的前一天是 20100111 ..
作者: wazhl    时间: 2010-01-12 10:08
这里有人在吗?
作者: LonnyHe    时间: 2010-01-12 10:09
linux 环境如下:

date "+%Y%m%d" -d "yesterday"
date "+%Y%m%d" -d "last-day"
date "+%Y%m%d" -d "1 day ago"
date "+%Y%m%d" -d "-1 day"

[ 本帖最后由 LonnyHe 于 2010-1-12 10:12 编辑 ]
作者: Shell_HAT    时间: 2010-01-12 10:11
What kind of Unix?
作者: ubuntuer    时间: 2010-01-12 10:47
原帖由 LonnyHe 于 2010-1-12 10:09 发表
linux 环境如下:

date "+%Y%m%d" -d "yesterday"
date "+%Y%m%d" -d "last-day"
date "+%Y%m%d" -d "1 day ago"
date "+%Y%m%d" -d "-1 day"

不错
作者: wazhl    时间: 2010-01-12 11:36
hp_ux  

不支持 date "+%Y%m%d" -d "yesterday" 这些格式的.
作者: nhw_cs    时间: 2010-01-12 12:21
原帖由 wazhl 于 2010-1-12 11:36 发表
hp_ux  

不支持 date "+%Y%m%d" -d "yesterday" 这些格式的.



set TZ envroment variable
作者: wazhl    时间: 2010-01-12 13:58
原帖由 nhw_cs 于 2010-1-12 12:21 发表



set TZ envroment variable



如何在shell里实现呢?
作者: nhw_cs    时间: 2010-01-12 14:09
An example here:

$ echo $TZ
UTC0
$ date
Tue Jan 12 06:07:16 UTC 2010
$ TZ=UTC24
$ date
Mon Jan 11 06:07:33 UTC 2010


Do you get the point?
作者: Shell_HAT    时间: 2010-01-12 14:17

$ date
Tue Jan 12 14:18:00 EAT 2010

$ YESTERDAY=`TZ=aaa24 date +%Y%m%d`

$ echo $YESTERDAY
20100111

$ uname
HP-UX

作者: wazhl    时间: 2010-01-12 14:21
原帖由 Shell_HAT 于 2010-1-12 14:17 发表

$ date
Tue Jan 12 14:18:00 EAT 2010

$ YESTERDAY=`TZ=aaa24 date +%Y%m%d`

$ echo $YESTERDAY
20100111

$ uname
HP-UX


响...这样不会影响系统的其他进程吧?这个方法看起来很不错哎.

TZ=aaa24都是相差24小时的意思吗?
作者: Shell_HAT    时间: 2010-01-12 14:58
标题: 回复 #11 wazhl 的帖子
参考:
http://archives.devshed.com/foru ... -y-m-d-1795682.html

as long as you use the format TZ=<string><offset>

<stringwill just be some descriptive text that you chose to
describe that rule. As long as you specify an offset (it could
be even more complicated like having two offsets for day light
saving time, and rules to tell when to switch to DST), then the
<stringis not used to define the rules (aaa will not refer to
any description file in /usr/share/zoneinfo contrary to another
contributor said), it will just be used in date +%Z.

So, TZ=aaa24 is a tab zone called "aaa" that is defined as being
offset by 24 wrt Universal time (GMT, UTC).

If you had specified:

TZ=aaa

so, without any rule embeded in $TZ, then "aaa" would have
refered to a system wide timezone, whose rules are defined in a
system wide description file (on some systems in
/usr/share/zoneinfo).

$ TZ=aaa truss -t open date
open("/var/ld/ld.config", RDNLY) Err#2 ENENT
open("/usr/lib/libc.so.1", RDNLY) = 3
open("/usr/lib/libdl.so.1", RDNLY) = 3
open("/usr/platform/SUNW,Ultra-5_10/lib/libc_psr.so.1", RDNLY) = 3
open("/usr/share/lib/zoneinfo/aaa", RDNLY) Err#2 ENENT
open("/usr/share/lib/zoneinfo/aaa", RDNLY) Err#2 ENENT
open("/usr/share/lib/zoneinfo/aaa", RDNLY) Err#2 ENENT

My system doesn't have such system-wide timezone.

$ TZ=Brazil/Acre truss -t open date
open("/var/ld/ld.config", RDNLY) Err#2 ENENT
open("/usr/lib/libc.so.1", RDNLY) = 3
open("/usr/lib/libdl.so.1", RDNLY) = 3
open("/usr/platform/SUNW,Ultra-5_10/lib/libc_psr.so.1", RDNLY) = 3
open("/", RDNLY) = 3
Wed Feb 15 09:24:47 AST 2006

It does have one called Brazil/Acre, but if I decide to specify
the rules for Brazil/Acre to say that it's 24 hours behind UTC:

$ TZ=Brazil/Acre24 truss -t open date
open("/var/ld/ld.config", RDNLY) Err#2 ENENT
open("/usr/lib/libc.so.1", RDNLY) = 3
open("/usr/lib/libdl.so.1", RDNLY) = 3
open("/usr/platform/SUNW,Ultra-5_10/lib/libc_psr.so.1", RDNLY) = 3
Tue Feb 14 14:25:41 Brazil/Acre 2006

the system-wide description file is not used.

作者: wazhl    时间: 2010-01-13 13:51
谢谢!!!
作者: alfred1980    时间: 2010-01-13 14:40
dati=`perl -e '($a,$b,$c,$day,$mon,$year,$d,$e,$f) = localtime(time-86400);printf "%4d%02d%02d",$year+1900,$mon+1,$day'`




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2