Date format variable help for MPE/iX
June 29, 2016
What would be the easiest way to get a variable date in the format "06/29/16" on a MPE/iX 7.0 and 7.5?
Michael Anderson replies
First echo:
echo ![str("!hpyyyymmdd",5,2)]/![str("!hpyyyymmdd",7,2)]/![str("!hpyyyymmdd",3,2)]
Then setvar:
setvar mydate '![str("!hpyyyymmdd",5,2)]/![str("!hpyyyymmdd",7,2)]/![str("!hpyyyymmdd",3,2)]'
(Note the usage of single-quote and double-quote in the setvar command.)
Barry Lake adds
Please note that the HPYYYYMMDD variable is already a string variable:
Frodo: calc typeof(HPYYYYMMDD)
2, $2, %2
So you don't have to dereference it with ! inside double quotes. In other words, the following works just as well, is easier to read, and might even execute a bit faster:
Frodo: echo ![str(hpyyyymmdd,5,2)]/![str(hpyyyymmdd,7,2)]/![str(hpyyyymmdd,3,2)]
06/29/16
Frodo: setvar my_date hpyyyymmdd
Frodo: echo ![str(my_date,5,2)]/![str(my_date,7,2)]/![str(my_date,3,2)]
06/29/16
Stan Sieler adds
Kudos to Barry and Michael for apparently correctly guessing that our manager was interested in a CI-based solution. Next time, you might want to make that clear. I was thinking along the lines of SPL/Pascal/COBOL and the CALENDAR intrinsic, as well as whether I should ask the obvious questions or not. Of course, the easiest method is: ECHO 06/29/16
Any code that wants a coherent date and time using separate sources (e.g., CLOCK and CALENDAR intrinsics, or HPTIMEF (#1)) needs to be aware of midnight crossing. Thus, I use a helper function in Pascal like:
procedure get_date_time (var cal : calendar_type; val clk : clock_type);
begin
repeat
begin
cal := calendar;
clk := clock;
end
until cal = calendar;
{avoid problems if ’clock’ called just after midnight}
end;