home *** CD-ROM | disk | FTP | other *** search
/ New Horizons Shareware Collection / HORIZONS1.BIN / SCALAR.CMD < prev    next >
OS/2 REXX Batch file  |  1994-12-27  |  5KB  |  107 lines

  1. /*****************************************************************************/
  2. /*                                                                           */
  3. /* SCALAR.CMD                                                                */
  4. /*                                                                           */
  5. /* Simple functions to calculate the number of days from the given figures;  */
  6. /* years, months, days or a date. See the comments before each function to   */
  7. /* get an idea of what it does.                                              */
  8. /*                                                                           */
  9. /* Craig Morrison, 10 July 1994                                              */
  10. /*                                                                           */
  11. /* Use, abuse, fold, spindle, mutate or mutilate freely.                     */
  12. /*                                                                           */
  13. /* Those of you familiar with SNIPPETs from the C_ECHO should recognize the  */
  14. /* alogrithm implemented below.                                              */
  15. /*                                                                           */
  16. /*****************************************************************************/
  17.  
  18.  dStr = DATE('S')               /* returns 19940710 */
  19.  Year  = SubStr(dStr, 1, 4)     /*     get ^^^^     */
  20.  Month = SubStr(dStr, 5, 2)     /*     get     ^^   */
  21.  Day   = SubStr(dStr, 7, 2)     /*     get       ^^ */
  22.  
  23.  /* This will print the number of days since 01 January 1900 */
  24.  say Date('B') + 1
  25.  
  26.  /* This will do the same with our functions */
  27.  say YMDToScalar(Year, Month, Day)
  28.  
  29.  exit
  30.  
  31. /*****************************************************************************/
  32. /*                                                                           */
  33. /* IsLeapYear(Year)                                                          */
  34. /*                                                                           */
  35. /* Returns 1 for leap year, 0 otherwise                                      */
  36. /*                                                                           */
  37. /*****************************************************************************/
  38. IsLeapYear:
  39.      Procedure
  40.  
  41.      Parse Arg yr
  42.  
  43.      lpyear = 0;
  44.  
  45.      If yr // 400 = 0 Then
  46.          lpyear = 1
  47.      Else If yr // 4 = 0 Then
  48.          If yr // 100 \= 0 Then lpyear = 1
  49.  
  50.  return lpyear
  51.  
  52. /*****************************************************************************/
  53. /*                                                                           */
  54. /* MonthsToDays(Months)                                                      */
  55. /*                                                                           */
  56. /* calculates days from a given number of months                             */
  57. /*                                                                           */
  58. /*****************************************************************************/
  59. MonthsToDays:
  60.      Procedure
  61.  
  62.      Parse Arg m
  63.  
  64.      d = trunc((m * 3057 - 3007) / 100)
  65.  
  66.  return d
  67.  
  68. /*****************************************************************************/
  69. /*                                                                           */
  70. /* YearsToDays(Years)                                                        */
  71. /*                                                                           */
  72. /* calculates days from the given years                                      */
  73. /*                                                                           */
  74. /*****************************************************************************/
  75. YearsToDays:
  76.      Procedure
  77.  
  78.      Parse Arg yr
  79.  
  80.      d = yr * 365 + trunc(yr / 4) - trunc(yr / 100) + trunc(yr / 400)
  81.  
  82.  return d
  83.  
  84. /*****************************************************************************/
  85. /*                                                                           */
  86. /* YMDToScalar(Year, Month, Day)                                             */
  87. /*                                                                           */
  88. /* calculates a scalar number from a date                                    */
  89. /*                                                                           */
  90. /*****************************************************************************/
  91. YMDToScalar:
  92.      Procedure
  93.  
  94.      Parse Arg yr, mo, day
  95.  
  96.      scalar = day + MonthsToDays(mo)
  97.      If mo > 2 Then Do
  98.          If IsLeapYear(yr) Then
  99.              scalar = scalar - 1
  100.          Else
  101.              scalar = scalar - 2
  102.      End
  103.  
  104.      scalar = scalar + YearsToDays(yr - 1)
  105.  
  106.  return scalar
  107.