16
Sat, Nov
2 New Articles

The Convert Date and Time Format

APIs
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

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 
BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: