Free Format Alternatives to TIME

Q. In fixed format RPG IV, I used the TIME operation code to get the current date and time; but TIME is not supported by free format. You mentioned that the %DATE, %TIME, and %TIMESTAMP functions can replace TIME, but I’m not sure how to use them for this purpose.

In my programs, I create a data structure like this:

D                 DS 
D Dtime                         14  0 
D  Wdate                         8  0 Overlay(Dtime) 
D  Wtime                         6  0 Overlay(Dtime:*Next) 

I’ve been using TIME to assign a value to Dtime, then I use Wtime and Wdate to assign values to work fields. How can I do this in free format?

A. If you do not specify a value for the %DATE, %TIME, and %TIMESTAMP functions they will return the current date, time, or timestamp. Then, it’s a simple matter to convert that value to a number in your data structure. Here’s one way to do it:

D                 DS 
D Dtime                         14  0 
D  Wdate                         8  0 Overlay(Dtime) 
D  Wtime                         6  0 Overlay(Dtime:*Next)

  // V5R3:
  Wtime = %Dec(%Time():*ISO);
  Wdate = %Dec(%Date():*ISO); 

  // V5R2:
  Wtime = %Dec(%Char(%Time():*ISO0):6:0);
  Wdate = %Dec(%Char(%Date():*ISO0):8:0); 

Or, if you make a small change to the data structure:

D                 DS 
D Dtime                         20  0 
D  Wdate                         8  0 Overlay(Dtime) 
D  Wtime                         6  0 Overlay(Dtime:*Next)

  // V5R3:
  Dtime = %Dec(%Timestamp());

  // V5R2:
  Dtime = %Dec(%Char(%Timestamp():*ISO0):20:0);