ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

How to validate time in RPG?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to validate time in RPG?

    Hello, Just one more question.. Is there we can validate time in RPG? Like, i need to write a code which gets time as an input from the user. How do i define time field in DDS (my idea was to define it as numeric 4 something like 00:00 format). User can enter something like 12:31 or may be 14:54

    How do i validate this (there would be 2 date fields and 2 time fields). I can find the date difference but need to validate the time as well. Like Input time to be lesser than output time something of that sort...

    Actually i am not sure how to do this? Can someone suggest me

  • #2
    Re: How to validate time in RPG?

    You can define date and time fields in DDS members like below.

    Code:
    A            FDATE           L      
    A            FTIME           T
    or date and time stamp field like

    Code:
    A            FDTS            Z
    Last edited by Mercury; January 29, 2010, 03:42 PM. Reason: typo
    Philippe

    Comment


    • #3
      Re: How to validate time in RPG?

      The TEST opcode should work nicely...

      The TEST operation code allows users to test the validity of date, time, or timestamp fields prior to using them.

      For information on the formats that can be used see Date Data Type, Time Data Type, and Timestamp Data Type.

      * If the |field-name operand is a field declared as Date, Time, or Timestamp:
      o The |dtz-format operand cannot be specified
      o Operation code extenders 'D', 'T', and 'Z' are not allowed
      * If the |field-name operand is a field declared as character or numeric, then one of the operation code extenders 'D', 'T', or 'Z' must be specified.

      Note:
      If the |field-name operand is a character field with no separators, the |dtz-format operand must contain the date, time, or timestamp format followed by a zero.

      o If the operation code extender includes 'D' (test Date),
      + |dtz-format is optional and may by any of the valid Date formats (See Date Data Type).
      + If |dtz-format is not specified, the format specified on the control specification with the DATFMT keyword is assumed. If this keyword is not specified, *ISO is assumed.
      o If the operation code extender includes 'T' (test Time),
      + |dtz-format is optional and may be any of the valid Time formats (See Time Data Type).
      + If |dtz-format is not specified, the format specified on the control specification with the TIMFMT keyword is assumed. If this keyword is not specified, *ISO is assumed.

      Note:
      The *USA date format is not allowed with the operation code extender (T). The *USA date format has an AM/PM restriction that cannot be converted to numeric when a numeric result field is used.

      o If the operation code extender includes 'Z' (test Timestamp),
      + |dtz-format is optional and may be *ISO or *ISO0 (See Timestamp Data Type).

      Numeric fields and character fields without separators are tested for valid digit portions of a Date, Time, or Timestamp value. Character fields are tested for both valid digits and separators.

      |If the character or numeric field specified as the |field-name operand is longer than required by the format being tested, only |the leftmost data is used. For example, if the |dtz-format operand is *MDY for a test of a numeric date, only the leftmost 6 |digits of the |field-name operand are examined.

      For the test operation, either the operation code extender 'E' or an error indicator ER must be specified, but not both. If the content of the |field-name operand is not valid, program status code 112 is signaled. Then, the error indicator is set on or the %ERROR built-in function is set to return '1' depending on the error handling method specified. For more information on error handling, see Program Exception/Errors.

      Figure 327. TEST (E D/T/Z) Example

      PHP Code:

      *...1....+....2....+....3....+....4....+....5....+....6....+....7...+....
      DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++++++++++++++++++++++
      D
      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')
      D
      CL0N01Factor1
      +++++++Opcode(E)+Factor2+++++++Result++++++++Len++D+HiLoEq....
       *
       *  
      Indicator 18 will not be set onsince the character field is a
       
      *  valid *ISO timestamp fieldwithout separators.
      C     *ISO0         TEST (Z)                Char_Tstmp             18
       
      *  Indicator 19 will not be set onsince the character field is a
       
      *  valid *MDY datewithout separators.
      C     *MDY0         TEST (D)                Char_Date              19
       
      *
       *  %
      ERROR will return '1'since Num_Date is not *DMY.
       *
      C     *DMY          TEST (DE)               Num_Date
       
      *
       *  
      No Factor 1 since result is a D data type field
       
      *  %ERROR will return '0'since the field
       
      *  contains a valid date
      C
      C                   TEST 
      (E)                Datefield
      C
       
      In the following test, %ERROR will return '1' since the
       
      Timefield does not contain a valid USA time.
      C
      C     
      *USA          TEST (ET)               Char_Time
      C
       
      *  In the following testindicator 20 will be set on since the
       
      *  character field is a valid *CMDYbut there are separators.
      C
      C     
      *CMDY0        TEST (D)                char_date2             20
      C
       
      *  In the following test, %ERROR will return '0' since
       
      *  the character field is a valid *LONGJUL date.
      C
      C     
      *LONGJUL      TEST (DE)               char_date3 
      Attached Files
      Last edited by jamief; January 29, 2010, 03:49 PM. Reason: charts
      All my answers were extracted from the "Big Dummy's Guide to the As400"
      and I take no responsibility for any of them.

      www.code400.com

      Comment


      • #4
        Re: How to validate time in RPG?

        Originally posted by jamief View Post
        The TEST opcode should work nicely...




        PHP Code:

        *...1....+....2....+....3....+....4....+....5....+....6....+....7...+....
        DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++++++++++++++++++++++
        D
        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')
        D
        CL0N01Factor1
        +++++++Opcode(E)+Factor2+++++++Result++++++++Len++D+HiLoEq....
         *
         *  
        Indicator 18 will not be set onsince the character field is a
         
        *  valid *ISO timestamp fieldwithout separators.
        C     *ISO0         TEST (Z)                Char_Tstmp             18
         
        *  Indicator 19 will not be set onsince the character field is a
         
        *  valid *MDY datewithout separators.
        C     *MDY0         TEST (D)                Char_Date              19
         
        *
         *  %
        ERROR will return '1'since Num_Date is not *DMY.
         *
        C     *DMY          TEST (DE)               Num_Date
         
        *
         *  
        No Factor 1 since result is a D data type field
         
        *  %ERROR will return '0'since the field
         
        *  contains a valid date
        C
        C                   TEST 
        (E)                Datefield
        C
         
        In the following test, %ERROR will return '1' since the
         
        Timefield does not contain a valid USA time.
        C
        C     
        *USA          TEST (ET)               Char_Time
        C
         
        *  In the following testindicator 20 will be set on since the
         
        *  character field is a valid *CMDYbut there are separators.
        C
        C     
        *CMDY0        TEST (D)                char_date2             20
        C
         
        *  In the following test, %ERROR will return '0' since
         
        *  the character field is a valid *LONGJUL date.
        C
        C     
        *LONGJUL      TEST (DE)               char_date3 

        Comment


        • #5
          Re: How to validate time in RPG?

          Thanks Jamie!! By the way, i posted another question related to Data area Data structure as well (you responded). That just worked.. I made a silly mistake when i was defining the result variable which i found finally.. this again is made simple with your example...I am back at work and feel my day is worth :-)

          Comment


          • #6
            Re: How to validate time in RPG?

            Or, for those who enjoy coding for the current century...
            Code:
                  //...1....+....2....+....3....+....4....+....5....+....6....+....7...+....
                 DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++++++++++++++++++++++
                 D
                 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')
                 D
                 CL0N01Factor1+++++++Opcode(E)+Factor2+++++++Result++++++++Len++D+HiLoEq....
                  /FREE
            
                   //  Indicator 18 will not be set on, since the character field is a
                   //  valid *ISO timestamp field, without separators.
                   TEST(ZE) *ISO0 Char_Tstmp;
            
                   //  Indicator 19 will not be set on, since the character field is a
                   //  valid *MDY date, without separators.
                   TEST(DE) *MDY0 Char_Date;
            
                   //  %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;
                  
                   //  In the following test, indicator 20 will be set on since the
                   //  character field is a valid *CMDY, but there are separators.
                   TEST(DE) *CMDY0 char_date2;
                  
                   //  In the following test, %ERROR will return '0' since
                   //  the character field is a valid *LONGJUL date.
                   TEST(DE) *LONGJUL char_date3;
            
                  /END-FREE
            "Time passes, but sometimes it beats the <crap> out of you as it goes."

            Comment

            Working...
            X