Fun With Dates In RPGLE

发表于:2007-05-26来源:作者:点击数: 标签:
RPGLE提供了许多有关日期数据格式的用法,下面对这些日期的用法进行了一下汇总。 [b:2ffd804c3d]1.Builtinfunctionsusedwithdateprocessing[/b:2ffd804c3d] %MSecondsRetrievemillisecondsfromformatteddate %SecondsRetrievesecondsfromformatteddate %Minu

RPGLE提供了许多有关日期数据格式的用法,下面对这些日期的用法进行了一下汇总。 
[b:2ffd804c3d]1.Built in functions used with date processing[/b:2ffd804c3d]
%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
%Char        Use a decimal field in a substring function

2.[b:2ffd804c3d]Define Date Variables All date formats[/b:2ffd804c3d] 
without keyword  DatFmt will default to *ISO. '2003-09-23'  
When you initialize a date field you must prefix the date with the letter "D" ,see below.
     [code:1:2ffd804c3d]D*---------------------------------------------------------
     D*  Field Definitions.
     D*---------------------------------------------------------
     D ISOdate         S               D
     D USAdate         S               D   DatFmt(*USA)
     D XMASDate       S               D   Inz(D'2003-12-25')
      [/code:1:2ffd804c3d]   
  
  
[b:2ffd804c3d]3.Various types of Date data format[/b:2ffd804c3d]   
      [code:1:2ffd804c3d]*-----------------------------------------------------------
      * RPG-defined date formats and separators for Date data type
      *-----------------------------------------------------------
      * 2-Digit Year Formats
      * *MDY  Month/Day/Year  mm/dd/yy  8  09/26/03
      * *DMY  Day/Month/Year  dd/mm/yy  8  26/09/03
      * *YMD  Year/Month/Day  yy/mm/dd  8  03/09/26
      * *JUL  Julian          yy/ddd    6  03/926
      *----------------------------------------------------------
      * 4-Digit Year Formats
      * *ISO  Int Standards Org yyyy-mm-dd  10  2003-09-26
      * *USA  IBM USA Standard  mm/dd/yyyy  10  09/26/2003
      * *EUR  IBM European Std  dd.mm.yyyy  10  26.09.2003
      * *JIS  Japan Indst Std   yyyy-mm-dd  10  2003-09-26
      *
      *
      *----------------------------------------------------------[/code:1:2ffd804c3d]
      

[b:2ffd804c3d]4.Use dates in RPGLE[/b:2ffd804c3d]
     First get current date in ISO date format
     (without DATFMT or DATEDT keyword in control specification)

     [code:1:2ffd804c3d]C                   Eval      ISOdate = %Date()[/code:1:2ffd804c3d]
 
 
       Then,move this date to a decimal 8,0 field
       the date is now in format  20030926
     [code:1:2ffd804c3d]  C                   Move      ISODate       Decimal8        8 0[/code:1:2ffd804c3d]
 
 
 
     
      Now   lets add 1 month to the date.
       date after will equal 2003-10-26
      %days and %years works the same as %months
      
    [code:1:2ffd804c3d] C                   Eval      WorkISO  = ISODate + %Months(1)[/code:1:2ffd804c3d]
 
 
      
      Logon date is set equal to today then the month is extracted
      the "*M" is the same as "*Months"  LogMonth = 09.
      LogDay   = 26.
    
     [code:1:2ffd804c3d]C                   Eval      LogonDate =  %Date()
     C                   Extrct    LogonDate:*Y  LogYear
     C                   Extrct    LogonDate:*M  LogMonth
     C                   Extrct    LogonDate:*D  LogDay[/code:1:2ffd804c3d]
     
   Build the date string - Later we will add the day name
     
    [code:1:2ffd804c3d]D MonthNames      S             12    Dim(12) CtData
 
 
     C                   Eval      Date_String =
     C                              %Trim(MonthNames(LogMonth))
     C                              + %trim('@') + %Trim(LogDay)
     C                              + %trim(',@') + %Char(LogYear)
     C*----------------------------------------------------
