converting date + time into timestamp

895 pts.
Tags:
AS/400 date format
Date calculation
RPGLE Date Calculation
Hi I'm trying to convert date+time into timestamp n get thousands of error...

my code is as below

c     FMTIMCHAR     ifeq      '*ALL'

Eval      FMTIMCHAR = '0000'

C                   Eval      T1 = %subst(fmTIMCHAR:1:2) + ':' +  C                             %subst(fmTIMCHAR:3:2)              

C                   Eval      FMTMS1 = %Timestamp(%Char(FMDAT) + '-' + C                             T1 + '.000000')                         

 

above code errors out on compilation when T1 is declared as 4A(Character length 4) says...operand is too small to create a Timestamp

so I appended extra zero and passed values as

FMDAT = '2009-10-27'

T1 = '00:00:00'

now i get error Date, Time or Timestamp value is not valid. on %timestamp statement.

Please help



Software/Hardware used:
AS400
1

Answer Wiki

Thanks. We'll let you know when a new response is added.

This is a subprocedure I wrote sometime back. You should be able to modify it to suite your needs.

<pre>
****************************************************************
* SET COMPILER OPTIONS *
****************************************************************
HNOMAIN
HOPTION(*NODEBUGIO:*SRCSTMT)
**********************************************************************
* PROGRAM NAME: CRTTIMSTMP *
* CREATION DATE: 04/20/04 *
* PURPOSE OF PROGRAM: CREATE DATE TIME STAMP. *
*********************************************************************
* PROTOTYPE FOR PROCEDURE.
*********************************************************************
D CRTTIMSTMP PR Z
D PASSDATE 8 0
D PASSTIME 4 0
D PASSTIME6 6 0
*********************************************************************
* START PROCEDURE DEFINITION.
*********************************************************************
P CRTTIMSTMP B EXPORT
D CRTTIMSTMP PI Z
D PASSDATE 8 0
D PASSTIME 4 0
D PASSTIME6 6 0
*********************************************************************
* DATE AND TIME FIELDS.
*********************************************************************
D RETURNSTAMP S Z INZ(*SYS)
D TIMESTAMP S T INZ(*SYS)
D DATESTAMP S D DATFMT(*ISO)
*********************************************************************
* FIELD DEFINITIONS.
*********************************************************************
D TIME6 S 6 0
D TIMCONVERT S 26 INZ(‘0001-01-01-00.00.00.000000’)
D TIMEFLDC S 8 INZ
*********************************************************************
* FORMAT TIME AND DATE FIELDS.
*********************************************************************
C IF PASSTIME <> 0
C MOVEL PASSTIME TIME6
C ELSE
C MOVEL PASSTIME6 TIME6
C ENDIF

C MOVEL TIME6 TIMESTAMP
C MOVEL PASSDATE DATESTAMP
C MOVE TIMESTAMP TIMEFLDC
*********************************************************************
* CREATE DATE TIME STAMP.
*********************************************************************
C MOVEL DATESTAMP TIMCONVERT
C EVAL TIMCONVERT = %REPLACE(TIMEFLDC :
C TIMCONVERT : 12 : 8)
C MOVEL TIMCONVERT RETURNSTAMP
*********************************************************************
* RETURN DATE TIME STAMP.
*********************************************************************
C RETURN RETURNSTAMP

P CRTTIMSTMP E

</pre>

Discuss This Question: 6  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.
  • Nutangujar
    yippee i think i got the answer...in the time field instead of ':' i shud use '.' silly mistake.... thanks guys...
    895 pointsBadges:
    report
  • Satsho
    You can always use the Convert Date and Time Format (QWCCVTDT) API
    1,245 pointsBadges:
    report
  • TomLiotta
    IMO -- Note that the date/time format for the program should be set explicitly. The default is generally *ISO, but could be changed. If the DATFMT() control spec is explicit, your program code can then be able to use the same date/time separators under all circumstances. Choose a standard format for handling inside the program. Be aware that system and job formats might change at any time. With a program format set, conversions can always be standardized in code. Tom
    125,585 pointsBadges:
    report
  • BigKat
         d wTime           s               t   inz(*sys)
         d wDate           s               d   inz(*sys)
         d wTimestamp      s               z
    
           wTimestamp = wDate + wTime;  
    9,410 pointsBadges:
    report
  • mmanley
    Here's an example of the easiest way (you can change the date/time formats to match the format of your data)

     %DATE, %TIME, and %TIMESTAMP can be used with a numeric or        
      character parameter, and an optional format, to return a         
      date, time, or timestamp:                                        
                                                                       
         date = %date(num : *mdy);                                     
         time = %time(char : *iso0);                                   
                                                                       
      %DATE and %TIME can be added together to return a timestamp:     
                                                                       
         timestamp = %date(numdate : *ymd) + %time(numtime : *iso);    
    470 pointsBadges:
    report
  • azohawk

    The answers above should set you on the right path, but to answer your original question, the code sample you provided is populating T1 with hour and minute but NOT seconds.

    You did not indicate how your fields were defined, as others have indicated, your fields must be defined properly. FMTMS1 should be defined as a timestamp data type, FMDAT as a date data type, and T1 as a time data type.

    3,990 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

Thanks! We'll email you when relevant content is added and updated.

Following

Share this item with your network: