免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: facesmile
打印 上一主题 下一主题

UNIX系统中,怎么取当前时间三天前的时间! [复制链接]

论坛徽章:
0
11 [报告]
发表于 2005-12-23 10:15 |只看该作者
我已经参考那个一天前的时间,改了怎么得到三天前的时间脚本,但这个也太繁琐了,有没有更简单的?
我写的脚本如下:
#!/bin/sh

# ydate: A Bourne shell script that
# prints yestarday's date
# Output Form: Month Day Year
# From Focus on Unix: http://unix.about.com

# Set the current month day and year.
month=`date +%m`
day=`date +%d`
year=`date +%Y`

# Add 0 to month. This is a
# trick to make month an unpadded integer.
month=`expr $month + 0`

# Subtract one from the current day.
day=`expr $day - 3`

# If the day is 0 then determine the last
# day of the previous month.
if [ $day -eq 0 ]; then
  
  # Find the preivous month.
  month=`expr $month - 1`  

  # If the month is 0 then it is Dec 31 of
  # the previous year.
  if [ $month -eq 0 ]; then
    month=12
    day=31
    year=`expr $year - 1`  

  # If the month is not zero we need to find
  # the last day of the month.
  else
    case $month in
      1|3|5|7|8|10|12) day=31;;
      4|6|9|11) day=30;;
      2)
        if [ `expr $year % 4` -eq 0 ]; then
          if [ `expr $year % 400` -eq 0 ]; then
            day=29
          elif [ `expr $year % 100` -eq 0 ]; then
            day=28
          else
            day=29
          fi
        else
          day=28
        fi
      ;;
    esac
  fi
fi

if [ $day -eq -1 ]; then
  
  # Find the preivous month.
  month=`expr $month - 1`  

  # If the month is 0 then it is Dec 31 of
  # the previous year.
  if [ $month -eq 0 ]; then
    month=12
    day=30
    year=`expr $year - 1`  

  # If the month is not zero we need to find
  # the last day of the month.
  else
    case $month in
      1|3|5|7|8|10|12) day=30;;
      4|6|9|11) day=29;;
      2)
        if [ `expr $year % 4` -eq 0 ]; then
          if [ `expr $year % 400` -eq 0 ]; then
            day=28
          elif [ `expr $year % 100` -eq 0 ]; then
            day=27
          else
            day=28
          fi
        else
          day=27
        fi
      ;;
    esac
  fi
fi

if [ $day -eq -2 ]; then
  
  # Find the preivous month.
  month=`expr $month - 1`  

  # If the month is 0 then it is Dec 31 of
  # the previous year.
  if [ $month -eq 0 ]; then
    month=12
    day=29
    year=`expr $year - 1`  

  # If the month is not zero we need to find
  # the last day of the month.
  else
    case $month in
      1|3|5|7|8|10|12) day=29;;
      4|6|9|11) day=28;;
      2)
        if [ `expr $year % 4` -eq 0 ]; then
          if [ `expr $year % 400` -eq 0 ]; then
            day=27
          elif [ `expr $year % 100` -eq 0 ]; then
            day=26
          else
            day=27
          fi
        else
          day=26
        fi
      ;;
    esac
  fi
fi

# Print the month day and year.
da="${year}-${month}-${day} `date +%H:%M:%S`"
echo $da
exit 0

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
12 [报告]
发表于 2005-12-23 12:11 |只看该作者
原帖由 facesmile 于 2005-12-23 10:15 发表
我已经参考那个一天前的时间,改了怎么得到三天前的时间脚本,但这个也太繁琐了,有没有更简单的?
我写的脚本如下:
#!/bin/sh

# ydate: A Bourne shell script that
# prints yestarday's date
# Output ...

简单的,用linux的date

  1. date -d'-3 day'
复制代码

论坛徽章:
0
13 [报告]
发表于 2005-12-23 13:39 |只看该作者
是的,在LINUX上可以这么做,但在SUN,HP,AIX上面呢?

论坛徽章:
0
14 [报告]
发表于 2005-12-30 16:29 |只看该作者
没有大虾来解决了吗?

论坛徽章:
8
摩羯座
日期:2014-11-26 18:59:452015亚冠之浦和红钻
日期:2015-06-23 19:10:532015亚冠之西悉尼流浪者
日期:2015-08-21 08:40:5815-16赛季CBA联赛之山东
日期:2016-01-31 18:25:0515-16赛季CBA联赛之四川
日期:2016-02-16 16:08:30程序设计版块每日发帖之星
日期:2016-06-29 06:20:002017金鸡报晓
日期:2017-01-10 15:19:5615-16赛季CBA联赛之佛山
日期:2017-02-27 20:41:19
15 [报告]
发表于 2005-12-30 16:33 |只看该作者

论坛徽章:
0
16 [报告]
发表于 2005-12-31 10:00 |只看该作者
date  +%Y-%m-%d -d'-3 day'

论坛徽章:
0
17 [报告]
发表于 2005-12-31 10:26 |只看该作者
终端下直接执行:
date -d "3 days ago" '+%Y-%m-%d %H:%M:%S'

论坛徽章:
0
18 [报告]
发表于 2005-12-31 10:40 |只看该作者
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP