1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to perform date addition?

Discussion in 'Tomato Firmware' started by leandroong, Jul 16, 2013.

  1. leandroong

    leandroong Addicted to LI Member

  2. Planiwa

    Planiwa LI Guru Member

    Code:
    date +%s           # get epoch time in seconds
    date -d @nnnnnnnnn # display epoch time in human readable form
    
    For example:

    Code:
       date
    Tue Jul 16 00:58:36 EDT 2013
       date +%s
    1373950719
       date -d @1373950719
    Tue Jul 16 00:58:39 EDT 2013
    
    And, ...

    Code:
       date; date -d @$(($(date +%s) + 1200))
    Tue Jul 16 01:03:57 EDT 2013
    Tue Jul 16 01:23:57 EDT 2013
    
     
  3. leandroong

    leandroong Addicted to LI Member

    Sample:
    date; date +%M

    Result:
    Code:
    Tue Jul 16 13:08:30 UTC 2013
    08
    
    Q: How do I add to minutes, say date +%M +10 and store to variable ?

    Result:
    Code:
    root@BTRouter2:/tmp# date;date +%M +10
    Tue Jul 16 13:10:29 UTC 2013
    date: invalid date '+10'
    
     
  4. leandroong

    leandroong Addicted to LI Member

    Thanks, got it: To add 15 minutes, I did this
    Code:
    oot@BTRouter2:/tmp# date;date -d @$(($(date +%s) +900))
    Tue Jul 16 13:17:19 UTC 2013
    Tue Jul 16 13:32:19 UTC 2013
    
     
  5. leandroong

    leandroong Addicted to LI Member

    How can I eventually extract the hour, minute and seconds into variables?
    doing this:
    Code:
    root@BTRouter2:/tmp# date;date -d @$(($(date +%s) +900)) +%M
    Tue Jul 16 13:25:51 UTC 2013
    40
     
    root@BTRouter2:/tmp# date;dmin=date -d @$(($(date +%s) +900)) +%M
    Tue Jul 16 13:27:17 UTC 2013
    -sh: -d: not found
    
    
     
  6. Planiwa

    Planiwa LI Guru Member

    Code:
         date;    set -- $(date|tr ':' ' ');    echo $4 $5 $6
    Tue Jul 16 01:33:48 EDT 2013
    01 33 48
    
     
  7. leandroong

    leandroong Addicted to LI Member

    Thanks a lot
     
  8. koitsu

    koitsu Network Guru Member

    Be aware this method will break badly when using certain locales (above makes assumption colon is always the delimiter).

    The important part to know is that libc on *IX systems already provides all these fields for you -- it's just that you're limited because of your choice of sticking to shell scripts; see struct tm for proof.

    This is one of those situations where using an actual programming language is the correct choice of action; for example in Perl this is available via the gmtime() or localtime() functions. I'm certain Python et al have similar.
     

Share This Page