System i date conversions in RPG

Andrew Borts
Date conversion in RPG is something that we do without too much hassle. Years ago, we'd use wacky calculations, which are now deemed harmful to proper processing (programming general warning!) The old multiply by 100.0001 is a bad way to go. Here's the reasoning why you need to eliminate all of these from your repertoire of conversion tools.

  • This produces a hiccup while processing --because the end result of that calculation creates an exception error.

  • The field gets violently (best word for it) truncated

  • The system stops whatever it's doing, thinks really hard over the answer, decides on keeping the remaining numbers from the calculation

  • The result is given to you after the server has been calmed down by years of OS monitor messages.

    Since the reaction to that calculation is kind of like a "train wreck" for processing, let's look at the alternative.

    Take a look at my data structure, and I'll get you converting dates in no time.

    Here's the way it works; you put the entire number into the appropriate original format. Let's convert from CYMD to MDY you'd Z-Add DATE into CYMD. Then Z-Add YY into cvYY. Two lines of code later, and you have MDY. Want to do the reverse – easy! Z-Add the DATE into MDY. Then take cvYY and Z-add that into YY. Use a window technique to extrapolate the century if you can (Window technique = If year (YY) < 60 then century (CC) = 20, ELSE century (CC) = 19).

    More Information

    Now you're shaking your head – two lines of code, no! The original date calculation may have been only one line of code, but since it causes so many exception errors while processing, it's slower then 100's of lines of code. The two innocuous lines of code we used to convert using my data structure are extremely fast.

    Now, let's do this in our CL programs. CVTDAT has been a wonderful part of our diet since the early days of the operating system.

    Let's convert some dates in CL.

    I grab my Job Date (define – the Job Date is different from the System Date (Note: even if they are the same) then use this CL command, but look – I can convert from *MDY (see my MDY DS entry from above isn't so original ) to *YMD or any other permutation. Please make note, that *CYMD isn't the result you may want. IBM used a century byte until they learned that we were going to be using a real date. So *CYMD for say 2/28/2007 would convert to 1070228 – not exactly friendly. *YYMD is going to be your friend here -- this format would return 20070228. Keep in mind that's if you set "To date separator" to *NONE.

    Like what you see? We'll calculate these next month. If you're excited about data calculations using CL, I highly recommend TAATOOLS, which is now available as a professional set of tools -- instead of being supplied with the operating system. There are a ton of handy commands - and last time I checked – licensed from Jim Sloan is about 30 cents a tool.

    About the author: Andrew Borts is webmaster at United Auto Insurance Group in North Miami, Fla. He is often a frequent speaker at COMMON and is past president of The Southern National Users Group, an iSeries-AS/400 user group based in Deerfield Beach, Fla.

    This was first published in March 2007

  • There are Comments. Add yours.

    TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

    REGISTER or login:

    Forgot Password?
    By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
    Sort by: OldestNewest

    Forgot Password?

    No problem! Submit your e-mail address below. We'll send you an email containing your password.

    Your password has been sent to:

    Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.