Knowledge Shared (Beta)


Posted by: HaroldB
Page 1405 has no ratings.
Everything I know about RPGIV Date, Time, Timestamp
Updated: 04 Dec 2009 - 277 days ago
Last viewed on: 06 Sep 2010 (560 views)
Date Time Timestamp information and examples

*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

*-*-*-*-*-*   D A T E   F O R M A T S   *-*-*-*-*-*-*-*-*-*-*-*-*-*    

* NAME      FORMAT      SEPARATORS     LENGTH   *LOVAL     *HIVAL      

* *CDMY   = CDD/MM/YY   /-.,&          9        101/01/01  999/12/31   

* *CMDY   = CMM/DD/YY   /-.,&          9        101/01/01  912/31/99   

* *CYMD   = CYY/MM/DD   /-.,&          9        101/01/01  999/12/31   

* *DMY    = DD/MM/YY    /-.,&          8        01/01/40   31/12/39    

* *EUR    = DD.MM.CCYY  .              10       01.01.0001 31.12.9999  

* *ISO    = CCYY-MM-DD  -              10       0001-01-01 9999-12-31  

* *JIS    = CCYYMMDD    -              10       0001-01-01 9999-12-31  

* *JUL    = YY/DDD      /-.,&          6        40/001     39/365      

* *LONGJUL= CCYY/DDD    /-.,&          8                               

* *MDY    = MM/DD/YY    /-.,&          8        01/01/40   12/31/39    

* *USA    = MM/DD/CCYY  /              10       01/01/0001 12/31/9999  

* *YMD    = YY/MM/DD    /-.,&          8        40/01/01   39/12/31    

*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

*-*-*-*-*-*   T I M E   F O R M A T S   *-*=*=*=*=*=*=*-*-*-*-*-*-*               

* NAME      FORMAT      SEPARATORS     LENGTH   *LOVAL     *HIVAL                 

* *hms    hh:mm:ss      :.,&           8        00:00:00   24:00:00               

* *iso    hh.mm.ss      .              8        00.00.00   24.00.00               

* *usa    hh:mm AM      :              8        12:00 AM   00:00 PM               

*         hh:mm PM                                                                

* *eur    hh.mm.ss      .              8        00.00.00   24:00.00               

* *jis    hh:mm:ss      :              8        00:00:00   24:00:00

*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

*-*-*-*-*-*   T I M E S T A M P   F O R M A T *-*-*-*-*-*-*-*-*-*-*               

*         FORMAT (LENGTH 26)                                                       

* TS =  ccyy-mm-dd-hh.mm.ss.mmmmmm    *Loval 0001-01-01.01.00.00.000000           

*    = '2003-09-26-15.16.26.531000'   *Hival 9999-12-31.24.00.00.000000

*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

When declaring a date or time fields in an RPG program, if DATFMT or TIMFMT is not specified on the D-spec, the formats will default to the DATFMT and TIMFMT specified on the H-spec.  If these keywords are not defined on the H-spec, the format will default to *ISO.

 

 

D-Spec

O-Spec, DSPF, PRTF, PF

Date

‘D’

‘L’

Time

‘T’

‘T’

TimeStamp

‘Z’

‘Z’

 

   

Initialization D-Spec

D @CurrDate       S               D   inz(*sys) DatFmt(*MDY)

D @CurrDate       S               D   inz(*sys) DatFmt(*MDY-)

D @CurrDate       S               D   inz(*sys)

D @CurrTime       S               T   inz(*sys) TimFmt(*iso)

D @CurrTimestmp   S               Z   inz(*sys)

D @CurrDate       S               D   inz(*job)

D @Date           S               D   inz(d'2000-01-31') DatFmt(*iso)

D @Time           S               T   inz(t’23.59.59’) TimFmt(*iso)

D @Date1          S               D   inz(*LoVal) DatFmt(*Iso)

D @Time1          s               T   Inz(*HiVal)   
D @LV_Timestamp   s               z   inz(z'0001-01-01-00.00.00.000000')

d @HV_Timestamp   s               z   Inz(z'9999-12-31-24.00.00.000000')

D @SatStrWeek     s               d   datfmt(*iso)

Built in functions used with date processing

%MSeconds                    Retrieve milliseconds from formatted date

