Refitting Migration to Look Like Emulation
Celebrate independence this weekend

Date format variable help for MPE/iX

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

Also note that since you're asking for the value of HPYYYYMMDD three different times, there's the tiniest chance that if you executed this right at last part of the second at 23:59:59, then the value could roll over to a new date in the 2nd or 3rd call, and you could possibly get the wrong date, or even year. Unlikely, I know. But to avoid that, you'd want to capture the current date once, then operate on that, as in:

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;

Comments