zoukankan      html  css  js  c++  java
  • busybox date 时间的加减

    1、下载安装busybox:

    # wget http://busybox.net/downloads/busybox-1.29.3.tar.bz2
    # tar -jxvf busybox-1.29.3.tar.bz2
    # cd busybox-1.29.3
    # make defconfig     //如果对根文件系统的大小不是很苛求,可以直接使用busybox的默认配置     
    # make 
    # make install

    构建date链接
    # ln -sf ./busybox ./date

    busybox date参数详解

    [busybox-1.29.3]# ./date --help
    BusyBox v1.29.3 (2019-11-25 11:00:35 CST) multi-call binary.
    
    Usage: date [OPTIONS] [+FMT] [TIME]
    
    Display time (using +FMT), or set time
    
        [-s,--set] TIME    Set time to TIME
        -u,--utc    Work in UTC (don't convert to local time)
        -R,--rfc-2822    Output RFC-2822 compliant date string
        -I[SPEC]    Output ISO-8601 compliant date string
                SPEC='date' (default) for date only,
                'hours', 'minutes', or 'seconds' for date and
                time to the indicated precision
        -r,--reference FILE    Display last modification time of FILE
        -d,--date TIME    Display TIME, not 'now'
        -D FMT        Use FMT for -d TIME conversion
    
    Recognized TIME formats:
        hh:mm[:ss]
        [YYYY.]MM.DD-hh:mm[:ss]
        YYYY-MM-DD hh:mm[:ss]
        [[[[[YY]YY]MM]DD]hh]mm[.ss]
        'date TIME' form accepts MMDDhhmm[[YY]YY][.ss] instead

    Linux系统中的date一般可以直接进行日期的相减,

    例如: centos7系统

      date 获取前一天的时间: date -d -1day     或者   date -d '1 day ago'

      date 获取前一个月的时间: date -d '1 month ago'

      date 获取前一年的时间: date -d '1 year ago'

    而busybox date则不可以直接获取前一天的时间,,需要时间数字相减的方法来实现获取前一天的时间

    例如: busybox date 获取前一天的时间:

    考虑到当前时间是年度第一天1月1日的情况,代码如下:

    year=`date +%Y`   // 获取当前时间的年份
    month=`date +%m`  // 获取当前时间的月份
    day=`date +%d`    // 获取当前时间的日期
    
    if [[ "$day" == "01" ]];then  // 如果当前时间是1号 ,则考虑一下月份问题
        if [[ "$month" == "01" ]];then  // 如果当前时间是1月1号,,获取的前一天则是去年的最后一天 ,所以年份需要减一,月份和日期则是12月31日
            year=`expr $year - 1`
            yesterday="${year}-12-31"
        elif [[ "$month" == "03" ]];then   // 如果当前时间是3月1号,获取的前一天则是2月的最后一天,2月又分28天和29天,,所以需要和4取余,
            year_type=`expr ${year} % 4`
            if [[ "$year_type" == "0" ]];then   // 与4取余为0则为闰年,这一年的2月最后一天是29号,,取余不为0则为平年,2月的最后一天是28号
                day="29"
            else
                day="28"
            fi
            yesterday="${year}-02-${day}"
    // 1,3,5,7,8,10,12月均是31天 elif [[ "$month" == "02" || "$month" == "04" || "$month" == "06" || "$month" == "08" || "$month" == "09" || "$month" == "11" ]];then month=`expr ${month} - 1` yesterday="${year}-${month}-31" elif [[ "$month" == "05" ||"$month" == "07" || "$month" == "10" || "$month" == "12" ]];then month=`expr ${month} - 1` yesterday="${year}-${month}-30" fi else yesterday=${year}-${month}-`expr ${day} - 1` fi

    echo $yesterday
  • 相关阅读:
    UVA 254 Towers of Hanoi
    UVA 701 The Archeologists' Dilemma
    UVA 185 Roman Numerals
    UVA 10994 Simple Addition
    UVA 10570 Meeting with Aliens
    UVA 306 Cipher
    UVA 10160 Servicing Stations
    UVA 317 Hexagon
    UVA 10123 No Tipping
    UVA 696 How Many Knights
  • 原文地址:https://www.cnblogs.com/carriezhangyan/p/11934663.html
Copyright © 2011-2022 走看看