%Seconds                        Retrieve seconds from formatted date

%Minutes                       Retrieve minutes from formatted date

%Hours                           Retrieve hours from formatted time

%Days                            Retrieve days from formatted date

%Months                        Retrieve months from formatted date

%Years                            Retrieve years from formatted date

%Date                             Retrieve the date       

%Time                            Retrieve the time

%TimeStamp                  Retrieve a time stamp

%Subdt                           Substring year, month or day from formatted date

%Diff                              Calculates the difference between two date fields.

 

Extract a portion of a Date, Time, Timestamp

%SUBDT(value:*MSECONDS|*SECONDS|*MINUTES|*HOURS|*DAYS|*MONTHS|*YEARS)
%SUBDT(value:*MS|*S|*MN|*H|*D|*M|*Y)

monthnum   = %subdt(%date():*m);                    

dayofmonth = %subdt(%date():*d);                  

yearnum    = %subdt(%date():*y);                     

monthnum   = %subdt(%timestamp():*m);               

dayofmonth = %subdt(%timestamp():*d);             

yearnum    = %subdt(%timestamp():*y);                

second     = %subdt(%timestamp():*s);                 

second     = %subdt(%time():*s);

                      

Date Conversion

Convert a Character field to a date

With no separators

Date = %date('122507'  : *MDY0);

Date = %date(yymmdd_c6 : *YMD0);

Date = %date('20071225': *ISO0);

With separators

Date = %date('12/25/07'  : *MDY);

Date = %date(yymmdd_c8   : *YMD);

Date = %date('2007-12-25': *ISO);

Convert a Numeric field to a Date

Date = %date(122507   : *MDY);

Date = %date(ddmmyy_n6: *DMY);

Date = %date(20071225 : *ISO);

Convert a date field to an Alpha field

With no separators

mmddccyy_c8 = %char(date: *usa0);

yymmdd_c6   = %char(date: *ymd0);

cyymmdd_c7  = %char(date: *cymd0);

With Separators

mmddccyy_c10 = %char(date: *usa/);

yymmdd_c8    = %char(date: *Iso-);

cyymmdd_c9   = %char(date: *cymd/);

Convert a date field to a Numeric field

yyddd_n5    = %dec( date: *jul);

         ddmmccyy_n8 = %dec( date: *eur);

Misc: Conversions

// Date/time to character

   With no Separators                                                           

           Ccyymmdd_c8 = %char(%date :*iso0);

           hhmmss_c6   = %char(%Time :*iso0);

           YYMMDD      = %Char(Friday : *YMD);          

   With Separators                                                           

           Ccyymmdd_c10 = %char(%date :*iso-);

           hhmmss_c8    = %char(%Time :*iso-);

           YYMMDD      = %Char(Friday : *YMD/);

     // date to numeric...                                                                       

 Yymmdd_n6   = %dec(@dateA:*ymd);      

 Cyymmdd_n7  = %dec(@dateA:*cymd);     

 Ccyymmdd_n8 = %dec(@dateA:*iso);     

 Mmddyy_n6   = %dec(@dateA:*mdy);      

 Mmddyyyy_n8 = %dec(@dateA:*usa);    

// Today’s date/time to numeric                                                         

Hhmmss_n6   = %Dec(%Time():*ISO);

Ccyymmdd_n8 = %Dec(%Date():*ISO);  

// Today’s timestamp to numeric                                                         

Numeric20      = %Dec(%Timestamp());

// Today’s timestamp to Alpha                                                      

Alpha26     = %Char(%Timestamp());

 

Combining The Conversion logic

   An example of converting from one format to another.

   Character to Numeric – Convert ‘12/25/07’ to a Numeric *YMD format

   First convert Character field to actual date format

         %Date(‘12/25/07’:*mdy/)

   Then Convert the date to numeric

         YYMMDD_n6 = %Dec(*******):*YMD0:6:0)

   Combine

          YYMMDD_n6 = %Dec(%Date(‘12/25/07’:*mdy/ ):*YMD)

// numeric to character...

   Output With no Separators and Input with no separators                                                    

Yymmdd_c6 = %char(%date( yymmdd_n6  : *ymd)  :*ymd0); 

Mmddyy_c6 = %char(%date( yymmdd_n6  : *ymd)  :*mdy0); 

Yymmdd_c6 = %char(%date( Cyymmdd_n7 : *cymd) :*ymd0);

   OutPut With Separators and Input with no separators                                                   

Yymmdd_c8 = %char(%date( yymmdd_n6  : *ymd)  :*ymd/); 

Mmddyy_c8 = %char(%date( yymmdd_n6  : *ymd)  :*mdy/); 

Yymmdd_c8 = %char(%date( Cyymmdd_n7 : *cymd) :*ymd/);

Yymmdd_c8 = %char(%date( Cyymmdd_n7 : *cymd/):*ymd/);

// numeric to numeric                             

 Cyymmdd_n7  = %dec(%date( yymmdd_n6 :*ymd):*cymd);   

 Ccyymmdd_n8 = %dec(%date( yymmdd_n6 :*ymd):*iso);

// character to numeric

   Input with Separators                            

 Yymmdd_n6   = %dec(%date( yymmdd_c8 :*ymd/):*ymd);   

 cyymmdd_n7  = %dec(%date( yymmdd_c9 :*ymd/):*cymd);  

 Yymmdd_n6   = %dec(%date( yymmdd_c8 :*ymd/):*iso);        

   Input with No Separators                            

 Yymmdd_n6   = %dec(%date( yymmdd_c6 :*ymd0):*ymd);   

 cyymmdd_n7  = %dec(%date( yymmdd_c6 :*ymd0):*cymd);  

             Yymmdd_n6   = %dec(%date( yymmdd_c6 :*ymd0):*iso);        

 

Date Math Examples

Duration Type

For two dates or a date and a timestamp: *DAYS, *MONTHS, and *YEARS

For two times or a time and a timestamp: *SECONDS, *MINUTES, and *HOURS

For two timestamps: *MSECONDS, *SECONDS, *MINUTES, *HOURS, *DAYS, *MONTHS, and *YEARS

Abbreviations - *MS, *S, *MN, *H, *D, *M, *Y

  duedate = invdate + %days(30);
  duedate += %Days(3);
  If (Today < duedate + %Days(30));
  invdate = duedate - %days(30);
  Duedate -= %Days(3);
  result = %DIFF( date1  :  date2  :  duration );
  MonthsToRun = %Diff(DueDate : Today : *M );
  If %Diff(DueDate : Today : *M ) < 6; 
  Days = %Diff(DueDate : InvDate : *Days);
  If %Diff(Today : DueDate : *Days) > 30;
  days = %diff(duedate:invdate:*DAYS);
  if %diff(today:dueDate:*Days) > 30;
  invdte = %dec(%date(invdte:*ISO)+%days(30));
  ExpiryDate = ContractDate + %Years(Cyears) +
 %Months(Cmonths) + %Days(1);
  WarningDate = ExpiryDate - %Days(90); 
  Creating a 14 position numeric TimeStamp (yyyymmddhhmmss)
  AjTmDt = %Dec(%Char(%Date():*Iso0) + %Char(%Time():*ISO0):14:0); 

Note on %Diff:  You can get a negative return value if the first parameter is an earlier date than the second parameter.                To avoid this, either make sure that the higher date is always first or use the %abs (absolute value) BIF on the return value:

diff_years = %diff( myDate2 : myDate1 : *years );
diff_years = %abs(diff_years); 

Combining Conversion and math logic

 
d cymd_num        s              7p 0 Inz(1090228) 
  CYMD_Num = %DEC(%Date(CYMD_Num:*cymd) + %Years(1):*CYMD); 
d CharCYMD        s              7a   inz('1040228')
  CharCymd = %CHAR(%Date(CharCYMD:*cymd0) + %Years(1):*CYMD0); 
d CharISO         s             10a   inz('2009-02-28')
  Chariso = %CHAR(%Date(Chariso:*iso) + %Months(1):*iso-); 
d CharCYMD        s              7a   inz('1040228')
d CharISO         s             10a   inz('2009-02-28')        
  CharCymd = %CHAR(%Date(Chariso:*iso-) + %Years(1):*CYMD0); 
d CharCYMD        s              7a   inz('1040228')
d DateISO         s               d   inz(d'2009-02-28')            
  DateIso = %Date(Charcymd:*cymd0) + %days(1);  
 
