Hi Carmen,

> We want to have a character time field on a screen, which will then have to
> be converted to a Time datatype within the RPG program.  We were trying to
> come up with all of the different possibilities they could enter, just using
> a pseudo-*USA format:  10:30 AM, 10 AM, 2 PM, etc.  Has anyone else worked
> with this scenario?  Trying to code for all or most of the possibilities
> seems overwhelming.  Thanks.

I wrote a subprocedure that looks through the character string byte by
byte...  if it's a number, it saves it into a field, if it's a colon it
moves on to the next field, if it's an 'A' or 'P' it figures out the AM/PM
logic, it ignores spaces and/or the letter 'M' (from "PM").

I'm not sure why you'd need this in a display file application (since, in
that scenario, you can just tell the user how to key the time info!) but
here it is, in case it's useful:

      *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      * ParseTime():  Parse a time field entered by the user
      *
      *     Input = (input) character field containing time
      *    Output = (output) Time field containing results
      *
      * Returns *ON if successful, *OFF otherwise
      *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     P ParseTime       B
     D ParseTime       PI             1N
     D  Input                        50A   varying const
     D  Output                         T

     D f               s             10I 0
     D x               s             10I 0
     D field           s             10A   varying dim(3)
     D c               s              1A
     D pm              s              1N
     D hh              s              2P 0
     D mm              s              2P 0
     D ss              s              2P 0
     D temp            s              8A

      /free

        field(1) = '';
        field(2) = '0';
        field(3) = '0';
        output = t'00.00.00';
        pm = *off;
        f = 1;

        for x = 1 to %len(input);

           c = %subst(input: x: 1);

           select;
           when c=' ' or c='a' or c='A' or c='a' or c='M';
             // ignore spaces and the letters 'AM'

           when c = ':';

             f = f + 1;

             if (f > %elem(field));
                return *OFF;
             endif;

           when c = 'p' or c = 'P';
             pm = *on;

           other;
             field(f) = field(f) + c;

           endsl;

        endfor;

        // Make sure user at least supplied the hour

        if (Field(1) = '');
           return *OFF;
        endif;

        // make sure that hr/min/sec can convert to numbers

        monitor;
           hh = %int(field(1));
           mm = %int(field(2));
           ss = %int(field(3));
        on-error;
           return *OFF;
        endmon;

        // if PM is specified, it's 12 hours later.

        if (pm);
           hh = hh + 12;
        endif;

        // try to build a time field

        temp = %editc(hh:'X') + ':'
             + %editc(mm:'X') + ':'
             + %editc(ss:'X');

        monitor;
          output = %time(temp:*HMS);
        on-error;
          return *OFF;
        endmon;

         return *ON;

      /end-free
     P                 E


As an Amazon Associate we earn from qualifying purchases.

This thread ...

Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.