** CTDATA MonthNames
January
February
March
April
May
June
July
August
September
October
November
December[/code:1:2ffd804c3d]   
    
     convert the "@" back to *Blanks
    Date_String = 'September 26, 2003'
    
     [code:1:2ffd804c3d]C     '@':' '       Xlate     Date_String   Date_String
      *
      * TimeStamp = yyyy-mm-dd-hh.mm.ss.mmmmmm (length 26).
      * TimeStamp = '2003-09-26-15.16.26.531000'
      *
     C                   Eval      TimeStamp = %TimeStamp()
      *
      *  Free Format date stuff   By the way Name2 = 'Friday'
      *
      /Free
        DateIn   = %Date()                     ;
        ISODate  = %Date()                     ;
        ISODate  = DateIn                      ;
        Year     = %Subdt(ISODate:*Y)          ;
        Month    = %Subdt(ISODate:*M)          ;
        Day      = %Subdt(ISODate:*D)          ;
        FromISO  = ISODate - %YEARS(1)         ;
        ToISO    = ISODate                     ;
        DiffDays = %Diff(ToISO:FromISO:*DAYS)  ;
        ISODate  = DateIn                      ;

        WorkField = %Diff(ISODate:D'1899-12-31':*DAYS);
        WorkField = %REM(WorkField:7);

        NamePtr = NamePtr + (WorkField * 9);
        Name2 = Name;
      /End-Free
      *
      *  Build the date string - With The Day Name
      *  DATE_STRING = 'Friday  September 26, 2003              '
      *
     C                   Eval      Date_String =
     C                              %trim(Name) + %Trim('@@')
     C                              + %trim(MonthNames(LogMonth))
     C                              + %trim('@') + %Trim(LogDay)
     C                              + %trim(',@') + %Char(LogYear)
     C                   Eval      Date_String = %Xlate('@':' ':Date_String)
      *
      * Calculate the last day of the month
      * ENDOFMONTH = '2003-09-30'
      *
     C     ISODate       AddDur    1:*Months     NextMonth
     C                   Extrct    NextMonth:*D  DiffDays
     C     NextMonth     SubDur    DiffDays:*D   EndOfMonth


     C                   Eval      *INLR = *On
 [/code:1:2ffd804c3d]

 yoyoage 回复于:2004-01-31 09:53:03
另外,日期格式之间可以相互转换
[code:1:ee07bc1a65]/free 
    // Convert Date from clearcase/" target="_blank" >ccyymmdd to mmddyy 
                              
          Sdt = %uns(%char(%Date(#SoSdt:*Iso):*Mdy0));               
                                                                
    // Todays Date in Ccyymmdd format   
                           
          Today = %uns(%char(%Date():*Iso0));                       
                                                                
    // Convert From Julian to ccyymmdd   
                          
          CbpPayDte = %Dec(%Char(%Date(%Subst(%Editc(Rpdgj:'X'):2:5) 
                 :*Jul0):*Iso0):8:0);         [/code:1:ee07bc1a65]

 andrewleading_he 回复于:2004-01-31 10:08:01
不错,有机会试试看!帮忙顶一下!

 huim 回复于:2004-01-31 20:44:50
好贴

 红像 回复于:2004-02-02 09:15:03
Great~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 yj2yq 回复于:2004-02-02 16:27:01
精彩!

 yoyoage 回复于:2004-02-02 19:58:17
另外还补充一下SQLRPGLE中的日期函数用法:
SQLRPGLE中,有下面这些可用的日期函数:
DATE 
DAY 
DAYS 
DAYOFMONTH 
DAYOFWEEK 
DAYOFWEEK_ISO 
DAYOFYEAR 

例如: 

SELECT Item, Date(Days(SUBSTR(CHAR(MFDATE),1,4)||'-'|| 
SUBSTR(CHAR(MFDATE),5,2)|| '-'|| 
SUBSTR(CHAR(MFDATE),7,2)) + EXPIRE) 
FROM ITEMMASTER 

MFDATE是YYYYMMDD格式的数据,存放生产日期
EXPIRE存放产品的生命周期,用天数表示。

DAYS函数可用于数字或者字符型,按照YYYY-MM-DD的格式计算天数。

原文转自:http://www.ltesting.net