Menu

The simplest way to calculate the number of seconds between two times

2021-12-24
2022-02-08
  • Eugenio Di Lorenzo

    GnuCOBOL offers a lot of FUNCTIONS for managing DATE and TIME.
    but ... what do you think is the simplest way to display the following three data:
    - the time at the start of the program
    - the time that passes as time passes
    - the difference in number of seconds between the two previous times as time passes 1, 2 3, .... 1326, 1327, etc et
    - the difference in format hh:mm:ss between the two previous times as time passes 1

     

    Last edit: Eugenio Di Lorenzo 2021-12-24
  • Simon Sobisch

    Simon Sobisch - 2021-12-24

    A quick answer, just my first thoughts, no actual testing involved.

    the difference in number of seconds between the two previous times as time passes

    I'd go with SECONDS-PAST-MIDNIGHT (taking care for the possible date swap, of course)

    the time that passes as time passes

    Isn't that the same as the one above?

    the difference in format hh:mm:ss between the two previous times as time passes 1

    FORMATTED-TIMEwith the seconds already available

     

    Last edit: Simon Sobisch 2021-12-24
  • Michael F Gleason

    If you are still interested in time durations, here are a couple of copy books I use in some of my programs. They are and expansion to code in the sample DEMOMATH.COB.
    Basically you save a start time like when the program starts or the beginning of some event in your code, then later perform the routine and get the duration back in hours, minutes and seconds.

    I also included a program that I used to test the copy books.

    You may find part of the code useful.

     
  • Eugenio Di Lorenzo

    First of all, thank you a lot for sharing your solution and your source code. this I think should be the spirit of all fans of open source code and free software code.
    Thanks again, really appreciated.
    Going into the merits of the problem, I can say that I too had found an "old-fashioned" solution.
    I am attaching an example below.

    But ... the main purpose of this post was to verify if there is a simpler solution through the use of intrinsic functions. For example we have many "old-fashioned" solutions for calculations between dates. A lot of source code has been written over time for this but with the intrinsic functions everything has become a lot simpler.

    For calculations between times, on the other hand, I have not yet seen if the intrinsic functions can be useful for this.
    Ciao.

    REPLACE ==:BCO:== BY ==with BACKGROUND-COLOR== ==:FCO:== BY ==FOREGROUND-COLOR==.
    IDENTIFICATION DIVISION.
    program-id. GCxxxxx.
    
    ENVIRONMENT DIVISION.
    CONFIGURATION SECTION.
    ...
    ...
    
    01  wTimeX.
       03 wTimehh         pic  9(2).
       03 wTimemm         pic  9(2).
       03 wTimess         pic  9(2).
       03 wTimecc         pic  9(2).
    01 TimeString         pic  x(025).
    01 wTimeA             pic 9(8) value zero. *> hhmmsscc.
    01 wTimeB             pic 9(8) value zero. *> hhmmsscc.
    
    01  wTIME.
        03  wSTART-TIME        PIC 9(06).
        03  wSTART-TIME-R REDEFINES wSTART-TIME.
            05  wSTART-TIME-H  PIC 9(02).
            05  wSTART-TIME-M  PIC 9(02).
            05  wSTART-TIME-S  PIC 9(02).
        03  wEND-TIME          PIC 9(06).
        03  wEND-TIME-R REDEFINES wEND-TIME.
            05  wEND-TIME-H    PIC 9(02).
            05  wEND-TIME-M    PIC 9(02).
            05  wEND-TIME-S    PIC 9(02).
        03  wDURATION-TIME.
            05  wDUR-TIME-H    PIC 9(02).  *> OR PIC Z9.
            05  FILLER         PIC X VALUE ":".
            05  wDUR-TIME-M    PIC 9(02).
            05  FILLER         PIC X VALUE ":".
            05  wDUR-TIME-S    PIC 9(02).
        03  wSTART-SECONDS     PIC 9(09).
        03  wEND-SECONDS       PIC 9(09).
        03  wNUMBER-OF-SECONDS PIC 9(09).
    ...
    ...
    
        accept wTimeX from time
        string "Time start .. " wTimeX(1:2) ':' wTimeX(3:2) ':' wTimeX(5:2) delimited by size into TimeString end-string
        display TimeString at 006053 :BCO: wBackgr-Bco :FCO: wBackgr-Fco highlight end-display
        move wTimex(1:6) to wSTART-TIME
        COMPUTE wSTART-SECONDS = ( wSTART-TIME-H * 3600 ) + ( wSTART-TIME-M *   60 ) + ( wSTART-TIME-S )
    
    .....
    .....
    
     accept wTimeX from time
     string "Time......... " wTimeX(1:2) ':' wTimeX(3:2) ':' wTimeX(5:2) delimited by size into TimeString end-string
     display TimeString at 007053 :BCO: wBackgr-Bco :FCO: wBackgr-Fco highlight end-display
     move wTimex(1:6) to wEND-TIME
     COMPUTE wEND-SECONDS   = ( wEND-TIME-H   * 3600 ) + ( wEND-TIME-M   *   60 ) + ( wEND-TIME-S   )
    
     if wEND-SECONDS > wSTART-SECONDS *> if midnight is not passed
        COMPUTE wNUMBER-OF-SECONDS =  wEND-SECONDS                   - wSTART-SECONDS
     else
        COMPUTE wNUMBER-OF-SECONDS = (wEND-SECONDS + ( 24 * 3600 ) ) - wSTART-SECONDS
     end-if
    
     DIVIDE wNUMBER-OF-SECONDS BY 3600 GIVING wDUR-TIME-H REMAINDER wNUMBER-OF-SECONDS
     DIVIDE wNUMBER-OF-SECONDS BY   60 GIVING wDUR-TIME-M REMAINDER wDUR-TIME-S
     string "Duration..... " wDURATION-TIME delimited by size into TimeString end-string
     display TimeString at 008053 :BCO: wBackgr-Bco :FCO: wBackgr-Fco highlight end-display
     ...
     ...
    
     
  • Jack Tearle

    Jack Tearle - 2022-02-08

    If you are on a Linux system, you might consider using the 'time' command. It gives output like:
    jack@ps:~$ time date
    Mon 07 Feb 2022 08:29:44 PM EST

    real 0m0.002s
    user 0m0.001s
    sys 0m0.001s
    jack@ps:~$

     

Anonymous
Anonymous

Add attachments
Cancel