COBOL Intrinsic Functions

The newest flavor of COBOL, generically called 20x but released by IBM under the name of COBOL/370 (previous version was COBOL II, which followed COBOL 85 - explain that naming convention), includes a collection of built-in, or intrinsic, functions. This page will discuss two categories of these functions:

  1. Date Functions
  2. Numeric Functions
  3. Date Functions

    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
    

    But normally reference modification is used to only grab the part you want:

         * Get the current date in yyyymmdd format
            MOVE FUNCTION CURRENT-DATE (1:8) TO WS-TODAY
    
         * Get the current time in hhmmss format
            MOVE FUNCTION CURRENT-DATE (9:6) TO WS-TIME
    

    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-INTEGER-DATE)
    
         * Convert from Julian to Integer formats
            COMPUTE WS-INTEGER-DATE = FUNCTION INTEGER-OF-DAY (WS-JULIAN-DATE)
    
         * Convert from Integer to Julian formats
            COMPUTE WS-JULIAN-DATE = FUNCTION DAY-OF-INTEGER (WS-INTEGER-DATE)
    

    All Gregorian and Julian dates are expected to have 4-digit years.

    You might be asking just what in the heck would you want with this integer date. Well, with it calendar math is a breeze. 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. It's pretty automatic:

    	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 DATE-OF-INTEGER (FUNCTION 
                    INTEGER-OF-DATE (WS-TODAY) + 150)
    

    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:

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

    Top

    Numeric Functions

    It's doubtful that there will be much use for most of the numeric functions available. Some of the more useful ones:

           * Calculate the square root of a number
                COMPUTE WS-RESULT = FUNCTION SQRT (WS-NUMBER)
    
           * Get the remainder from dividing two integers
                COMPUTE WS-RESULT = FUNCTION MOD (WS-INTEGER-1, WS-INTEGER-2)
    
           * Get the remainder from dividing two non-integers
                COMPUTE WS-RESULT = FUNCTION REM (WS-NUMBER-1, WS-NUMBER-2)
    

    There are also geometric functions (SIN, COS, TAN), log functions (LOG, LOG10), FACTORIAL and even RANDOM for generating random numbers.


    Top