• [quote] How ot calculate date in linux cmd


    From

    http://linuxconfig.org/addition-and-subtraction-arithmetics-with-linux-date-command

     

    This article features some simple examples on how to add or subtract a time from a current date. At first we use a native date sytax to do this trick and later we will see how this can be done manually by converting date to an epoch time. Let's start with a simple backup script based on the date Linux command:

    #!/bin/bash
    
    tar cjf linuxconfig_$(date +%H%M-%d%m%Y).tar.bz2 ~/public_html
    

    Every time the script is executed it will create a file with a current date included in the file name. The backup problem is solved. However, we do not usually want to keep all backup files indefinitely or until they consume all available free space. This is where the subtraction arithmetics with Linux date command comes handy. Let's see couple examples how to subtract time from a current date using date string: Subtract 10 years from a current date:

    $ date
    Mon Mar  7 09:26:13 EST 2011
    $ date --date="10 years ago" +%H%M-%d%m%Y
    0926-07032001
    

    Subtract 3 months from a current date:

    $ date
    Mon Mar  7 09:29:39 EST 2011
    $ date --date="3 months ago" +%H%M-%d%m%Y
    0929-07122010
    

    Subtract 255 days from a current date:

    $ date
    Mon Mar  7 09:31:13 EST 2011
    $ date --date="255 days ago" +%H%M-%d%m%Y
    0831-25062010
    

    Subtract 32 weeks from a current date:

    date; date --date="32 weeks ago" +%H%M-%d%m%Y
    Mon Mar  7 10:05:34 EST 2011
    0905-26072010
    

    Subtract hours minutes from a current date:

    $ date; date --date="5 hours ago" +%H%M-%d%m%Y
    Mon Mar  7 09:54:42 EST 2011
    0454-07032011
    

    and as you have already guessed we follow the same format to subtract minutes from current date:

    $ date; date --date="5 minutes ago" +%H%M-%d%m%Y
    Mon Mar  7 09:32:48 EST 2011
    0927-07032011
    

    We can now improve our simple backup script to keep only files which are not older than 6 months:

    #!/bin/bash
    
    tar cjf linuxconfig_$(date +%H%M-%d%m%Y).tar.bz2 ~/public_html
    rm linuxconfig_$(date --date="6 months ago" +%H%M-%d%m%Y).tar.bz2
    

    Going into the future with date command is as easy as going into the past. All what needs to be done is to add "-" ( minus ) sign in front of every date string. For example you can ask date command to add 12 hours to a current date and time:

    date; date --date="-12 hours ago" +%H%M-%d%m%Y
    Mon Mar  7 10:02:17 EST 2011
    2202-07032011
    

    On some Unix systems the date syntax described above may not be available. In this case here as a simple example on how to the do all above using epoch time. epoch time is simply a number of seconds since "Jan 1, 1970 00:00:00". Therefore, epoch time "1" using universal time is:

    $ date -ud@1
    Thu Jan  1 00:00:01 UTC 1970
    

    Lets see how we can subtract 2 weeks 3 days and 23 seconds from a current date sing epoch time. Please not that we use only universal time for a following calculations. In the first step we need to convert 2 weeks 3 days and 23 seconds into seconds. Use a following table to guide you through this process:

    1 year ( 365 ) 31,536,000 seconds
    1 week 604 800 seconds
    1 day 86 400 seconds
    1 hour 3600 seconds
    1 minute 60 seconds
    1 second 1 second
    2 weeks 3 days and 23 seconds = 2 x 604 800 + 3 x 86 400 + 23
    2 weeks 3 days and 23 seconds = 1 209 600 + 259 200 + 23
    2 weeks 3 days and 23 seconds = 1 468 823 seconds
    

    Now, that we have number of seconds lets subtract this number from a current epoch time:

    $ date;echo `date --universal +%s` - 1468823 | bc 
    Mon Mar  7 10:43:14 EST 2011
    1297986171
    

    All what has left is to convert output echo time to universal human readable date format:

    $ date -ud@1297986171
    Thu Feb 17 23:42:51 UTC 2011
    

    To add 2 weeks 3 days and 23 seconds use the same process but use addition instead of subtraction:

    $ date;echo `date --universal +%s` + 1468823 | bc
    Mon Mar  7 11:02:29 EST 2011
    1300924972
    

    Convert epoch time:

    $ date -ud@1300924972
    Thu Mar 24 00:02:52 UTC 2011
    
  • 相关阅读:
    mysql8.0.20安装
    MySQL EXPLAIN结果集分析
    初次安装aliSql
    升级vim到8.0
    REPL环境对语言的帮助
    Python环境搭建及pip的使用
    mysql数据库分库分表(Sharding)
    Git的使用
    Promise的初步认识
    对引用的文件起别名
  • 原文地址:https://www.cnblogs.com/lake-of-embedded-system/p/3483905.html
Copyright © 2020-2023  润新知