Sei sulla pagina 1di 5

MC Press Online

The Convert Date and Time Format


Contributed by Craig Pelkie Friday, 30 April 1993 Last Updated Friday, 30 April 1993

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.

http://www.mcpressonline.com

Powered by Joomla!

Generated: 28 August, 2008, 23:55

MC Press Online

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
http://www.mcpressonline.com Powered by Joomla! Generated: 28 August, 2008, 23:55

MC Press Online

Number 1

Description

Type

Usage

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) Input variable (see Figure 2) CHAR(*) Input Input

2 3

Output format (same as Input format, *CURRENT not allowed) Output variable (see Figure 2)

CHAR(10)

4 5

CHAR(*)

Output I/O

Error code (see sidebar: The API Error CHAR(*) Code Parameter in "The Edit APIs," MC, April 1993)

The Convert Date and Time Format Figure 2 Date/Time Data Structure

Subfield 1

Description Century Digit: 0 - 20th century 1 - 21st century

From/To 1-1

Date, left justified. 2-7 Cannot be all blanks or zeros. For Julian dates, left justify, blank fill last position. Time (HHMMSS format) Milliseconds. 14 - 16 (This value cannot be blank.) 8 - 13

3 4

The Convert Date and Time Format Figure 3 Error Messages Sent by QWCCVTDT API

Message Message Text CPF1060 CPF1061 CPF1848 CPF1849 CPF1850 CPF24B4 CPF3CF1 Date not valid Time not valid Century digit & not valid Milliseconds value &1 not valid Format &1 not valid Severe error while addressing parameter list 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)


Powered by Joomla! Generated: 28 August, 2008, 23:55

http://www.mcpressonline.com

MC Press Online

* 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
http://www.mcpressonline.com Powered by Joomla! Generated: 28 August, 2008, 23:55

MC Press Online

* C C C C C C * C C C * C

CALL 'QWCCVTDT' PARM CVTDIF 10 PARM CVTDIV 16 PARM CVTDOF 10 PARM CVTDOV 16 PARM APIERR AEBYAV IFEQ 0 MOVELCVTDOV ENDIF ENDSR

Input format Input variable Output format Output variable API error code

No error bytes DTTMDS Move output var

http://www.mcpressonline.com

Powered by Joomla!

Generated: 28 August, 2008, 23:55

Potrebbero piacerti anche