星期三, 10月 04, 2023

Get IFS file stat() epoch to RPG timestamps

 STATPGM.RPGLE

000001160318     /* THE INFORMATION CONTAINED IN THIS DOCUMENT HAS NOT BEEN SUBMITTE D   */
000002160318     /* TO ANY FORMAL TESTS AND IS DISTRIBUTED ON AN 'AS IS' BASIS           */
000003160318     /* WITHOUT ANY WARRANTY EITHER EXPRESSED OR IMPLIED. THE USE OF THI S   */
000004160318     /* INFORMATION OR THE IMPLEMENTATION OF ANY OF THESE TECHNIQUES IS A    */
000005160318     /* CUSTOMER RESPONSIBILITY AND DEPENDS ON THE CUSTOMER'S ABILITY TO     */
000006160318     /* EVALUATE AND INTEGRATE THEM INTO THE CUSTOMER'S OPERATION            */
000007160318     /* ENVIRONMENT. WHILE EACH ITEM MAY HAVE BEEN REVIEWED BY IBM           */
000008160318     /* FOR ACCURACY IN A SPECIFIC SITUATION, THERE IS NO GUARANTEE THAT THE */
000009160318     /* SAME OR SIMILAR RESULTS WILL BE OBTAINED ELSEWHERE. CUSTOMERS        */
000010160318     /* ATTEMPTING TO ADAPT THESE TECHNIQUES TO THEIR ENVIRONMENTS DO SO     */
000011160318     /* AT THEIR OWN RISK.                                                   */
000012160317       // ----------------------------------------------------------------------
000100160317       Ctl-Opt DFTACTGRP(*NO) ACTGRP(*NEW);
000101160317       // *********************************************************************
000102160317       //  File Information Structure (stat)
000103160317       //
000104160317       //  struct stat {
000105160317       //   mode_t         st_mode;       /* File mode                       */
000106160317       //   ino_t          st_ino;        /* File serial number              */
000107160317       //   nlink_t        st_nlink;      /* Number of links                 */
000108160317       //   uid_t          st_uid;        /* User ID of the owner of file    */
000109160317       //   gid_t          st_gid;        /* Group ID of the group of file   */
000110160317       //   off_t          st_size;       /* For regular files, the file
000111160317       //                                  * size in bytes                   */
000112160317       //   time_t         st_atime;      /* Time of last access             */
000113160317       //   time_t         st_mtime;      /* Time of last data modification  */
000114160317       //   time_t         st_ctime;      /* Time of last file status change */
000115160317       //   dev_t          st_dev;        /* ID of device containing file    */
000116160317       //   size_t         st_blksize;    /* Size of a block of the file     */
000117160317       //   unsigned long  st_allocsize;  /* Allocation size of the file     */
000118160317       //   qp0l_objtype_t st_objtype;    /* AS/400 object type              */
000119160317       //   unsigned short st_codepage;   /* Object data codepage            */
000120160317       //   char           st_reserved1[66]; /* Reserved                     */
000121160317       //  };
000122160317       //
000123160317       Dcl-S p_statds        Pointer;
000124160317       Dcl-Ds statds BASED(p_statds);
000125160317         st_mode         Uns(10);
000126160317         st_ino          Uns(10);
000127160317         st_nlink        Uns(5);
000128160317         st_pad          Char(2);
000129160317         st_uid          Uns(10);
000130160317         st_gid          Uns(10);
000131160317         st_size         Int(10);
000132160317         st_atime        Int(10);
000133160317         st_mtime        Int(10);
000134160317         st_ctime        Int(10);
000135160317         st_dev          Uns(10);
000136160317         st_blksize      Uns(10);
000137160317         st_alctize      Uns(10);
000138160317         st_objtype      Char(12);
000139160317         st_codepag      Uns(5);
000140160317         st_resv11       Char(62);
000141160317         st_ino_gen_id   Uns(10);
000142160317       End-Ds;
000143160317
000144160317       // --------------------------------------------------------------------
000145160317       //  Get File Information
000146160317       //
000147160317       //  int stat(const char *path, struct stat *buf)
000148160317       // --------------------------------------------------------------------
000149160317       Dcl-Pr stat Int(10) ExtProc('stat');
000150160317         path            Pointer         value options(*string);
000151160317         buf             Pointer         value;
000152160317       End-Pr;
000153160317
000154160317       Dcl-Pr GetTimeZone Char(5) End-Pr;
000155160317
000156160317       Dcl-Ds timezone;
000157160317         tzDir           Char(1);
000158160317         tzHour          Zoned(2:0);
000159160317         tzFrac          Zoned(2:0);
000160160317       End-Ds;
000161160317
000162160317
000163160317       Dcl-S statsize        Int(10);
000164160317       Dcl-S Msg             Char(50);
000165160317       Dcl-S AccessTime      TimeStamp;
000166160317       Dcl-S ModifyTime      TimeStamp;
000167160317       Dcl-S ChgStsTime      TimeStamp;
000168160317       Dcl-S charTS          Char(26);
000169160317       Dcl-S Epoch           TimeStamp       INZ(z'1970-01-01-00.00.00');
000170160317       // Prototype for QWCRSVAL
000171160317       Dcl-Pr Pgm_QWCRSVAL ExtPgm('QWCRSVAL');
000172160317         peRcvVar        Char(1)         Dim(100);
000173160317         peRVarLen       Int(10);
000174160317         peNumVals       Int(10);
000175160317         peSysValNm      Char(10);
000176160317         dsErrCode       Char(256);
000177160317       End-Pr;
000178160317
000179160317      *inlr = *on;
000180160317
000181160317       statsize = %size(statds);
000182160317       p_statds = %Alloc(statsize);
000183160317
000184160317       If stat('/temp/jt400.jar': p_statds) < 0;
000185160317         Msg = 'stat() failed?  Check errno!';
000186160317       // ...-->                    dsply                   Msg
000187160317     c                   dsply                   Msg
000188160317         Return;
000189160317       EndIf;
000190160317
000191160317       // * times in statds are seconds from the "epoch" (Jan 1, 1970)
000192160317       // *   and are in GMT (Greenwich Mean Time)...
000193160317       // * Hey!  Lets convert them to RPG timestamps!
000194160317       AccessTime = Epoch + %Seconds(st_atime);
000195160317       ModifyTime = Epoch + %Seconds(st_mtime);
000196160317       ChgStsTime = Epoch + %Seconds(st_ctime);
000197160317
000198160317       // * adjust timestamps for timezone:
000199160317       timezone = GetTimeZone;
000200160317       If tzDir = '-';
000201160317         AccessTime -= %Hours(tzHour);
000202160317         ModifyTime -= %Hours(tzHour);
000203160317         ChgStsTime -= %Hours(tzHour);
000204160317       Else;
000205160317         AccessTime += %Hours(tzHour);
000206160317         ModifyTime += %Hours(tzHour);
000207160317         ChgStsTime += %Hours(tzHour);
000208160317       EndIf;
000209160317
000210160317       //  display the relevant times:
000211160317       charTS = %Char(AccessTime);
000212160317       Msg = 'Last Access ' + charTS;
000213160317       // ...-->                    dsply                   Msg
000214160317     c                   dsply                   Msg
000215160317
000216160317       charTS = %Char(ModifyTime);
000217160317       Msg = 'Last Modified ' + charTS;
000218160317       // ...-->                    dsply                   Msg
000219160317     c                   dsply                   Msg
000220160317
000221160317       charTS = %Char(ChgStsTime);
000222160317       Msg = 'Status Changed ' + charTS;
000223160317       // ...-->                    dsply                   Msg
000224160317     c                   dsply                   Msg
000225160317
000226160317       DeAlloc p_statds;
000227160317       *inlr = *on;
000228160317
000229160317
000230160317       // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
000231160317       //   This gets the offset from Universal Coordinated Time (UTC)
000232160317       //     from the system value QUTCOFFSET
000233160317       // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
000234160317       Dcl-Proc GetTimeZone;
000235160317         Dcl-Pi GetTimeZone Char(5) End-Pi;
000236160317         Dcl-S peRcvVar        Char(1)         DIM(100);
000237160317         Dcl-S peRVarLen       Int(10);
000238160317         Dcl-S peNumVals       Int(10);
000239160317         Dcl-S peSysValNm      Char(10);
000240160317         Dcl-S p_Offset        Pointer;
000241160317         Dcl-S wkOffset        Int(10)         BASED(p_Offset);
000242160317         Dcl-S p_SV            Pointer;
000243160317         Dcl-Ds dsSV BASED(p_SV);
000244160317           dsSVSysVal      Char(10);
000245160317           dsSVDtaTyp      Char(1);
000246160317           dsSVDtaSts      Char(1);
000247160317           dsSVDtaLen      Int(10);
000248160317           dsSVData        Char(5);
000249160317         End-Ds;
000250160317         Dcl-Ds dsErrCode;
000251160317           dsBytesPrv      BinDec(9:0)     Pos(1) INZ(256);
000252160317           dsBytesAvl      BinDec(9:0)     Pos(5) INZ(0);
000253160317           dsExcpID        Char(7)         Pos(9);
000254160317           dsReserved      Char(1)         Pos(16);
000255160317           dsExcpData      Char(240)       Pos(17);
000256160317         End-Ds;
000257160317         peRVarLen = 100;
000258160317         peNumVals = 1;
000259160317         peSysValNm = 'QUTCOFFSET';
000260160317         CallP(e) Pgm_QWCRSVAL(peRcvVar : peRVarLen :
000261160317               peNumVals : peSysValNm : dsErrCode);
000262160317         *In99 = %Error;
000263160317         If dsBytesAvl > 0  or  *IN99 = *On;
000264160317           Return *blanks;
000265160317         EndIf;
000266160317         p_Offset = %addr(peRcvVar(5));
000267160317         p_SV = %addr(peRcvVar(wkOffset+1));
000268160317         Return dsSVData;
000269160317       End-Proc;

沒有留言: