The FIND DURATION function is used to calculate duration between:
The FIND-DURATION function returns an integer in the form of complete units of the specified duration. Any rounding is done downwards. The calculation of durations includes microseconds.
The function type is integer.
The function result is a nine-digit integer. If the function result is larger than 9 digits (999,999,999), a machine check occurs.
FUNCTION FIND-DURATION (argument-1 argument-2 argument-3 )
argument-1, argument-2
argument-3
Is a keyword that represents a duration. The valid Find Duration keywords are:
In order to determine the valid duration keywords, the following rules apply:
Example:
COMPUTE integer-1 = FUNCTION FIND-DURATION (date-3 date-4 MONTHS). COMPUTE integer-1 = FUNCTION FIND-DURATION (timestamp-1 date-5 MONTHS).
The ADD-DURATION function adds duration to a date, time, or timestamp item, and returns the modified item.
The function type is date-time.
The length of the returned value depends on the length of the date, time, or timestamp item specified in argument-1. The returned value will be truncated to the length of argument-1.
If a duration is added to a date item, the date returned must fall within a certain range:
If a duration is added to a 2-digit date item, the range is the same as for a 4-digit year, but the year in the value returned is truncated to 2 digits.
FUNCTION ADD-DURATION (argument-1 argument-2 argument-3)
argument-1
argument-2
Argument-2 is a keyword that represents a duration. The valid duration keywords are:
The duration keyword must be consistent with argument-1. For example, the duration keywords most obey the following rules:
argument-3
If a duration is added to the date, and the result is invalid, the date is adjusted. For example, if duration of 1 month is added to the date March 31, 1997, the result would be the invalid date April 31, 1997. This date would be adjusted to the valid date of April 30, 1997.
Example:
Adding months to a specific Date. MOVE FUNCTION ADD-DURATION (date-3 MONTHS 1)TO date-2. MOVE FUNCTION ADD-DURATION (date-3 MONTHS int-1 * 2) TO date-1. Adding years, months, and days to a specific Date MOVE FUNCTION ADD-DURATION (date-1 YEARS 1 MONTHS 5 DAYS 23) TO date-2.
If a duration is subtracted from a date item, the date returned must fall within a certain range:
If a duration is subtracted from a 2-digit date item, the range is the same as for a 4-digit year, but the year in the value returned is truncated to 2 digits.
FUNCTION SUBTRACT-DURATION (argument-1 argument-2 argument-3)
argument-1
argument-2
Argument-2 is a keyword that represents a duration. The valid durations are:
The duration keyword or conversion specifier used must be consistent with argument-1. For example, the duration keywords must obey the following rules:
argument-3
If a duration is subtracted from a date, and the result is invalid, the date is adjusted. For example, if the duration of 1 month is subtracted from the date May 31, 1997, the result would be the invalid date April 31, 1997. This date would be adjusted to the valid date of April 30, 1997.
Example:
Adding months to a specific Date. MOVE FUNCTION SUBTRACT-DURATION (date-1 MONTHS 1) TO date-2. MOVE FUNCTION SUBTRACT-DURATION (date-2 MONTHS 1 + 2 * 3) TO date-1. Adding years, months, and days to a specific Date MOVE FUNCTION SUBTRACT-DURATION (date-3 MONTHS 5 DAYS 1000) TO date-1.
The MOD function returns an integer value that is argument-1 modulo argument-2.
The function type is an integer.
The function result is an integer with as many digits as the shorter of argument-1 and argument-2.
FUNCTION MOD (argument-1 argument-2)
argument-1
Must be an integer.
argument-2
The following illustrates the expected results for some values of argument-1 and argument-2.
Example
Argument-1 | Argument-2 | Return |
11 | 5 | 1 |
-11 | 5 | 4 |
11 | -5 | -4 |
-11 | -5 | -1 |
Example: Subtract the specified number of months from an input date.
01 MONTH-END-DD PIC X(24) VALUE '312831303130313130313031'. 01 TBL-MONTH-END REDEFINES MONTH-END-DD. 03 TBL-MONTH-END-DAY OCCURS 12 TIMES. 05 TBL-MONTH-END-DD PIC 9(02). 01 WS-DATE. 02 YYYY PIC 9(4). 02 MM PIC 99. 02 DD PIC 99. MOVE INPUT-DATE TO WS-DATE DIVIDE WS-NO-OF-MONTHS BY 12 GIVING WS-NO-OF-YEARS REMAINDER WS-REMAINING-MONTHS SUBTRACT WS-NO-OF-YEARS FROM YYYY IF MM > WS-REMAINING-MONTHS COMPUTE MM = MM - WS-REMAINING-MONTHS ELSE SUBTRACT 1 FROM YYYY COMPUTE MM = 12 - WS-REMAINING-MONTHS END-IF IF ( FUNCTION MOD (YYYY, 400) = 0) OR ( FUNCTION MOD (YYYY, 4) = 0 AND FUNCTION MOD (YYYY, 100) NOT = 0 ) MOVE '29' TO TBL-MONTH-END-DD (3:2) ELSE MOVE '28' TO TBL-MONTH-END-DD (3:2) END-IF IF DD > TBL-MONTH-END-DAY (MM) MOVE TBL-MONTH-END-DAY (MM) TO DD END-IF
Mastering duration calculations in COBOL is crucial for handling date and time operations efficiently, especially in applications that deal with financial transactions and mainframe systems. By understanding how to find, add, subtract, and perform modular arithmetic on durations, COBOL developers can enhance their ability to work with temporal data effectively.
A well-maintained product backlog is crucial for successful product development. It serves as a single…
Incremental value to the customer refers to the gradual delivery of small, functional parts of…
A Product Market refers to the group of potential customers who might be interested in…
The Professional Agile Leadership - Evidence-Based Management (PAL-EBM) certification offered by Scrum.org is designed for…
The Professional Agile Leadership (PAL I) certification, offered by Scrum.org, is designed to equip leaders…
Choosing the right Scrum Master Certification depends on your current experience and career goals. If…