HealthLinks is your destination for reliable, understandable, and credible health information and expert advice that always keeps why you came to us in mind.

How to Convert a Date to Unix Timestamp in COBOL

104 64
    • 1). Define fields in working storage to store values.

      01 NUMBER-OF-DAYS PIC 9(09) VALUE ZEROES.

      01 CURRENT-DATE-FIELDS.

      05 CURRENT-DATE-VALUE.

      10 CURRENT-YY PIC 9(04) VALUE ZEROES.

      10 CURRENT-MM PIC 9(02) VALUE ZEROES.

      10 CURRENT-DD PIC 9(02) VALUE ZEROES.

      05 CURRENT-TIME-VALUE.

      10 CURRENT-HOUR PIC 9(02) VALUE ZEROES.

      10 CURRENT-MIN PIC 9(02) VALUE ZEROES.

      10 CURRENT-SEC PIC 9(02) VALUE ZEROES.

      10 CURRENT-MS PIC 9(02) VALUE ZEROES.

      01 SECONDS-IN-A-DAY PIC 9(05) VALUE 86400.

      01 SECONDS-IN-A-HOUR PIC 9(05) VALUE 3600.

      01 SECONDS-IN-A-MIN PIC 9(02) VALUE 60.

      01 UNIX-TIMESTAMP PIC 9(10) VALUE ZEROES.

    • 2). Find the current date and time using this COBOL function.

      MOVE FUNCTION CURRENT-DATE TO CURRENT-DATE-FIELDS.

    • 3). The Unix timestamp represents the number of seconds that have passed since January 1, 1970. Find the difference between the current date and January 1, 1970 by using the compute command and the integer-of-date function in a COBOL program.

      COMPUTE NUMBER-OF-DAYS = FUNCTION INTEGER-OF-DATE (CURRENT-DATE-VALUE) -

      FUNCTION INTEGER-OF-DATE ("19700101").

    • 4). Find the number of seconds that have passed since January 1, 1970 by calculating the number of seconds by day, hour, and minute and adding them together.

      COMPUTE UNIX-TIMESTAMP =

      (NUMBER-OF-DAYS * SECONDS-IN-A-DAY) +

      (CURRENT-HOUR * SECONDS-IN-A-HOUR) +

      (CURRENT-MIN * SECONDS-IN-A-MIN) +

      CURRENT-SEC.

Source...

Leave A Reply

Your email address will not be published.