Page 1 of 3 123 LastLast
Results 1 to 10 of 23

Thread: command to to find difference between dates

Hybrid View

  1. #1

    Default command to to find difference between dates

    For code i'm writing, I need to find the difference one time/date and another time/date.

    Is there a command to find the difference between one date and another date.

    m1/d1/y1 - m2/m2/y2

    I can figure out on my own, but I'm hoping for a command that does this function for you.


    Need help.

  2. #2
    Join Date
    Jun 2008
    Location
    Groningen, Netherlands
    Posts
    19,645
    Blog Entries
    14

    Default Re: command to to find difference between dates

    Completely depends on the kind of code you're wrighting. MySQL queries can do this, php can do it. I'm oldfashioned, mostly bring dates back to secs from 1970-1-1 0:00:00 than subtract, and format the result.
    ° Appreciate my reply? Click the star and let me know why.

    ° Perfection is not gonna happen. No way.

    https://en.opensuse.org/openSUSE:Board#Members
    http://en.opensuse.org/User:Knurpht
    http://nl.opensuse.org/Gebruiker:Knurpht

  3. #3
    Join Date
    Jun 2008
    Location
    Podunk
    Posts
    26,498
    Blog Entries
    15

    Default Re: command to to find difference between dates

    Quote Originally Posted by lord_valarian View Post
    For code i'm writing, I need to find the difference one time/date and another time/date.

    Is there a command to find the difference between one date and another date.

    m1/d1/y1 - m2/m2/y2

    I can figure out on my own, but I'm hoping for a command that does this function for you.


    Need help.
    Hi
    Install dateutils and there is ddiff and dadd... check the man page for formatting as required...

    Code:
    ddiff 2017-11-15 2018-27-08
    350
    Cheers Malcolm °¿° SUSE Knowledge Partner (Linux Counter #276890)
    SUSE SLE, openSUSE Leap/Tumbleweed (x86_64) | GNOME DE
    If you find this post helpful and are logged into the web interface,
    please show your appreciation and click on the star below... Thanks!

  4. #4

    Default Re: command to to find difference between dates

    Hi,

    Since openSUSE has GNU date(1) you can probably convert the dates to epoch and do the math.

    Code:
    date1=$(date -d 'yesterday' '+%s')
    Code:
    date2=$(date -d 'now' '+%s')

    Code:
    echo $(( date2 - date1 ))
    Code:
    86410
    Not sure it helps but GNU date(1) can give some human readable format as well.

    Code:
    date -d 'yesterday' '+%F %T'
    Code:
    date -d '-1 year' '+%F %T'
    Code:
    date -d '-30 min'  '+%F %T'

    Code:
    bc -q <<<  "$(date -d '-30 min' '+%s') -  $(date -d 'yesterday' '+%s')"
    see the man pages for more info.

    Code:
    man 1 date
    If you have control over the date, meaning you're the one giving the date format then yeah GNU date(1) might be a good choice for you, if not well dateutils as suggested above.
    "Unfortunately time is always against us" -- [Morpheus]

    .:https://github.com/Jetchisel:.

  5. #5

    Default Re: command to to find difference between dates

    Code:
    malcolmlewis
    Hi
    Install dateutils and there is ddiff and dadd... check the man page for formatting as required...
    
    Code:
    
    ddiff 2017-11-15 2018-27-08
    350
    I'm trying to avoid having too many dependencies in scanvirus. It now has two. However, I will keep that info noted for something else. Thanks.


    scanvirus is missing the feature: how long since last scan? linux and mswin

    I have various ways of saving that data, including globals and logfile. If I read the logfile backward or grep, I can find the date info. Next question...

    How do I convert a text date into numbers I can use?

    '9-9-18' --> 9-9-18 month=9, day=9, year= 2018

    Parsing using '-'. Some other method?

  6. #6
    Join Date
    Jun 2008
    Location
    San Diego, Ca, USA
    Posts
    10,923
    Blog Entries
    2

    Default Re: command to to find difference between dates

    Quote Originally Posted by lord_valarian View Post
    Code:
    malcolmlewis
    Hi
    Install dateutils and there is ddiff and dadd... check the man page for formatting as required...
    
    Code:
    
    ddiff 2017-11-15 2018-27-08
    350
    I'm trying to avoid having too many dependencies in scanvirus. It now has two. However, I will keep that info noted for something else. Thanks.


    scanvirus is missing the feature: how long since last scan? linux and mswin

    I have various ways of saving that data, including globals and logfile. If I read the logfile backward or grep, I can find the date info. Next question...

    How do I convert a text date into numbers I can use?

    '9-9-18' --> 9-9-18 month=9, day=9, year= 2018

    Parsing using '-'. Some other method?
    First,
    As jetchisel and dcurtisfra describe,
    date is available by default and supports diffing dates.

    As for changing date formats, I'd recommend you simply avoid the problem instead of doing a conversion (although of course conversion is an option). Create your own application log and create events however you wish, including your preferred data format. Then you can extract data as you wish from your own logs and do as you wish.

    Or,
    Re-think what your task, maybe there is a different and possibly better way of doing what you need to have accomplished.
    For instance, why do you need to diff your dates and then do something with that info? Would simply activating a counter that counts seconds and then execute a task at some threshold accomplish the same objective?

    TSU
    Beginner Wiki Quickstart - https://en.opensuse.org/User:Tsu2/Quickstart_Wiki
    Solved a problem recently? Create a wiki page for future personal reference!
    Learn something new?
    Attended a computing event?
    Post and Share!

  7. #7

    Default Re: command to to find difference between dates

    Quote Originally Posted by tsu2 View Post
    First,
    As jetchisel and dcurtisfra describe,
    date is available by default and supports diffing dates.

    As for changing date formats, I'd recommend you simply avoid the problem instead of doing a conversion (although of course conversion is an option). Create your own application log and create events however you wish, including your preferred data format. Then you can extract data as you wish from your own logs and do as you wish.

    Or,
    Re-think what your task, maybe there is a different and possibly better way of doing what you need to have accomplished.
    For instance, why do you need to diff your dates and then do something with that info? Would simply activating a counter that counts seconds and then execute a task at some threshold accomplish the same objective?

    TSU

    I'v been reading (and reading again) the various posts (and later again). The current scanvirus log:

    Clean Scan: linux 10-09-2018 10:01:43pm {}
    Clean Scan: mswin 10-10-2018 10:01:43pm {(partitions scanned)}


    These scans I usually run after booting and often more than once a session.

    A counter that functions outside the script would require more complex code. I understand the linux concept of multi-user and multi-processing system. I'm avoiding starting other processes that I would need to later shutdown. Based on all the info presented, I --think-- i'v found the simplest and most practical method. This also avoids adding more dependencies.

    Provide some simple upgrade instructions to the user. Copy older logs somewhere else.

    dateinsecondssince1970 Clean Scan: linux 10-09-2018 10:01:43pm {}

    The first parm is masked out in 'scanvirus -vl'. The script can then make use of new field. Then It should be a simple matter of finding both last mswin and linux scan.

    Also, I make an additional data file holding date of last scans of linux and mswin. Or adding that info to the beginning of the logfile, masking it from 'scanvirus -vl'.


    Thanks to all for the very useful info. I need to work the on the new design more.

  8. #8

    Default Re: command to to find difference between dates

    Quote Originally Posted by tsu2 View Post
    First,
    As jetchisel and dcurtisfra describe,
    date is available by default and supports diffing dates.

    As for changing date formats, I'd recommend you simply avoid the problem instead of doing a conversion (although of course conversion is an option). Create your own application log and create events however you wish, including your preferred data format. Then you can extract data as you wish from your own logs and do as you wish.

    Or,
    Re-think what your task, maybe there is a different and possibly better way of doing what you need to have accomplished.
    For instance, why do you need to diff your dates and then do something with that info? Would simply activating a counter that counts seconds and then execute a task at some threshold accomplish the same objective?

    TSU

    I took your advice. I did a complete redesign. The help menu shows the changes. Thanks to all for the information.

    FYI, I tried to get 'declare' integer to work, use integers from the start. I kept getting errors. I'll have to do more reading on the subject. Including the '[[' '[' '=' '==' string operators.

    scanvirus 'view logs' mask works, but I still have some coding on the rest of scanvirus before another release.

    Scheduled scans? I'll give that some thought.




    Code:
    Scan Virus
    help commands
    -------------
    virus scan linux files
        p1: -l or linux
        p2: u   freshclam update     (optional)
        p2: l   low system priority  (optional)
        p2: p   shutdown on finish + high system priority  (optional)
    
    virus scan windows files
        p1: -m or -mswin
        p2: m   Move to Vault        (optional)
        p2: u   freshclam update     (optional)
        p2: l   low system priority  (optional)
        p2: p   shutdown on finish + high system priority  (optional)
    open virus vault (KDE)
        p1: -vf
    view logs
        p1: -vl
        p2: l     mask by linux scans
        p2: m   mask by mswin scans
        p2: d    mask by year and month  p3: year  p4: date

    Code:
    scanvirus -vl
    scanvirus -vl m
    scanvirus -vl l
    scanvirus -vl d 2018 11
    scanvirus -vl dm 2018 11
    scanvirus -vl dl 2018 11

  9. #9
    Join Date
    Jun 2008
    Location
    Groningen, Netherlands
    Posts
    19,645
    Blog Entries
    14

    Default Re: command to to find difference between dates

    You're giving yourself a hardtime in the date/time calculation. Instead of splitting up, better use the entire date/time string to get the right number, do the calculation, then reformat to your liking.
    ° Appreciate my reply? Click the star and let me know why.

    ° Perfection is not gonna happen. No way.

    https://en.opensuse.org/openSUSE:Board#Members
    http://en.opensuse.org/User:Knurpht
    http://nl.opensuse.org/Gebruiker:Knurpht

  10. #10
    Join Date
    Feb 2010
    Location
    Germany
    Posts
    2,475

    Smile Re: command to to find difference between dates

    Quote Originally Posted by Knurpht View Post
    better use the entire date/time string to get the right number, do the calculation,
    And, IMHO, the reason is, the way that UNIX® -- and therefore Linux -- handles time:
    • Basically it's (currently) a 64 bit value (used to be a 32 bit value) which is the number of seconds since the UNIX® epoch: 00:00:00 UTC Thursday, 1st January 1970 …
    • Yes, at system programming levels we begin to talk about the “jiffy” which is determined by the kernel constant “HZ” which is often 4 ms.
    • You can also take a look at output of “/proc/timer_list” -- on this system, CPU 0 -- clock 0 -- has a resolution of 1 ns …


    The bottom line for scripts is, time is a 64 bit integer value representing the seconds since the UNIX® epoch …
    • In a script, simply handle everything which is dealing with time as a 64 bit integer value …

Page 1 of 3 123 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •