免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
123下一页
最近访问板块 发新帖
查看: 7909 | 回复: 25
打印 上一主题 下一主题

[FreeBSD] FreeBSD下取得昨天日期的方法 [复制链接]

论坛徽章:
2
丑牛
日期:2013-09-29 09:47:222015七夕节徽章
日期:2015-08-21 11:06:17
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-04-26 09:02 |只看该作者 |倒序浏览
经常看到有人问如何得到昨天的日期,尤其是备份日志、数据库的时候都需要,我对shell不熟,原来都是发贴问的,好歹算是能满足需要了,现在正在学shell,今天把我现在所知道的方法给总结一下。

发到这里了:http://www.cublog.cn/u/4206/showart.php?id=104921 如有更新则会更新这里的,这样大家只看一页就什么都有了


方法一:
linux下的date加参数直接就可以得到昨天的日期,FreeBSD也可以的。

date -v -1d +%Y%m%d
将得到昨天的日期,显示年月日

具体可以man date

方法二:
#!/bin/sh
#昨天日期shell版
now=$(date +%Y%m%d%H%M)
echo $now
yesterday=`TZ=GMT+16;export TZ;date +%Y%m%d%H%M`
echo $yesterday

注:此处的16是24-8得来的,8是当前时区与GMT的差值,北京时间是8
这个方法似乎只能得到7天前的日期,再多就不行了,我实验了半天,好像最多最多能到167,也就是说只能得到7天前7小时的时间。大于167得到的时间就跟现在一模一样了。


这个方法实际上是“歪打正着”,本来是用来取得其它某个时区的当前时间的方法,却被用来获得本地之前的某个时间了。全球的时区一共才24个,也就是说,如果使用这种方法的话,其实“小时”的数值只有这么一点点合法的范围。

【POSIX】:


5860   offset Indicates the value added to the local time to arrive at
5861          Coordinated Universal Time. The offset has the form:
5862                  hh[:mm[:ss]]
5863          The minutes (mm) and seconds (ss) are optional. The hour (hh)
5864          shall be required and may be a single digit. The offset following
5865          std shall be required. If no offset follows dst, the alternative time
5866          is assumed to be one hour ahead of standard time. One or more
5867          digits may be used; the value is always interpreted as a decimal
5868          number. The hour shall be between zero and 24, and the minutes
5869          (and seconds)—if present—between zero and 59. The result of
5870          using values outside of this range is unspecified. If preceded by
5871          a '-', the timezone shall be east of the Prime Meridian;
5872          otherwise, it shall be west (which may be indicated by an
5873          optional preceding '+').

按照上面的说法,使用超范围数值的结果是未定义的。在实验中之所以还能撑到167个小时,应该是和具体实现有关了,但这个数值早就已经不合法了。


方法三:
用gawk的strftime函数,这个方法可以得到很多天前的,不像方法一的那么多限制,但是首先需要安装gawk
cd /usr/ports/japanese/gawk
make install clean
rehash

gawk 'BEGIN{print strftime("%Y%m%d%H%M",systime());print strftime("%Y%m%d%H%M",systime()-1*24*3600)}'
会返回当前的时间和昨天日期的时间

当然你可以把systime()-1*24*3600改成你希望的天数,只要改那个1就成了。

方法四:

#!/bin/sh
# 取得昨天的日期
YEAR=`date +%Y`
DAY=`date +%d`
MONTH=`date +%m`

DAY=`expr $DAY - 1`
if [ $DAY -eq 0 ]; then
MONTH=`expr $MONTH - 1`
if [ $MONTH -eq 0 ]; then
MONTH=12
YEAR=`expr $YEAR - 1`
fi
LASTLINE=`cal $MONTH $YEAR | grep "[0-9]" | tail -1`
DAY=`echo $LASTLINE | sed "s/.* \([0-9][0-9]*\)/\1/"`
fi
if [ $DAY -lt 10 ]; then
DAY=0$DAY
fi
YESTERDAY="$YEAR$MONTH$DAY"
echo $YESTERDAY

方法五:
来源于http://bbs.chinaunix.net/viewthr ... &extra=page%3D1

#!/bin/sh
#昨天日期shell版
yy=`date +%Y`;mm=`date +%m`;dd=`date +%d`
d0=`echo "$dd"|awk '{printf"%02d\n",$1-1}'`
m0=`echo "$mm"|awk '{printf"%02d\n",$1-1}'`
y0=`echo "$yy"|awk '{printf"%02d\n",$1-1}'`
case $d0 in
00) if [ $m0 = 00 ]; then
    ym=`cal 12 $y0|xargs|awk '{print $NF}'`
    y0=$y0;m0=12;d0=$ym
    else
    ym=`cal $m0 $yy|xargs|awk '{print $NF}'`
    d0=$ym;m0=$m0;y0=$yy
    fi
    ;;
