Brief: Converting a date from one format to another is a frequently required programming task. In the past, a number of tricks and techniques have been used to accomplish this. V2R2 provides a callable API that you can use to convert dates to different formats.
One of the more troublesome data elements that we work with is date fields. The problem is that there are four different date formats supported by OS/400, and the format that you select to store a date field may not be the one needed to perform calculations or display or print the date. Programs that work with date fields usually include a date conversion routine or use the insidious technique of multiplying by 10000.01, or some such number. CL provides us with the Convert Date (CVTDAT) command. However, CVTDAT is impractical when thousands of records must be processed in an RPG program.
As part of the V2R2 system application program interfaces (APIs), IBM has provided and documented a program (QWCCVTDT) that can be called to perform date conversions. At first glance, this may seem unnecessary, especially if you are satisfied with your current date conversion techniques. But there are several advantages to using the API that you should consider:
o The API can convert any date format into any other date format.
o The converted format can also include the current time.
o The API validates the date, time, century and date format fields, and
returns an error identification to your program if it detects any errors.
Ready, Set...
You must set five parameters (shown in 1) before calling program QWCCVTDT.
You must set five parameters (shown in Figure 1) before calling program QWCCVTDT.
Input format is used to specify the format of the date you want to convert. The date to be converted is placed in the input variable field. The input variable is a character variable with the format shown in 2.
Input format is used to specify the format of the date you want to convert. The date to be converted is placed in the input variable field. The input variable is a character variable with the format shown in Figure 2.
If you use *CURRENT for the input format, the machine clock's current date and time is used as the input variable; any value that you enter is ignored. If the input format is *DTS (system timestamp), only the first eight bytes of the input variable are used. (See the accompanying sidebar, "System Timestamps," for details.) For other formats, the first 16 bytes of the input variable are used. Before calling the API, you must set the century digit, date, time and milliseconds. Because the API does not convert the time and milliseconds fields, you can simply set those to zero if you want to convert the date only. For input formats other than *CURRENT, the date and time values that you supply in the input variable must be valid. If the date or time is not valid, an error is returned from the API.
Output format can be any of the formats shown for the input format, with the exception of *CURRENT. The output format can be the same as the input format. If it is, the input variable is not converted, but simply copied to the output variable. This allows you to use the API's date validation without actually converting your input date. The converted date and time value is placed into the output variable, using one of the formats shown in 2. If you use *CURRENT as the input format, the current system time is supplied in the output variable's time and milliseconds fields.
Output format can be any of the formats shown for the input format, with the exception of *CURRENT. The output format can be the same as the input format. If it is, the input variable is not converted, but simply copied to the output variable. This allows you to use the API's date validation without actually converting your input date. The converted date and time value is placed into the output variable, using one of the formats shown in Figure 2. If you use *CURRENT as the input format, the current system time is supplied in the output variable's time and milliseconds fields.
If any errors exist, the API can return the error information to you in the error code parameter. Error messages that may be returned are shown in 3. For a description of the error code parameter, see "The API Error Code Parameter" sidebar which appeared last month in "The Edit APIs."
If any errors exist, the API can return the error information to you in the error code parameter. Error messages that may be returned are shown in Figure 3. For a description of the error code parameter, see "The API Error Code Parameter" sidebar which appeared last month in "The Edit APIs."
Go!
The sample code in 4 shows data structures for the API error code and for the standard date/time. For illustration purposes, a constant '040193' is converted from MMDDYY format to YYMMDD format. The API program is called in the #CVTDT subroutine, which can be used as a standard conversion subroutine in your programs. The five required parameters are assigned values before executing the subroutine.
The sample code in Figure 4 shows data structures for the API error code and for the standard date/time. For illustration purposes, a constant '040193' is converted from MMDDYY format to YYMMDD format. The API program is called in the #CVTDT subroutine, which can be used as a standard conversion subroutine in your programs. The five required parameters are assigned values before executing the subroutine.
The time and milliseconds fields are cleared before calling the API. There is no point in setting these fields, unless you are converting to a system timestamp field. However, because the values supplied in those fields must be valid, they are initialized prior to the call.
The #CVTDT subroutine clears the output variable and the error code, then sets the length of the bytes-provided subfield of the error code to indicate that the API should return error information in the error code. The QWCCVTDT program is called with the five parameters.
If there are no errors, the bytes-available field (AEBYAV) is zero. The converted output variable value is moved to the standard date/time data structure, after which the components are available to your program. If an error occurs, the standard date/time data structure has the same value as it did prior to calling the API. You can use the exception ID field (AEEXID) to determine the cause of the error, and possibly code an error correction routine.
You should consider using the QWCCVTDT API when you need to either convert or validate a date. In addition to converting from and to any valid date format, the error detection capability of the API makes it more useful than customary date conversion routines. IBM has practically done all the work for you.
Craig Pelkie can be contacted through Midrange Computing.
The Convert Date and Time Format
Figure 1 Parameters Used With QWCCVTDT API
Number Description Type Usage 1 Input format CHAR(10) Input * CURRENT Current machine date/time * DTS System date/time stamp *JOB Format in DATFMT job attribute *SYSVAL Format in QDATFMT system value *YMD YYMMDD format *MDY MMDDYY format *DMY DDMMYY format *JUL Julian format (YYDDD) 2 Input variable (see Figure 2) CHAR(*) Input 3 Output format (same as Input format, CHAR(10) Input *CURRENT not allowed) 4 Output variable (see Figure 2) CHAR(*) Output 5 Error code (see sidebar: The API Error CHAR(*) I/O Code Parameter in "The Edit APIs," MC, April 1993)
The Convert Date and Time Format
Figure 2 Date/Time Data Structure
Subfield Description From/To 1 Century Digit: 1 - 1 0 - 20th century 1 - 21st century 2 Date, left justified. 2 - 7 Cannot be all blanks or zeros. For Julian dates, left justify, blank fill last position. 3 Time (HHMMSS format) 8 - 13 4 Milliseconds. 14 - 16 (This value cannot be blank.)
The Convert Date and Time Format
Figure 3 Error Messages Sent by QWCCVTDT API
Message Message Text CPF1060 Date not valid CPF1061 Time not valid CPF1848 Century digit & not valid CPF1849 Milliseconds value &1 not valid CPF1850 Format &1 not valid CPF24B4 Severe error while addressing parameter list CPF3CF1 Error code parameter not valid
The Convert Date and Time Format
Figure 4 RPG Program XCP002RG, Using QWCCVTDT
* * * CRTRPGPGM PGM(XXX/XCP002RG) SRCFILE(XXX/QRPGSRC) * * IAPIERR DS ********************************************************************* * Standard API error data structure * * AEBYPR - Bytes provided for error information * (0 or 8 - 32,783) * AEBYAV - Bytes of error information available * AEEXID - Exception ID of error * AERESV - Reserved * AEEXDT - Exception data ********************************************************************* I B 1 40AEBYPR I B 5 80AEBYAV I 9 15 AEEXID I 16 16 AERESV I 17 116 AEEXDT IDTTMDS DS ********************************************************************* * Standard date/time data structure (Refer to Figure 2) * * DTCENT - Century digit * 0 - 20th century * 1 - 21st century * DTDATE - Date, left-justified. Value cannot be all * blanks or zeros. For julian dates, left * justify and fill last digit with a blank. * DTTIME - Time, HHMMSS format * DTMILS - Milliseconds ********************************************************************* I 1 1 DTCENT I 2 7 DTDATE I 8 130DTTIME I 14 160DTMILS * Load input variable C MOVEL'0' DTCENT Century C MOVEL'040193' DTDATE Date C CLEARDTTIME Time C CLEARDTMILS Milliseconds C MOVELDTTMDS CVTDIV * Load convert from and to formats C MOVEL'*MDY' CVTDIF C MOVEL'*YMD' CVTDOF * Perform conversion C EXSR #CVTDT C CVTDOV DSPLY ANSWER 1 * Output variable CVTDOV now contains the converted date C MOVEL*ON *INLR ********************************************************************* * Call QWCCVTDT API * * Before calling, set values for these fields: * * CVTDIF - Input format (convert from) * CVTDIV - Input variable * CVTDOF - Output format (convert to) * * Values are returned in these fields: * CVTDOV - Output variable * APIERR - API error code data structure ********************************************************************* C #CVTDT BEGSR C CLEARCVTDOV Output variable C CLEARAPIERR API error code C Z-ADD116 AEBYPR Bytes provided * C CALL 'QWCCVTDT' C PARM CVTDIF 10 Input format C PARM CVTDIV 16 Input variable C PARM CVTDOF 10 Output format C PARM CVTDOV 16 Output variable C PARM APIERR API error code * C AEBYAV IFEQ 0 No error bytes C MOVELCVTDOV DTTMDS Move output var C ENDIF * C ENDSR
LATEST COMMENTS
MC Press Online