home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / dongrovs.zip / datetime.cmd < prev    next >
OS/2 REXX Batch file  |  1996-10-16  |  14KB  |  276 lines

  1. /* ********************************************************************* */
  2. /*   By: Jetnick Enterprise                                              */
  3. /*       Don E. Groves, Jr.                                              */
  4. /*   Contact Information:                                                */
  5. /*     E-mail: jetnick@erols.com                                         */
  6. /*        CIS: 71310,3702                                                */
  7. /* Date: 17 Sep 1996                                                     */
  8. /* ********************************************************************* */
  9. /* public Classes and Routines                                           */
  10. /*  .DateTime                Easy way to pass Date and Time as a unit    */
  11. /*  .Elasp_Time              A simple Elapsed time timer.                */
  12. /*  .Clock         Factory for producing current DateTime objects.       */
  13. /* ********************************************************************* */
  14. /* This file is primary intented use is as a ::REQUIRES type file.       */
  15. /* ********************************************************************* */
  16. /*  General Documentation for other Programmer type Users                */
  17. /*                                                                       */
  18. /*   Some Simple Adveration:                                             */
  19. /* N         = Number                                                    */
  20. /* S         = Character or String                                       */
  21. /* DS        = Date String same formatted as produced by the             */
  22. /*             built in DATE('N') function                               */
  23. /* TS        = Time String same formatted as produced be the             */
  24. /*             built in TIME('N') function                               */
  25. /* NTS       = Number of Seconds since '1 Jan 1970 00:00:00'             */
  26. /* DT        = DateTime Object {described below}                         */
  27. /* ********************************************************************* */
  28. /*                                                                       */
  29. /*  Class DateTime                                                       */
  30. /*     Inherates all methods from The Object class.                      */
  31. /*     Objects of this type are useful for passing the Date/Time         */
  32. /*     around  as a unit.  {Pass by value only type object}              */
  33. /*   Public Methods:                                                     */
  34. /*     SecPerDay      Returns number of Seconds in a day                 */
  35. /*     NTS_Base       Return the beginning of TIME <G> Base              */
  36. /*                     currently set to DATE('B','1 Jan 1970','N')       */
  37. /*                                                                       */
  38. /*     INIT( )       Parameters: (optional one of the following)         */
  39. /*            DS, TS   { Date('N') and TIME('N') strings}                */
  40. /*            DT       { another DateTime object }                       */
  41. /*            NTS      { Number of Seconds since '1 Jan 1970 00:00:00' } */
  42. /*                    If No parameters then uses current Date and Time   */
  43. /*                                                                       */
  44. /*     NTS       returns Number of second since  NTS_Base date.          */
  45. /*                                                                       */
  46. /*     compareBy return NTS useful in generic sorts and compares.        */
  47. /*                                                                       */
  48. /*     Elapsed_Seconds       returns number of seconds to/from now       */
  49. /*                   positive means Object is older then now.            */
  50. /*                                                                       */
  51. /*     Date    Parameters:                                               */
  52. /*          (optional same as first parameter to built in DATE())        */
  53. /*          returns Object's DATE value in format requested.             */
  54. /*                                                                       */
  55. /*     DOW     Parameters: NONE                                          */
  56. /*          returns 0 - 6  for Day of week 0= Sunday                     */
  57. /*                                                                       */
  58. /*     Time   Parameters:                                                */
  59. /*          (optional same as first parameter to built in TIME())        */
  60. /*       Returns: Object's TIME value in format requested.               */
  61. /*                                                                       */
  62. /*     Adjust   Parameters: (optional)                                   */
  63. /*          N   { Number of seconds to adjust }                          */
  64. /*                Positive Forwards                                      */
  65. /*                Negitive Backwards.                                    */
  66. /*                Defaults to Zero                                       */
  67. /*       Returns: New DateTime object equal to Current plus Adjustment.  */
  68. /*                                                                       */
  69. /*      +      Parameter:                                                */
  70. /*          N   { Number of seconds to adjust }                          */
  71. /*       Returns: New DateTime object equal to Current plus Adjustment.  */
  72. /*                                                                       */
  73. /*      -      Parameter:                                                */
  74. /*          N   { Number of seconds to adjust }                          */
  75. /*       Returns: New DateTime object equal to Current minus Adjustment. */
  76. /*                                                                       */
  77. /*     String   Parameters: (none)                                       */
  78. /*       Returns:  Date and Time in Message Date format.                 */
  79. /*                                                                       */
  80. /*     MakeString    same as String method.                              */
  81. /*                                                                       */
  82. /*     MakeDateTime   Parameters: (none)                                 */
  83. /*       Returns: New copy of DateTime object equal to current object.   */
  84. /*                                                                       */
  85. /* ********************************************************************* */
  86. /*                                                                       */
  87. /*    Class Clock a DateTime Producer, a factory iows.                   */
  88. /*      This is a Factory for creating 'DateTime' objects, with the      */
  89. /*      current Date and Time. Clock objects themself remain running.    */
  90. /*     All CLASS members are also Instances methods.                     */
  91. /*                                                                       */
  92. /*      Inherates all methods from DateTime class, except all methods    */
  93. /*     information returned is based on the current date and time.       */
  94. /*                                                                       */
  95. /*     MakeDateTime                                                      */
  96. /*       Parameters: (none)                                              */
  97. /*       Returns: New DateTime object equal to current Current DateTime. */
  98. /*                                                                       */
  99. /* ********************************************************************* */
  100. /*                                                                       */
  101. /*    Class Elasp_Time a subclass of DateTime public                     */
  102. /*      Inherates all methods from DateTime.                             */
  103. /*      Overrides                                                        */
  104. /*     String   Parameters: (none)                                       */
  105. /*       Returns: A formatted string equal to the Age of the object.     */
  106. /*                                                                       */
  107. /* ********************************************************************* */
  108. /*  All other classes defined in this file arenot visable outside of it. */
  109. /* ********************************************************************* */
  110. /*                                                                       */
  111. /* ********************************************************************* */
  112. /* ********************************************************************* */
  113. parse source . invhow myname
  114. if invhow == 'COMMAND'
  115. THEN DO
  116.    SAY .Clock
  117. END
  118. return 0
  119.  
  120. ::class DateTime_Const       /* Seperating these constants out saves     */
  121. ::method init class          /* running though this 5+ additional times. */
  122.   expose NTS_Base SecPerDay
  123.   NTS_Base  = DATE('B','1 Jan 1970','N')
  124.   SecPerDay = 86400
  125. return self
  126. ::method SecPerDay class     /* number of Seconds in a day */
  127.   expose NTS_Base SecPerDay
  128. forward to (SecPerDay) message 'MakeString'
  129. ::method NTS_Base  class     /* The Date part of the beginning of TIME */
  130.   expose NTS_Base SecPerDay
  131. forward to (NTS_Base) message 'MakeString'
  132.  
  133.   /* the common private base of all DateTime type objects */
  134. ::class DateTime_Root
  135. ::method init
  136. return self
  137. ::method init class
  138. return self
  139.     /* Give access to DateTime Constant Data */
  140. ::method SecPerDay class  /* number of Seconds in a day */
  141.   forward to (.DateTime_Const)
  142. ::method SecPerDay        /* Give instances the same access. */
  143.   forward to (.DateTime_Const)
  144.    /* nts = ( DATE('B') - Nts_Base ) * SecPerDate + Time('S') */
  145. ::method NTS_Base  class  /* The Date part of the beginning of TIME */
  146.   forward to (.DateTime_Const)
  147. ::method NTS_Base         /* Give instances the same access. */
  148.   forward to (.DateTime_Const)
  149.  
  150.   /* the common private base of all DateTime instance type objects */
  151. ::class DateTime_Base subclass DateTime_Root
  152. ::method init
  153.   expose nts
  154.   use arg inDate, inTime
  155.   forward continue class (super)
  156.   if arg() = 1 /* If only one argument then */
  157.   THEN DO
  158.      nts = inDate~Request('DateTime')
  159.      IF nts \= .nil
  160.      THEN nts = nts~nts
  161.      ELSE IF inDate~datatype('W')      /* if Numeric */
  162.      THEN nts = inDate~MakeString      /* then assume NTS value */
  163.      ELSE RAISE syntax 93.904 array(1,indate)
  164.   END
  165.   ELSE DO
  166.      nts = TIME('S',inTime) + ((DATE('B',inDate) - self~NTS_Base ) * self~SecPerDay )
  167.      if ARG() > 2  /* optional Adjustment Amount in seconds */
  168.      THEN nts = nts + ARG(3)
  169.   END
  170.   if nts < 0
  171.   then nts = 0  /* handle before the beginning of time condition. */
  172. return self
  173. ::method NTS
  174.   expose nts
  175.    /* Simply put, for current date the nts is           */
  176.    /* ( DATE('B') - Nts_Base ) * SecPerDate + Time('S') */
  177. forward to (nts) message 'MakeString'
  178.  
  179.   /* Objects of this type are useful for passing the Date/Time around */
  180. ::class DateTime subclass DateTime_BASE public
  181. ::method init
  182.   if arg() = 0  /* default to current date and time */
  183.   THEN forward class (super) ARRAY ( DATE(), TIME())
  184.   ELSE forward class (super)
  185. ::method Elapsed_Seconds    /* returns number of seconds to/from now */
  186. forward to (.Clock~NTS - self~NTS) message 'MakeString'
  187. ::method Date
  188.   use arg how
  189.   if ARG() = 0
  190.   then how = 'N'
  191. return Date(how,((self~nts % self~SecPerDay) + self~NTS_Base),'B')
  192. ::method Dow   /* Sunday = 0, Monday=1, ... , Saturday=6 */
  193. return (1 + ((self~nts % self~SecPerDay) + self~NTS_Base) // 7) // 7
  194. ::method Time
  195.   use arg how
  196.   if ARG(1,'O')
  197.   then how = 'N'
  198. return TIME(how,(self~nts // self~SecPerDay),'S')
  199. ::method Adjust
  200.   use arg phrs
  201.   wrk = self~NTS
  202.   IF ARG(1,'E')
  203.   THEN wrk = wrk + phrs
  204. forward to (.DateTime) message 'new' Array (wrk)
  205. ::method String
  206.   dt = self~DATE
  207. return DATE('W',dt)~SUBSTR(1,3)~''(',')~' '(DATE('N',dt)~RighT(11))~' '(self~TIME('N'))
  208. ::method MakeString
  209.   forward message 'String'
  210. ::method MakeDateTime
  211. forward to (.DateTime) message 'new' Array (self~NTS)
  212. ::method compareBy
  213.   forward message 'NTS'
  214. ::method '+'
  215.   forward message 'Adjust'
  216. ::method '-'
  217.   use arg phrs
  218.   IF ARG(1,'O')
  219.   THEN phrs = 0
  220.   forward message 'Adjust' ARRAY ( -1 * phrs )
  221.  
  222.  
  223.   /* A handy Elapsed Time Timer with nice String display */
  224. ::class Elasp_Time subclass DateTime public
  225. ::method String
  226.   sttime = .Clock~MakeDateTime
  227.   elps = stTime~NTS - self~NTS
  228.   if elps < 0
  229.   then DO  /* Handle TIME NOW is before condition. <G> */
  230.      elps = elps~ABS
  231.      rtCode = (elps % self~SecPerDay)~' '('days,')~' '(TIME('N',(elps // self~SecPerDay),'S'))~' '('Until:')~' '(self~String:super)
  232.   END
  233.   ELSE rtCode = sttime~String~' '('Elasped Time:')~' '(elps % self~SecPerDay)~' '('days,')~' '(TIME('N',(elps // self~SecPerDay),'S'))
  234. return rtCode
  235. ::method MakeElasp_Time
  236. forward to (.Elasp_Time) message 'new' Array (self~NTS)
  237.  
  238.  
  239.  /* This is a Factory for creating 'DateTime' objects, */
  240.  /* with the current Date and Time. */
  241.  /* Clock objects themself remain running. */
  242. ::class Clock subclass DateTime_Root Public
  243.  
  244. ::method MakeDateTime class
  245. return .DateTime~NEW
  246. ::method MakeDateTime
  247.   forward to (self~class)
  248.  
  249. ::method UNKNOWN  CLASS
  250.   use arg cMsg, ARGS
  251.   /* Create a 'DateTime' and let the resulting object handle */
  252.   /* all other messages. */
  253.   forward to (self~MakeDateTime) message (cMsg) ARGUMENTS (ARGS)
  254. ::method UNKNOWN             /* Make instances look the same */
  255.   forward to (self~class)
  256.  
  257. ::method String CLASS
  258.   forward message 'UNKNOWN' ARRAY ( 'STRING', ARG(1,'A'))
  259. ::method String              /* Make instances look the same */
  260.   forward to (self~class)
  261.  
  262. ::method MakeString CLASS
  263.   forward message 'UNKNOWN' ARRAY ( 'MAKESTRING', ARG(1,'A'))
  264. ::method MakeString          /* Make instances look the same */
  265.   forward to (self~class)
  266.  
  267. ::method NEW                 /* Make instances look the same */
  268.   forward to (self~Class)
  269.  
  270. ::routine Clock public
  271. return .clock
  272.  
  273. /* ********************************* */
  274. /* ********************************* */
  275.  
  276.