Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 23

Thread: command to to find difference between dates

  1. #11
    Join Date
    Feb 2010
    Location
    Germany
    Posts
    2,518

    Talking Re: command to to find difference between dates

    Please be aware that, Bash is a mixture of “quick and dirty” and “strict”:
    • The “quick and dirty” is what I posted previously.


    A “strict” method ensures that the values which Bash is comparing are strictly integer values and not what Bash “decides for itself”:
    • Ensure that Bash is using integer values: ‘declare -i LastDate’ -- ensures that the parameter “LastDate” is in fact an integer value …
    • Use integer comparisons: ‘if (( $LastDate < $TwoDaysAgo ))’ -- note the integer “less than” comparison and the use of ‘((’ and ‘))’ to force Arithmetic Evaluation of the Compound Command …

  2. #12

    Default Re: command to to find difference between dates

    With additional information (and help here), I found on linux help pages, this is a code snip from scanvirus that allows me to keep the original logs. NOT recommended to use at this time for current scan virus users. Still needs some debugging. I may need to have users delete or rename old logs.

    Code:
        elif [ "$1" = "-vl" ] || [ "$1" = '-viewlogs' ] ; then
            let system_mask=0
            if [[ "$2" == *"l"* ]]; then
                system_mask=1
                    
            elif [ "$2" == *"m"* ];then
                system_mask=2
            fi
    
            let scan_limit=0
            if [[ "$2" == *"d"* ]]; then
                if [[ "$3" == "" ]]; then
                    let scan_limit=0
                else
                    scan_limit="$3"
                fi
            fi
    
            current_time_seconds=$(date +%s)
            let linecount=0
            while read -ra line
            do
                let linecount=linecount+1
                if [ "$linecount" -le 4 ]; then
                    for update_field in ${line[*]}
                    do
                        printf "%s" $update_field
                    done
                    printf "\n"
                    continue
                fi
    
                scan_time_seconds=${line[6]}
                if [ "$scan_time_seconds" = "" ]; then
                    #date --date="11/12/2018 09:04:34pm" '+%s'
                    #echo "11-13-2018"  | tr "-" "/"
                    scan_date=$(printf "${line[3]}" | tr "-" "/")
                    scan_time=${line[4]}
                    scan_time_seconds=$(date --date="$scan_date $scan_time" '+%s')
                    #printf "scan_time_seconds= %s %s\n" $scan_time_seconds
                fi
                
                scan_difference_seconds=$((current_time_seconds - scan_time_seconds))
                scan_difference_minutes=$((scan_difference_seconds / 60))
                scan_difference_hours=$((scan_difference_minutes / 60))
                scan_difference_days=$((scan_difference_hours / 60))
                #printf "%s - %s = %s %s\n" $current_date $scan_date $scan_difference_seconds $scan_difference_minutes
    
                if [ $scan_difference_days -le $scan_limit ] || [ $scan_limit -eq 0 ]; then
                    printf "%s %s %s %s %s %s %s" ${line[0]} ${line[1]} ${line[2]} ${line[3]} ${line[4]} ${line[5]}
                    printf "  %s days" $scan_difference_days
                    printf "\n"
                fi
            done < "${Virus_Vault_Folder}/VirusScanLog.txt"
            printf "\n"

  3. #13
    Join Date
    Feb 2010
    Location
    Germany
    Posts
    2,518

    Default Re: command to to find difference between dates

    Quote Originally Posted by lord_valarian View Post
    Still needs some debugging.
    My inspection raises the following issues, supported by TLDP (“The Linux Documentation Project”) notes:
    Code:
    elif [ "$1" = "-vl" ] || [ "$1" = '-viewlogs' ] ; then
    • Note the TLDP explanation of the meaning of “=” versus “==” …

    <https://www.tldp.org/LDP/abs/html/opprecedence.html> <https://www.tldp.org/LDP/abs/html/co...l#EQUALSIGNREF>
    I find the use of a single “=” «surrounded by whitespace <https://www.tldp.org/LDP/abs/html/sp...#WHITESPACEREF>» for string comparison to be not as clear as the use of “==” or, possibly but not as clear, “=.” -- one can easily invoke an “assignment” which (usually) always succeeds «successfully == ‘0’ » …

    Note the explanation of “[ … ]” versus “[[ … ]]” here: <https://www.tldp.org/LDP/abs/html/testconstructs.html>
    Using the [[ ... ]] test construct, rather than [ ... ] can prevent many logic errors in scripts.
    Code:
    let system_mask=0
    • Have you read the following TLDP notes on “Arithmetic Expansion”?

    <https://www.tldp.org/LDP/abs/html/arithexp.html> <https://www.tldp.org/LDP/abs/html/internal.html#LETREF>
    AFAICS, ‘let’ is useful but, beware of the required syntax …

  4. #14

    Default Re: command to to find difference between dates

    Still trying to understand the small differences in syntax for '==' and '=', as well other syntax. I'll get back that later.

    Several bugs have come up.

    Code:
                if [ $scan_difference_days -le $scan_limit ] || [ $scan_limit -eq 0 ]; then
                    printf "%s %s %s %s %s %s %s" ${line[0]} ${line[1]} ${line[2]} ${line[3]} ${line[4]} ${line[5]}
                    printf "  %s " $scan_difference_days
                    printf "\n"
                fi
    The days are wrong. I thought it was a rounding error. So, I combined the line.

    Code:
    scan_difference_days=$((scan_difference_seconds / 216000))
    Same output.

    .....VirusScanLog.....
    _____________________________________________________________________

    Clean Scan: linux 11-07-2018 04:25:55pm {} 5
    Clean Scan: linux 11-07-2018 05:01:49pm {} 5
    Clean Scan: linux 11-07-2018 05:21:34pm {} 5
    Clean Scan: linux 11-08-2018 01:21:37pm {} 4
    Clean Scan: linux 11-11-2018 01:14:36pm {} 3
    Clean Scan: linux 11-11-2018 01:38:24pm {} 3
    Clean Scan: linux 11-12-2018 12:57:02pm {} 3
    Clean Scan: linux 11-12-2018 08:44:07pm {} 3
    Clean Scan: mswin 11-12-2018 08:45:36pm {;} 3
    Clean Scan: linux 11-12-2018 09:04:34pm {} 3
    Clean Scan: linux 11-13-2018 01:44:37pm {} 2
    Clean Scan: linux 11-13-2018 02:33:04pm {} 2
    Clean Scan: linux 11-13-2018 10:24:35pm {} 2
    Clean Scan: linux 11-14-2018 01:46:56pm {} 2
    Clean Scan: linux 11-14-2018 08:21:06pm {} 2
    Clean Scan: linux 11-14-2018 09:05:43pm {} 2
    Clean Scan: linux 11-16-2018 02:03:40pm {} 1
    Clean Scan: linux 11-16-2018 09:35:14pm {} 1
    Clean Scan: linux 11-16-2018 09:35:32pm {} 1
    Clean Scan: linux 11-18-2018 12:47:23pm {} 0
    Clean Scan: linux 11-18-2018 12:48:12pm {} 0
    Clean Scan: linux 11-18-2018 02:03:20pm {} 0
    Clean Scan: linux 11-20-2018 01:25:38am {} 0
    Virus Found: mswin 11-20-2018 01:59:50am {Recovery;EFI 7142

    Unless the date app program has a bug, there is a one in my code. I'll study that new info.

    The last line shows another bug. I'm working on it.

  5. #15

    Default Re: command to to find difference between dates

    I'v been reading the comments and the help pages. The string comparison work. I'll leave them alone for now. 'let systemmask=0' is being used like white space. It might be causing a bug.

    Also, i'v figured out how to fix the broken line for 'mswin'. I may need to change the position of the time stamp.

    I forgot to include all three: code, input, and output. Using 'declare -i var1 var2 var3', mixing strings and vars might be creating a buggy output.


    Code:
        elif [ "$1" = "-vl" ] || [ "$1" = '-viewlogs' ] ; then
            let system_mask=0
            if [[ "$2" == *"l"* ]]; then
                system_mask=1
                    
            elif [ "$2" == *"m"* ];then
                system_mask=2
            fi
    
            let scan_limit=0
            if [[ "$2" == *"d"* ]]; then
                if [[ "$3" == "" ]]; then
                    let scan_limit=0
                else
                    scan_limit="$3"
                fi
            fi
    
            current_time_seconds=$(date +%s)
            let linecount=0
            while read -ra line
            do
                let linecount=linecount+1
                if [ "$linecount" -le 4 ]; then
                    for update_field in ${line[*]}
                    do
                        printf "%s" $update_field
                    done
                    printf "\n"
                    continue
                fi
    
                scan_time_seconds=$(printf "${line[6]}" | tr " " "/")
                if [ "$scan_time_seconds" = "" ]; then
                    #date --date="11/12/2018 09:04:34pm" '+%s'
                    #echo "11-13-2018"
                    scan_date=$(printf "${line[3]}" | tr "-" "/")
                    scan_time=${line[4]}
                    scan_time_seconds=$(date --date="$scan_date $scan_time" '+%s')
                    #printf "scan_time_seconds= %s %s\n" $scan_time_seconds
                fi
                
                scan_difference_seconds=$((current_time_seconds - scan_time_seconds))
                scan_difference_minutes=$((scan_difference_seconds / 60))
                scan_difference_hours=$((scan_difference_minutes / 60))
                scan_difference_days=$((scan_difference_seconds / 216000))
                #printf "%s - %s = %s %s\n" $current_date $scan_date $scan_difference_seconds $scan_difference_minutes
    
                if [ $scan_difference_days -le $scan_limit ] || [ $scan_limit -eq 0 ]; then
                    printf "%s %s %s %s %s %s %s" ${line[0]} ${line[1]} ${line[2]} ${line[3]} ${line[4]} ${line[5]}
                    printf "  %s " $scan_difference_days
                    printf "\n"
                fi
            done < "${Virus_Vault_Folder}/VirusScanLog.txt"
            printf "\n"
    Input file: scanlog file is worst possible mixing of old and new code runs. To see if the new code can handle it.


    Code:
    ..... Virus Scan Log .....
    _____________________________________________________________________
    
    Clean Scan: linux 11-07-2018 04:25:55pm {}
    Clean Scan: linux 11-07-2018 05:01:49pm {}
    Clean Scan: linux 11-07-2018 05:21:34pm {}
    Clean Scan: linux 11-08-2018 01:21:37pm {}
    Clean Scan: linux 11-11-2018 01:14:36pm {}
    Clean Scan: linux 11-11-2018 01:38:24pm {}
    Clean Scan: linux 11-12-2018 12:57:02pm {}
    Clean Scan: linux 11-12-2018 08:44:07pm {} 1542084259
    Clean Scan: mswin 11-12-2018 08:45:36pm {;} 1542084350
    Clean Scan: linux 11-12-2018 09:04:34pm {} 1542085487
    Clean Scan: linux 11-13-2018 01:44:37pm {}
    Clean Scan: linux 11-13-2018 02:33:04pm {}
    Clean Scan: linux 11-13-2018 10:24:35pm {}
    Clean Scan: linux 11-14-2018 01:46:56pm {}
    Clean Scan: linux 11-14-2018 08:21:06pm {}
    Clean Scan: linux 11-14-2018 09:05:43pm {} 1542258358
    Clean Scan: linux 11-16-2018 02:03:40pm {}
    Clean Scan: linux 11-16-2018 09:35:14pm {} 1542432926
    Clean Scan: linux 11-16-2018 09:35:32pm {} 1542432944
    Clean Scan: linux 11-18-2018 12:47:23pm {} 1542574055
    Clean Scan: linux 11-18-2018 12:48:12pm {} 1542575479
    Clean Scan: linux 11-18-2018 02:03:20pm {} 1542579740
    Clean Scan: linux 11-20-2018 01:25:38am {}
    Virus Found: mswin 11-20-2018 01:59:50am {Recovery;EFI system partition;MSWIN6410;;Backups;;} 1542725039
    Clean Scan: linux 11-20-2018 02:13:36pm {} 1542753136
    Clean Scan: linux 11-22-2018 01:42:07pm {} 1542924356
    Output
    Code:
    .....VirusScanLog.....
    _____________________________________________________________________
    
    Clean Scan: linux 11-07-2018 04:25:55pm {}   5 
    Clean Scan: linux 11-07-2018 05:01:49pm {}   5 
    Clean Scan: linux 11-07-2018 05:21:34pm {}   5 
    Clean Scan: linux 11-08-2018 01:21:37pm {}   5 
    Clean Scan: linux 11-11-2018 01:14:36pm {}   4 
    Clean Scan: linux 11-11-2018 01:38:24pm {}   4 
    Clean Scan: linux 11-12-2018 12:57:02pm {}   4 
    Clean Scan: linux 11-12-2018 08:44:07pm {}   3 
    Clean Scan: mswin 11-12-2018 08:45:36pm {;}   3 
    Clean Scan: linux 11-12-2018 09:04:34pm {}   3 
    Clean Scan: linux 11-13-2018 01:44:37pm {}   3 
    Clean Scan: linux 11-13-2018 02:33:04pm {}   3 
    Clean Scan: linux 11-13-2018 10:24:35pm {}   3 
    Clean Scan: linux 11-14-2018 01:46:56pm {}   3 
    Clean Scan: linux 11-14-2018 08:21:06pm {}   3 
    Clean Scan: linux 11-14-2018 09:05:43pm {}   3 
    Clean Scan: linux 11-16-2018 02:03:40pm {}   2 
    Clean Scan: linux 11-16-2018 09:35:14pm {}   2 
    Clean Scan: linux 11-16-2018 09:35:32pm {}   2 
    Clean Scan: linux 11-18-2018 12:47:23pm {}   1 
    Clean Scan: linux 11-18-2018 12:48:12pm {}   1 
    Clean Scan: linux 11-18-2018 02:03:20pm {}   1 
    Clean Scan: linux 11-20-2018 01:25:38am {}   1 
    Virus Found: mswin 11-20-2018 01:59:50am {Recovery;EFI   7143 
    Clean Scan: linux 11-20-2018 02:13:36pm {}   0 
    Clean Scan: linux 11-22-2018 01:42:07pm {}   0

  6. #16

    Default Re: command to to find difference between dates

    I'm can't find any code error. Unless someone finds the problem of the date conversion, I have to drop the idea in favor of an alternative idea. I'll wait a few more days.

  7. #17

    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

  8. #18
    Join Date
    Jun 2008
    Location
    Groningen, Netherlands
    Posts
    19,791
    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

  9. #19
    Join Date
    Feb 2010
    Location
    Germany
    Posts
    2,518

    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 …

  10. #20

    Default Re: command to to find difference between dates

    Quote Originally Posted by Knurpht View Post
    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.
    FYI, I'v been doing some formatting of the code to remove the single ' =' and consistent use of '[[' and ']]'. No errors reported and it works.

    Code:
    #date --date="11/12/2018 09:04:34pm" '+%s'
    This is the base code I found on a website. In my code, the larger the date difference, the worse the error. So, either this base code is the wrong method of date
    conversion or the date command has a bug.

    It looks like the bash 'date' command conversion isn't taking into account extra days in some months. NOV 30 days, DEC 31 days. Unless, I missed something that code should work.

    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 …
    Any attempt at using integers gives me errors and a lot of errors. Any recommended reads for integers?

Page 2 of 3 FirstFirst 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
  •