Date handling in RPG3

2290 pts.
Tags:
RPG ILE
I want to ask how can I match two dates. I have been doing programming in RPG ILE which was quite easy to handle dates. Now I have been given assignment of RPG3. I have created a DS with 4 subfields of Century, year,month and day I have taken input from user and pass it to DS now should I manually handle either user input is date or not.

Answer Wiki

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

Discuss This Question: 12  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
  • philpl1jb

    Working with dates in RPG/400 - RPG III is infact a miserable waste of time.  Since you're locked into RPG III for the program you're working on, I would recommend that you make and RPGILE datefmt program with various functions and call it from the RPG code.

    You would pass it a date and a date format, mode, a field for days to add/subtract and a return date and format.

    it would return valid/invalide, and output date if required.

    50,860 pointsBadges:
    report
  • ToddN2000
    I agree with philpl1jb. Call an ILE program from your RPG3 program.
    It will save you a lot of time and headaches. As a side note, have you tried converting the RPG3 code to ILE using CVTRPGSRC ??
    15,080 pointsBadges:
    report
  • hunshabbir7
    I have read many comments like you have given. I want to know what people in 80s and 90s were doing with dates. How they handle it? I want to have some practice so if I am asked to amend some code so I could understand it first and then make changes accordingly. Second thing is i hesitate to convert it to ILE code if some problem occurs then there is no one to fix it on the spot.I am just new to this technology so beg your pardon for such question.
    2,290 pointsBadges:
    report
  • philpl1jb

    Dates were a real problem.

    - there was a math trick used to turn dates around (mmddyy to yymmdd) or vice versa by multiplying by a big number .. RPG drops the overflow

    - checking dates .. check the century / year between some range of values . check the month between 1 and 12. check the day based on the month .. check for leap year.

    Adding / subtracting days, months .. add days if days exceed number of days in that month add a month correct days .. if months > 12 add 1 to year subtract 12 from months.

     All very messy code, all unnecessary with RPGLE.

    50,860 pointsBadges:
    report
  • bvining
    Using just RPG3 another, fairly easy, option would be to call the Convert Date and Time Format (QWCCVTDT) API. As your data structure appears to be already formatted as CYYMMDD you could add nine bytes for some arbitrary time and tell the API to convert the date to some format. While you don't really care about any date format conversion QWCCVTDT will, as part of its operation, also validate that what you have as an input to the API is a valid date. This will provide date validation through the year 2899 -- and all using just RPG3.
    6,620 pointsBadges:
    report
  • hunshabbir7
    Thanks a lot bvining, you saved me. I made a demo RPG program in which I can convert date from one format to another and also it is validating date. One problem is when I give a wrong date the program crashes. Message ID is CPF1060 i.e. date not valid. How can I catch it in RPG3. I have read many articles but couldn't succeeded yet.
    Thanks in advance 
    2,290 pointsBadges:
    report
  • bvining
    The fifth parameter when calling QWCCVTDT is the Error Code parameter. This parameter is a data structure that allows you to control how errors are returned to your program.

    The first field, named Bytes Provided, is a 4-byte binary (9b 0 in RPG3) value that you provide. This value indicates how large the Error Code parameter is. If you provide a value of 0 then you're telling the API to return any error as an escape message and your program (normally) dies/crashes.

    In the data structure you can define a second 4-byte binary field immediately following the Bytes Provided field. This field is often called Bytes Available. If you now initialize Bytes Provided to 8 (rather than 0) then the API will not send an escape message. Rather it will set the Bytes Available field to a non-0 value indicating that error information is available.

    In the data structure you can define a third field as a 7-byte alphanumeric (7a). This is often referred to as Exception ID. If you now set Bytes Provided to 15 (to reflect that you have a Bytes Provided field, a Bytes Available field, and an Exception ID field) then, if there's an error, Bytes Available will be set to a non-0 value and the Exception ID field will tell you what the message ID (CPF1060) is.

    Make the data structure bigger and you can get even more information on the failure.

    If you have the QSYSINC library installed on your system (option 13 of the i operating system) then you will find that the Error Code data structure is already declared for you. It's in member QUSEC of QSYSINC/QRPGSRC.

    The Knowledge Center can provide additional information on the Error Code parameter.
    6,620 pointsBadges:
    report
  • hunshabbir7
    Thanks bvining for giving in detail information. Actually I was initializing byte provided and byte available data structure entries to zero. They were creating problem. When I reassigned values as you suggested they work just fine. Now my program is stable. One other thing (if you can suggest) I have two date fields one is "From" and the other one is "To". I have to validate them that from date shouldn't be greater then to date. After validating date from QWCVVTDT I compared my dates data structure entries manually. It is working ok. If there is some other better solution to this problem.

    Thanks again. 
    2,290 pointsBadges:
    report
  • bvining
    Given that your from and to dates are stored in century/year/month/day sequence then a straight compare of FromDate <= ToDate should take care of it. 
    6,620 pointsBadges:
    report
  • hunshabbir7
    yes you are right. Actually I take input from user in format ddmmyyyy. I was comparing it. It means in code I should change its format to yyyymmdd and then compare dates?
    2,290 pointsBadges:
    report
  • bvining
    Yes. And then you could even be using QWCCVTDT for its conversion support (in addition to the validation function).
    6,620 pointsBadges:
    report
  • hunshabbir7
    Thanks bvining
    2,290 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