How to Convert a Date to Unix Timestamp in COBOL

How to Convert a Date to Unix Timestamp in COBOL thumbnail
Reformat dates before passing to a different platform.

Most businesses maintain computer systems that pass critical information between different platforms. Mainframe systems support legacy code and large core systems, while smaller client server systems running Unix, Linux, or Windows based operating systems are used for client-server and web applications. To integrate the systems, the core computer system accommodates formatting differences when passing data to other platforms. COBOL is the preferred language for many legacy systems. With a few commands used in sequence, it translates a date to the Unix time-stamp.

Things You'll Need

  • COBOL system
  • Interface method
Show More

Instructions

    • 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.

Tips & Warnings

  • The integer function is useful to add or subtract days from a date without keeping track of the specific number of days in each month.

  • Code the utility or program on the Unix platform to recognize the field.

Related Searches:

References

  • Photo Credit computer belly image by MLProject from Fotolia.com

Comments

You May Also Like

Related Ads

Featured