Purusothaman Ramanujam

Purusothaman Ramanujam

Personal Blog

18 Jan 2012

COBOL Calendar and Date Functions

You need to know what date is 150 days from today (and this kind of stuff happens more often than you’d think)? Convert today to an integer date, add 150 to it and convert it back. No more checking which months you’re going through to see if it’s a 30 day or 31 day month. No more leap year calculations.

Some sample COBOL Date Example:

01  WS-TODAY         PIC 9(8).
01  WS-FUTURE-DATE   PIC 9(8).
....
MOVE FUNCTION CURRENT-DATE (1:8) TO WS-TODAY.
COMPUTE WS-FUTURE-DATE = FUNCTION INTEGER-OF-DATE (WS-TODAY) + 150

Probably the most useful intrinsic function is CURRENT-DATE which is a replacement for ACCEPT DATE and ACCEPT TIME. CURRENT-DATE is Y2K-compliant, having a 4-digit year. This function returns a 20-character alphanumeric field which is laid out as follows:

01  WS-CURRENT-DATE-FIELDS.
05  WS-CURRENT-DATE.
10  WS-CURRENT-YEAR    PIC  9(4).
10  WS-CURRENT-MONTH   PIC  9(2).
10  WS-CURRENT-DAY     PIC  9(2).
05  WS-CURRENT-TIME.
10  WS-CURRENT-HOUR    PIC  9(2).
10  WS-CURRENT-MINUTE  PIC  9(2).
10  WS-CURRENT-SECOND  PIC  9(2).
10  WS-CURRENT-MS      PIC  9(2).
05  WS-DIFF-FROM-GMT       PIC S9(4).

So not only can you get the time down to the millisecond, but you can get the difference between your time and Greenwich Mean Time.

The function is used in a MOVE:

MOVE FUNCTION CURRENT-DATE TO WS-CURRENT-DATE-FIELDS

The other intrinsic date functions deal with converting between either Gregorian dates or Julian dates and an internal Integer format. This Integer format is simply the number of days since some predetermined, fixed date like 1/1/0001. These four conversion functions are:

Convert from Gregorian to Integer formats

COMPUTE WS-INTEGER-DATE = FUNCTION INTEGER-OF-DATE (WS-DATE)

Convert from Integer to Gregorian formats

COMPUTE WS-DATE = FUNCTION DATE-OF-INTEGER (WS-INT-DATE)

Convert from Julian to Integer formats

COMPUTE WS-INTEGER-DATE = FUNCTION INTEGER-OF-DAY (WS-JUL-DATE)

Convert from Integer to Julian formats

COMPUTE WS-JULIAN-DATE = FUNCTION DAY-OF-INTEGER (WS-INT-DATE)
  • All Gregorian and Julian dates are expected to have 4-digit years.

  • COMPUTE is OK because we’re only using integers here.

How many days between two dates?

COMPUTE WS-DAYS = FUNCTION INTEGER-OF-DATE (WS-DATE-1) - 
                        FUNCTION INTEGER-OF-DATE (WS-DATE-2)

Converting between Gregorian and Julian formats used to be a pain also. Now it’s easy as below.

COMPUTE WS-DATE = FUNCTION DATE-OF-INTEGER (FUNCTION INTEGER-OF-DAY (WS-JULIAN))

Categories