d NumCYMD         s              7p 0 Inz(1090228)
d DateISO         s               d   inz(d'2009-02-28')             
  Numcymd = %dec(DateIso + %Days(1) : *cymd) 
d CharCYMD        s              7a   inz('1040228')
d DateISO         s               d   inz(d'2009-02-28')              
  Charcymd = %Char(DateIso + %Days(1) : *cymd0); 
d NumCYMD         s              7p 0 Inz(1090228)
d DateISO         s               d   inz(d'2009-02-28')      
  Dateiso = %date(Numcymd:*Cymd) + %years(1);                                  

Validating a Date

D Datefield       S               D   DATFMT(*JIS)
D Num_Date        S              6P 0 INZ(910921)
D Char_Time       S              8    INZ('13:05 PM')
D Char_Date       S              6    INZ('041596')
D Char_Tstmp      S             20    INZ('19960723140856834000')
D Char_Date2      S              9A   INZ('402/10/66')
D Char_Date3      S              8A   INZ('2120/115') 
  * Operation Codes
  * E = error             For a Date, Time or Timestamp field or with the ‘D’, ‘T’, ‘Z’  Operation Code
  * D = date             For a Numeric or Character Field
  * T = Time             For a Numeric or Character Field
  * Z = TimeStamp   For a Numeric or Character Field
* Do not have a space between the Operation codes, (‘DE’ is Good, ‘D E’ will not work) 
*  %ERROR will return '1', since Num_Date is not *DMY.
 Test(DE) *DMY Num_Date; 
*  No Factor 1 since result is a D data type field
*  %ERROR will return '0', since the field contains a valid date
 Test(E) DateField; 
* In the following test, %ERROR will return '1' since the
* Timefield does not contain a valid USA time.
 Test(ET) *USA Char_Time;
    if %error;                       
       exsr err???;                            
    endif; 
*  In the following test, %ERROR will return '0' since
*  the character field is a valid *LONGJUL date.
 Test(DE) *LongJul Char_Date3; 
d myDate          s               d
d myDateWithSep   s             10a   inz('2004-04-31')
d error           s             10a   inz('ERROR!') 
    test(de) *ISO myDateWithSep ;
    if %error();
      // handle error
    else ;
      myDate = %date( myDateWithSep : *ISO );
    endif; 
 Using the MONITOR Op-Code (Recommended process)
    monitor ;
      myDate = %date( myDateWithSep : *USA );
    on-error ;
     // handle error
      dsply error ;
    endmon;  

Special Calculations

 

// Determine if 4 digit year is a leap year             

monitor;                                                   

   @datea    = (@year * 10000) + 0229;                    

on-error;                                                 

   // Not Leap year process                               

endmon;

 

// Beginning of Month Date to Date Field

   bom_date= date- %days( %subdt(date: *days) - 1);

 

// End of Month Date to Date Field

   eom_date= bom_date+ %months(1) - %days(1);

 

// Beginning of Quarter Date to Date Field

   boq_date= bom_date- %months( %rem( %subdt(date: *months) -1 :3));

 

// End of Quarter Date to Date Field

   eoq_date= boq_date+ %months(3) - %days(1);

 

// Beginning of Year Date to Date Field

   boy_date= %date( %char( %subdt(date: *years)) + '-01-01' : *iso);

 

// Beginning of Year Date to Date Field

   eoy_date= %date( %char( %subdt(date: *years)) + '-12-31' : *iso);

 

// Quarter Number

   qtr_nbr = %subdt(eoq_date: *months) / 3;

 

// Day of Week (1=Monday...7=Sunday)

   d_o_w = %rem( %diff(date: monday : *days) : 7) + 1;

 

   if d_o_w <= *zeros;

      d_o_w += 7;

   endif;

 

// Day of Year

   d_o_y = %diff( date: boy_date: *days) + 1;

 

// Week Number (ISO 8601)

   jan_4_dat = %date( %char( %subdt(date: *years)) + '-01-04' : *iso);

   jan_4_dow = %rem( %diff(jan_4_dat : monday : *days) : 7) + 1;

 

   wk_nbr = %div(d_o_y + jan_4_dow + 2 : 7);

 

   if wk_nbr = *zeros;

      wk_nbr = 53;

   endif;

 

 

 

     

Return to ClassANova.com home page.