星期三, 11月 08, 2023

2011-07-19 如何取得含有 micro second 的系統時間 ?(MI MATTOD, API QWCCVTDT and Qp0zCvtToTimeval to get timestamp)


如何取得含有 micro second 的系統時間 ?(MI MATTOD, API QWCCVTDT and Qp0zCvtToTimeval to get timestamp)

File  : QRPGLESRC

Member: MATTODR2

Type  : RPGLE

Usage : CRTBNDRPG MATTODR2


     H DEBUG  OPTION(*SRCSTMT:*NODEBUGIO) DFTACTGRP(*NO) ACTGRP(*CALLER)
     H BNDDIR('QC2LE')
     **-- Display long text:  ------------------------------------------------**
     D DspLngTxt       Pr                  ExtPgm( 'QUILNGTX' )
     D  DtLngTxt                   1024a   Const  Options( *VarSize )
     D  DtLngTxtLen                  10i 0 Const
     D  DtMsgId                       7a   Const
     D  DtMsgF                       20a   Const
     D  DtError                      10i 0 Const
     D*-- GetErrNo ---- Get error number ----------------------------------
     D*   extern int * __errno(void);
     D @__ERRNO        PR              *   EXTPROC('__errno')
     D
     D*-- StrError ---- Get error text ------------------------------------
     D*   char *strerror(int errnum);
     D STRERROR        PR              *   EXTPROC('strerror')
     D    ERRNUM                     10I 0 VALUE
     D
     D ERRNO           PR            10I 0

     **-- Convert date & time:  ----------------------------------------------**
     D CvtDtf          Pr                  ExtPgm( 'QWCCVTDT' )
     D  CdInpFmt                     10a   Const
     D  CdInpVar                     17a   Const  Options( *VarSize )
     D  CdOutFmt                     10a   Const
     D  CdOutVar                     20a          Options( *VarSize )
     D  CdError                      10i 0 Const

     **-- Qp0zCvtToTimeval()-Convert _MI_Time to Timeval Structure------------**
     D DtsToTimeval    Pr            10I 0 ExtProc( 'Qp0zCvtToTimeval' )
     D  toTimeVal                      *   Value
     D  fromDts                       8a   Const
     D  option                       10I 0 Value

     D p_timeval       S               *
     D timeval         DS                  based(p_timeval)
     D   tv_sec                      10I 0
     D   tv_usec                     10I 0

     D QP0Z_CVTTIME_TO_TIMESTAMP...
     D                 C                   CONST(1)

     D tv              S               *
     D tvlen           S             10I 0
     D rc              S             10I 0
     D err             S             10I 0
     D DateOut         s             20a   Inz( *All'0' )

     D MATTOD          pr                  extproc('mattod')
     D  DTS                           8a

     D MsgTxt          S            256
     D DtsCur          S              8
     D tv_usec6        S              6  0

     C                   Eval      *InLr = *On

     c                   eval      tvlen      = %size(timeval)
     c                   alloc     tvlen         tv
     c                   eval      p_timeval  = tv
      * get System machine time
     C                   callp     MatTOD(DtsCur)

     C                   CallP     CvtDtf( '*DTS'
     C                                   : DtsCur
     C                                   : '*YYMD'
     C                                   : DateOut
     C                                   : 0
     C                                   )
     C                   eval      rc = DtsToTimeval (
     C                                   tv             :
     C                                   DtsCur         :
     C                                   QP0Z_CVTTIME_TO_TIMESTAMP
     C                                               )
     C                   if        rc < 0
     C                   EVAL      ERR = ERRNO
     C                   Eval      MsgTxt = 'error: ' + %CHAR(ERR) + ' ' +
     C                                      %STR(STRERROR(ERR))
     C                   else
     C                   eval      tv_usec6 = tv_usec
     C                   eval      %SubSt(DateOut: 15) = %Char(tv_usec6)
     C                   eval      MsgTxt = 'Get current _MI_TIME with ' +
     C                                      'MATTOD and converted with ' +
     C                                      'Qp0zCvtToTimeval API -> '   +
     C                                      'Current time is ' +
     C                                      DateOut +
     C                                      '. Timestamp is ' +
     C                                      %Char(tv_sec)  + '.' +
     C                                      %Char(tv_usec6)
     C                   endIf
     **
     C                   CallP(e)  DspLngTxt( MsgTxt
     C                                      : %Len( MsgTxt )
     C                                      : *Blanks
     C                                      : *Blanks
     C                                      : *Zero
     C                                      )

     P*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     P*  This procedure return call socket C API errno
     P*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     P ERRNO           B
     D ERRNO           PI            10I 0
     D P_ERRNO         S               *
     D WWRETURN        S             10I 0 BASED(P_ERRNO)
     C                   EVAL      P_ERRNO = @__ERRNO
     C                   RETURN    WWRETURN
     P                 E





沒有留言: