fhg
Volume 9, Number 13 -- April 15, 2009

A Bevy of BIFs: Dealing with a Bad Date

Published: April 15, 2009

by Susan Gantner

In an earlier tip, I talked about how %Date simplifies our logic by converting our "date" values stored in numeric or character fields into true dates. In that tip, I mentioned that using %Date could potentially result in an error message in the event your "date-like" fields contain an invalid date value, such as zero or blank or a month value of 25.

Here I'll cover how to avoid getting run time errors due to bad date values. Since many of the "date-like" fields we deal with in our applications come from screens, database fields, HTML or XML documents, etc., converting them to date fields safely can be problematic.

There are two primary ways to avoid run time errors due to bad date values. You can either test the value before you attempt to convert it, or you can monitor for an error condition and deal with it if it occurs. Both are relatively simple to do. Choosing between those two options depends on whether you are optimistic or pessimistic about the chances of the field values in question being valid.

If you think most of the date-like fields you will process are likely to contain valid values the majority of the time, then your best choice may be to use the optimistic approach of monitoring for bad values. That way, your program doesn't run any "extra" logic to test the contents of the field. It only runs the extra logic if and when the data is found to be bad. With the pessimistic approach, your program needs to run the testing logic for every field every time, which will be slower, especially in cases where only a small portion of the data is bad. But I suspect it's the approach most RPGers would think of using first.

Let's be pessimistic in our first example. We're assuming that a high proportion of input dates will be bad, so we'll test them using RPG's TEST operation code. When using it with dates, we need two operation code extenders: "D" to specify that it's a date value we're testing for and "E" to specify that we'll use %Error to determine the result of the test. "Factor 1" contains the format the data in the "date-like" field is expected to be and "Result" contains the field being tested. (If you're not coding in /Free, you should note that there is no Factor 2.) The code looks something like this:

  Test(DE)*YMD Number;
  If %Error;
    If Number = 0;
      OutputAge = 'Unknown';
    Else;
      OutputAge = 'Error';
    EndIf;
  Else;  
    OutputAge = %Char(%Diff( %Date() : %Date(Number:*YMD) : *Years));
  EndIf;

Now for the optimistic approach. Using MONITOR, I'll simply run the logic I need to run to do the date calculation. If my optimism is well founded, most of the time the calculation will run without error and without any pre-testing of the field value. I'll monitor for the exceptional cases by comparing to a status code that means a bad date value was encountered. That status code happens to be 112. (You can find status code values in the ILE RPG Reference manual in Chapter 5.) In this example, I'm including a D spec that defines the named constant that I'm referencing in the On-Error statement. I could have simply coded the value 112 in the logic, but I don't want programmers coming after me needing to look up what "112" means!

D BadDateValue    C                   112

   Monitor;
     OutputAge = %Char(%Diff( %Date() : %Date(Number:*YMD) : *Years));
   On-error BadDateValue;
     If Number = 0;
        OutputAge = 'Unknown';
     Else;
        OutputAge = 'Error';
     EndIf;
   EndMon;

So, that's how you can deal with bad dates in your RPG code. Wouldn't it be nice if dealing with bad dates in real life were as simple?


Susan Gantner is one of the most respected System i gurus in the world and is one of the co-founders of System i Developer, an organization dedicated to RPG, DB2, and other relevant software technologies for the System i platform that hosts the new RPG & DB2 Summit conference. Gantner, who has worked in IBM's Rochester and Toronto labs, left IBM to focus on training OS/400 and i5/OS shops on the latest programming technologies. She is also a regular speaker at COMMON and other user groups. Send your questions or comments for Susan to Ted Holt via the IT Jungle Contact page.


RELATED STORIES

A Bevy of BIFs: Getting a Date is Easy with %Date

A Bevy of BIFs: Look Up to %LookUp



                     Post this story to del.icio.us
               Post this story to Digg
    Post this story to Slashdot


Sponsored By
HELP/SYSTEMS

                                                 SEQUEL
                                                 IBM® System i® Data Access Made Easy

                                              · Complete management access to critical data
                                              · Easy to use by IT and end users
                                              · Automated data access and display
                                              · Comprehensive BI package: reports, tables,
                                                 key performance indicators, and dashboards
                                              · System i-centric for real-time data analysis
                                              · Expert support and training

Click here for a FREE Information Kit!


Senior Technical Editor: Ted Holt
Technical Editor: Joe Hertvik
Contributing Technical Editors: Edwin Earley, Brian Kelly, Michael Sansoterra
Publisher and Advertising Director: Jenny Thomas
Advertising Sales Representative: Kim Reed
Contact the Editors: To contact anyone on the IT Jungle Team
Go to our contacts page and send us a message.

Sponsored Links

MKS:  FREE white paper: What Do IBM i Developers Want Out of Their ALM Software?
S4i Systems:  Say YES to DASD-Plus. Disk management starting at $350
COMMON:  Join us at the 2009 annual meeting and expo, April 26-30, Reno, Nevada


 

IT Jungle Store Top Book Picks

Easy Steps to Internet Programming for AS/400, iSeries, and System i: List Price, $49.95
The iSeries Express Web Implementer's Guide: List Price, $49.95
The System i RPG & RPG IV Tutorial and Lab Exercises: List Price, $59.95
The System i Pocket RPG & RPG IV Guide: List Price, $69.95
The iSeries Pocket Database Guide: List Price, $59.00
The iSeries Pocket SQL Guide: List Price, $59.00
The iSeries Pocket Query Guide: List Price, $49.00
The iSeries Pocket WebFacing Primer: List Price, $39.00
Migrating to WebSphere Express for iSeries: List Price, $49.00
Getting Started With WebSphere Development Studio Client for iSeries: List Price, $89.00
Getting Started with WebSphere Express for iSeries: List Price, $49.00
Can the AS/400 Survive IBM?: List Price, $49.00
Chip Wars: List Price, $29.95


 
The Four Hundred
Power vs. Nehalem: Time to Double Up and Double Down

The IBM-Sun Saga Continues--Or Rather, Doesn't

Clone Memory Maker Dataram Buys Rival MMB

Lawson Ekes Out Profit in Q3, Partners to Peddle in Quebec

Automation: Monitoring the Monitors Isn't It

Four Hundred Stuff
Fujitsu Introduces RPG to .NET Application Modernization Service

LANSA Adds Refinements to BPI Product

SkyView Adds PCI Checks to Risk Assessment Tool

SPSS Changes Data Miner's Name, Drops System i Support

So Long ASNA--It's BluePhoenix System i Division From Now On

Four Hundred Monitor
Four Hundred Monitor's
Full iSeries Events Calendar

System i PTF Guide
April 11, 2009: Volume 11, Number 15

April 4, 2009: Volume 11, Number 14

March 28, 2009: Volume 11, Number 13

March 21, 2009: Volume 11, Number 12

March 14, 2009: Volume 11, Number 11

March 7, 2009: Volume 11, Number 10

TPM at The Register
Big-iron brains powers Schooner appliance power

Dell adds VC hotshot to board

IBM serves System S streaming super

Sun software - Does it make diddly?

Nehalem aces OLTP test on HP iron

Cisco shells out $105m for Tidal

Microsoft puts Sun's Tremblay in SiArch

Novell's openSUSE does ARM Linux

Sun Sparc guru splits for Redmond

Sun revs VirtualBox to 2.2

Double-Take expands beyond server replication

Blade server standards coming this year

Apple plays catchup with Nehalem EP-powered Xserves

Egenera grooms Nehalem EP blades

THIS ISSUE SPONSORED BY:

Help/Systems
Vision Solutions
WorksRight Software


Printer Friendly Version


TABLE OF CONTENTS
A Bevy of BIFs: Dealing with a Bad Date

A Bevy of BIFs: %CHAR, %EDITC and %EDITW

Admin Alert: And /QOpenSys and /QOpenSys and /QOpenSys and. . .

Reader Feedback: Programming E-mail Notification When a Robot/SCHEDULE Job Fails

Four Hundred Guru

BACK ISSUES

From the IT Jungle Forums
PHP CLI Call

Perl, PHP, and/or ZendCore

batch printing PDF files from RPG program

Using db2_connect in PHP on iSeries

How to return value from CL program?

ADO.NET/IBM.Data.DB2.iSeries/ iDB2Connection

Order by alias names




 
Subscription Information:
You can unsubscribe, change your email address, or sign up for any of IT Jungle's free e-newsletters through our Web site at http://www.itjungle.com/sub/subscribe.html.

Copyright © 1996-2009 Guild Companies, Inc. All Rights Reserved.
Guild Companies, Inc., 50 Park Terrace East, Suite 8F, New York, NY 10034

Privacy Statement