RPGLE DATE CALCULATIONS

115 pts.
Tags:
AS/400
RPGLE
RPGLE Date Calculation
I need to subtract 1 day from the system date and output it into a file in the format mmddyyyy. No separators. I cannot figure out how to use date functions like subdur w/o the field containing separaters on output. And I can't figure out how to use a DS to redefine it so I can exclude the separaters. Can anyone enlighten me?

Software/Hardware used:
AS400 OS V6.1 RPGLE

Answer Wiki

Thanks. We'll let you know when a new response is added.

There are quite a few ways of getting it done. Here’s an example that brings pretty much everything you mentioned into it:<pre>
H Debug

D TstDat s d datfmt(*usa)
D ds
D TstDat2 8s 0
D TstDat3 8 overlay( TstDat2 )

D OutDat s 8

C seton LR

C eval TstDat = %date()-%days(1)
C eval TstDat2 = %dec( TstDat )

C eval OutDat = TstDat3

C dump</pre>
Note that OutDat isn’t actually necessary. I only included it to show that the value of TstDat3 really is what the dump says that it is. You can use TstDat3 as it is.

The value of %date() by itself is the current system date, but it’s in *ISO format. After subtracting one day from it, the result is formatted as MM/DD/YYYY by using the definition of the TstDat field. We put that date through the %dec() function in order to get the numeric digits out of the date. This value is in a field defined as zoned decimal, so we can access that memory directly as character. And we do that simply by overlaying a character field on it.

BTW, if you have any choice at all, use the *ISO format (or at the very least as YYYYMMDD) instead of putting the result out as MMDDYYYY.

Tom
——————————————————————
Or you can skip the data structure conversion
<pre>
C eval OutDat = %char( %date()-%days(1) : *usa0 )
</pre>

BigKat

Discuss This Question: 10  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

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
  • TomLiotta
    Yes, I should have added examples of a couple alternatives. I got focused on the details mentioned in the question only because different steps highlight small pieces. I can't tell yet if a C-like approach of getting a thing done in a single line is "better" than breaking parts out for discussion. But It does seem best to show alternatives together, especially using the same variable names. The alternatives help to illustrate each other. Nice addition. Tom
    125,585 pointsBadges:
    report
  • BigKat
    well, you had done a great job of explaining everything step by step, I just added the piece that put it all together I like to use spacing and even breaking terms onto multiple lines to help make the "one-liners" more readable
    8,350 pointsBadges:
    report
  • KatanaBeach
    Thnks to both of you for the "enlightenment". :)
    115 pointsBadges:
    report
  • SteveFletcher
    Hmmm ... 1 Line versus 10 Lines. That's not an alternative, that's a no-brainer! ;)
    80 pointsBadges:
    report
  • MurrayInfoSys
    Do not for get your audience. I work in a shop with 24+ programmers. Of those, I am one of two that code in Free format. I showed a co-worker the single line code. Reaction: HUH? Remember, someone other than you may be tasked to change the program.
    940 pointsBadges:
    report
  • BigKat
    Not trying to be snarky, but... if they don't want to keep up with the developments in the language, then maybe they should be replaced with someone who does. Otherwise, why aren't we still using COMPs and GOTOs I hate when people complain that we are called a "legacy" system, but then they don't want to bother to learn the "new" stuff (FREE format has been around almost my entire career)
    8,350 pointsBadges:
    report
  • Splat
    Free-format hasn't been around for most of my career, but I've been incorporating it into my coding for about three years. It's adaptable and not difficult to learn. Then again, I remember the reactions I got at one client location when they saw what could be done with RPG IV. Very few shops encourage their programmers to learn new stuff.
    7,635 pointsBadges:
    report
  • TomLiotta
    1 Line versus 10 Lines. That’s not an alternative, that’s a no-brainer! I generally agree with most comments. Just for accuracy, though, I'll point out that it might be closer to "2 lines v. 6 lines". Some of the demo lines were only for the purpose of making a complete program, and those would need to be included in the shorter version for a complete comparison. Or, if all of those extra lines are excluded, the shorter version still needs some kind of D-spec. Regardless, the sentiment about keeping up with new techniques is extremely valid. Even if a technique isn't used, it should be understood. But in order to understand, the various pieces that go into often need to be viewed as discrete parts. The parts can be understood as discrete elements. Then the single-line version shows how those parts can be "glued together" into a complex whole. Sometimes only one of the "parts" is needed. If the single-line version is all that is known, it can be difficult figuring out how to extract just the part you want. In the original question, there was some confusion over how a DS might be used for an operation. It's not needed in the single-line version, but DS usage is important in itself. In some unusual cases, it might be performance-critical to reference a DS overlay instead of executing code that converts between data types. E.g., I have an app that needs to do thousands of various conversions per second on some heavily used systems. The app might run non-stop for weeks at a time, and it should stay down in the 3%-5% CPU range even under heaviest workload. I'll choose a DS overlay without thinking twice if it gets me the proper data format without needing code that's generated inside of a %bif. But that's an unusual case. It might not even apply to this particular date operation. Just adding comments for discussion. Tom
    125,585 pointsBadges:
    report
  • Junky
    Sorry about messing with the BigKat's answer. I wanted to see all of Tom's code so I clicked on revert. Didn't realize that it would repost it under my id. Anyway, I agree totally with you on keeping up with the latest trends. We are moving to Microsoft Dynamics AX (not by my choice) and it's amazing how close some of it is to free format RPG and imbedded SQL. There are a heck of a lot of differences too, but I have been able to be productive faster than I thought because of these similarities. Man am I going to miss the iSeries, but hopefully I'll still be employed when this is all over.
    95 pointsBadges:
    report
  • BigKat
    Tom's answer was VERY correct and complete (as usual :) ) for the question as stated by the asker. I was just adding the additional information that offered an alternative that was not covered. It wasn't meant to be better or worse (as Tom pointed out depending on load) just another option. :) no problem Junky, but AFAIK I left all of Tom's answer, just tagged on to it.
    8,350 pointsBadges:
    report

Forgot Password

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

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

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

Thanks! We'll email you when relevant content is added and updated.

Following