*) d0=$d0;m0=$mm;y0=$yy;;
esac
echo "$y0$m0$d0"

[ 本帖最后由 剑心通明 于 2006-4-29 10:21 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2006-04-29 09:26 |只看该作者
原帖由 剑心通明 于 2006-4-26 09:02 发表
这个方法似乎只能得到7天前的日期,再多就不行了,我实验了半天,好像最多最多能到167,也就是说只能得到7天前7小时的时间。大于167得到的时间就跟现在一模一样了,不知道是为了什么?
...


按我的理解,这个方法实际上是“歪打正着”,本来是用来取得其它某个时区的当前时间的方法,却被用来获得本地之前的某个时间了。全球的时区一共才24个,也就是说,如果使用这种方法的话,其实“小时”的数值只有这么一点点合法的范围。

【POSIX】:

5860   offset Indicates the value added to the local time to arrive at
5861          Coordinated Universal Time. The offset has the form:
5862                  hh[:mm[:ss]]
5863          The minutes (mm) and seconds (ss) are optional. The hour (hh)
5864          shall be required and may be a single digit. The offset following
5865          std shall be required. If no offset follows dst, the alternative time
5866          is assumed to be one hour ahead of standard time. One or more
5867          digits may be used; the value is always interpreted as a decimal
5868          number. The hour shall be between zero and 24, and the minutes
5869          (and seconds)—if present—between zero and 59. The result of
5870          using values outside of this range is unspecified. If preceded by
5871          a '-', the timezone shall be east of the Prime Meridian;
5872          otherwise, it shall be west (which may be indicated by an
5873          optional preceding '+').

按照上面的说法,使用超范围数值的结果是未定义的。在实验中之所以还能撑到167个小时,应该是和具体实现有关了,但这个数值早就已经不合法了。

[ 本帖最后由 雨丝风片 于 2006-4-29 09:27 编辑 ]

论坛徽章:
2
丑牛
日期:2013-09-29 09:47:222015七夕节徽章
日期:2015-08-21 11:06:17
3 [报告]
发表于 2006-04-29 09:34 |只看该作者
也就是说本来只能是一天以内的了。
好像有些系统下是只能在一天以内,FB下我试了,最多到167

论坛徽章:
0
4 [报告]
发表于 2006-04-29 09:40 |只看该作者

论坛徽章:
0
5 [报告]
发表于 2006-04-29 09:40 |只看该作者
原帖由 剑心通明 于 2006-4-29 09:34 发表
也就是说本来只能是一天以内的了。
好像有些系统下是只能在一天以内,FB下我试了,最多到167



可能FB考虑到了有人会打这个工具的主意,就把上限放宽了点。。。

论坛徽章:
2
丑牛
日期:2013-09-29 09:47:222015七夕节徽章
日期:2015-08-21 11:06:17
6 [报告]
发表于 2006-04-29 09:45 |只看该作者
原帖由 小猴儿 于 2006-4-29 09:40 发表
收录了

http://bbs.unixsky.net/read.php?tid=2321


嘿嘿


给版权费

论坛徽章:
0
7 [报告]
发表于 2006-04-29 09:49 |只看该作者
原帖由 剑心通明 于 2006-4-29 09:45 发表

给版权费

:em11:
不给不给就不给, 真想要的话, 偶以身相许得了!!!

论坛徽章:
0
8 [报告]
发表于 2006-04-29 09:49 |只看该作者
首先date , 然后,按条件+-天数..... 然后用其价 touch 一个文件  ,然后取touch后的文件时间就好,突然想到的,不知道行不行.

论坛徽章:
0
9 [报告]
发表于 2006-04-29 09:55 |只看该作者
原帖由 小猴儿 于 2006-4-29 09:49 发表

:em11:
不给不给就不给, 真想要的话, 偶以身相许得了!!!


程序开发版只讨论技术,此类问题请私下交易,谢谢!

论坛徽章:
1
寅虎
日期:2013-09-29 23:15:15
10 [报告]
发表于 2006-04-29 10:06 |只看该作者
gawk这个不错,偶只需要1分钟前的^_^
# gawk 'BEGIN{print(strftime("%Y%m%d%H%M",systime())); print(strftime("%Y%m%d%H%M",systime()-60))}'
200604291005
200604291004
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP