BrianGarland / blog

This is my blog
1 stars 0 forks source link

Converting Date and Time data types in RPGLE #8

Open BrianGarland opened 3 years ago

BrianGarland commented 3 years ago

If your application has been around for a while you probably have dates and times stored in some tables as numeric variables.

But, you use date and time data types for variables in your RPGLE programs to make the math easier, right?

There are many ways to convert from one data type to another. In the past developers have used data structures with overlapping subfields, or converting to character and then using substring.

Here are some examples of how to do this in a minimum amount of code. The examples work on IBM i 7.1 and newer.

// convert a date to a number in YYYYMMDD format
numeric8 = %DEC(date_variable:*ISO);
// extract the date from a timestamp in YYYYMMDD format
numeric8 = %DEC(%DATE(timestamp_variable):*ISO);
// convert a time to a number in HHMMSS format
numeric6 = %DEC(time_Variable:*HMS);
// extract the time from a timestamp in HHMMSSD format
numeric6 = %DEC(%TIME(timestamp_variable):*HMS);
JDubbTX commented 3 years ago

Hi Brian - just heard your interview on the incredible i podcast.

Thank you for this timely post - In my shop we are rewriting some very old date arithmetic programs originally written in Synon /2e into an RPGLE service program that will use many of these built in functions.

Our shop uses dates in CYYMMDD format (where 0 in the century byte represents '19' and 1 in the Century byte represents '20').

We will likely be using the %Months built in function to add a months to a date, but not after converting the numeric value to a date field. I thought about using embedded SQL to do the date arithmetic, but it doesn't support our date format, where rpgle does with its *cymd format. Just shows that sometimes RPG is still best for some things :)

BrianGarland commented 3 years ago

@JDubbTX , thanks for the comment. CYYMMDD is a bit of an od format. I've had to deal with it in the past.