UDATE may not be the right way to get the current date, but old habits are hard to break.
When you need to put a date and a time together in your RPG IV program, be sure you understand how to use UDATE, *DATE, the TIME opcode , %DATE, %TIME, and %TIMESTAMP.
History of UDATE Misuse
In early versions, RPG provided the UDATE reserved word as an easy way to refer to the six-digit job date. The vast majority of RPG date code I have run into revolves around UDATE, and I expect this holds true in many shops. UDATE makes it really simple to put a date in a report heading or on a display screen. *DATE is a reserved word in later RPG versions that provides similar simplicity with an eight-digit date.
Where some developers ran into trouble was when they needed to put a date and a time together. In my experience, this most frequently occurred when time-stamping a database record, but also occasionally in report headings or on display screens. Historically, database timestamps consisted of two fields: a date field and a time field. Most of the existing code to create these two fields works most of the time, but every now and then the operational environment changes and the results are just plain wrong.
Given that
What many failed to consider is that the value returned by UDATE or *DATE is static. The
In contrast, the TIME opcode and the %DATE, %TIME, and %TIMESTAMP BIFs return dynamic data from the system clock. Time values are always changing, and the date value changes at
Pairing an unchanging Job date with a changing time works in most situations. As long as the job does not continue running past
A Recent Bad Example
This recently came to my attention and provoked this article. The shop floor in a local company runs two shifts five days a week from
It turned out the code that puts the timestamps in the transactions uses the UDATE and TIME opcode combination.
The floor workers sometimes (often?) forget to end their 5250 sessions, so the regular Monday through Friday day-end process always ends the QINTER subsystem at
Maybe QINTER should be brought down every evening. Maybe inactive 5250 sessions should time out after eight hours. Maybe there are other ways to stop jobs running over
Nevertheless, with RPG IV, it is a trivial effort to create timestamps that will always be correct, regardless of when the job starts or how long it runs, so there is no excuse for timestamp code that doesn't work all the time.
When to Use UDATE
Use UDATE with the clear understanding that it might not be the current date. It might be the prior day if the job started before
If you want the current date, use %DATE(), recognizing that this really is the current date and it changes at
When Not to Use UDATE
Do not use UDATE when you want to pair a date with a time. If you are designing a new system with a timestamp in the records, consider using a field with a timestamp data type, type Z. Populate it with the %TIMESTAMP() BIF.
If you are working with a legacy system that has separate date and time fields for the time stamp, use code like this:
D TS s z
/FREE
TS = %timestamp();
CHGDTE = %dec(%date(TS):*YMD);
CHGTME = %dec(%time(TS):*HMS);
Granted, you could just use %DATE() followed by %TIME(), but there is a remote chance that the system clock could roll to the next day between the two instructions.
(If you need help working with legacy dates, see "RPG IV Legacy Dates Cheat Sheet.")
Conclusion
UDATE isn't inherently evil. Just be sure you understand what it reflects when you use it, and seriously consider using a BIF when there is a time involved.
LATEST COMMENTS
MC Press Online