home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / julian.zip / JULDATE.CMD next >
OS/2 REXX Batch file  |  1997-09-15  |  5KB  |  175 lines

  1. /*#! rexx*/
  2. /*****************************************************************
  3. * Juldate.cmd
  4. * Author Kurt A. Spaugh, Softlynks Inc.
  5. * Fort Lauderdale FL.
  6. * Copyleft (c) 1996
  7. * call greg2jul(gregdate) for julian day
  8. * call jul2greg(juldate) to convert julian day to gregorian date
  9. *
  10. * Useful: The naval observatory abreviates the julian date in 
  11. * their time transmissions as julianDay-2400000.
  12. * e.g. 10/13/1997 (2450735 Julian) is abbreviated as 50735
  13. * ...why they picked 11/16/1858 is beyond me...
  14. * Another common contraction subtracts  5/23/1968, or 2440000
  15. *
  16. * Bugs: they're yours to keep.  The code is free.
  17. *       None observed to date.
  18. *
  19. * Suggestions:
  20. * The code is a straight forward translation. Some economy
  21. * can be had by folding expressions. Is is worth it?
  22. * I used modulo arithmetic to int-ify real numbers.
  23. * Is trunc(exp) better/faster than epx%1 ??
  24. *
  25. * Notes:
  26. * Because of the change to Gregorian calendar,
  27. * Thursday Oct. 4,1582, is followed by Friday Oct. 15, 1582.
  28. * Entering these dates will produce correct, but odd, results:
  29. * 10/5/1582 is julian day 2299161.
  30. * Julian day 2299161 when converted to gregorian will return
  31. * 10/15/1582...this is normal, and correct, AFAIK
  32. *****************************************************************/
  33. /* sample main to try code */
  34. parse arg mm '/' dd '/' yy   /* must put in full year- eg 1999 */
  35. jul= greg2jul( mm,dd,yy)
  36. say 'Stardate:' jul
  37.  
  38. greg = jul2greg(jul)
  39. say greg 'is a' whatday(jul,'text')
  40.  
  41. DST=todst(yy)
  42. std=TOstd(yy)
  43. say 'In' yy||', DST starts on:' WHATDAY(dst,'t')||',' jul2greg(dst)
  44. say 'In' yy||', EST starts on:' WHATDAY(std,'t')||',' jul2greg(std)
  45.  
  46. if isleap(yy) then 
  47.   say yy 'is a leap year.'
  48. else
  49.   say yy 'is not a leap year.'
  50.  
  51. return
  52.  
  53. /*********************************************/
  54. /* Adapted from 'Numerical Recipes In C'     */
  55. /* Cambridge Press                           */
  56. /* Adaptation by Kurt Spaugh, Softlynks Inc. */
  57. /* Comments omitted. Some Math Buddah figured*/
  58. /* this out. It just works. Go Figure.       */
  59. /*********************************************/
  60. greg2jul: procedure
  61. do
  62.   parse arg mm,id,iyyy
  63.   igreg=(15+31*(10+12*1582))
  64.  
  65.   jy=iyyy;
  66.   if jy = 0 then
  67.   do
  68.     say "there is no year 0"
  69.     exit 255
  70.   end /* do */
  71.   if jy < 0 then jy=jy+1
  72.   if mm > 2 then jm=mm+1
  73.   else
  74.   do
  75.      jy=jy-1
  76.      jm=mm+13
  77.   end /* do */
  78.   jul=(365.25*jy)%1+(30.6001*jm)%1+id+1720995
  79.   if( id+31*(mm+12*iyyy) >= igreg) then
  80.   do
  81.      ja=(.01*jy)%1
  82.      jul=jul+(2-ja+(0.25*ja)%1)
  83.   end /* do */
  84.   return jul
  85. end
  86. /****************************************************************/
  87. /*jul2greg(juldate)  Also adapted from 'Numerical Recipes in C' */
  88. /*Adaptation by Kurt Spaugh, Softlynks Inc.                     */ 
  89. /****************************************************************/
  90. jul2greg: procedure 
  91. do
  92.    igreg=2299161
  93.    parse arg julian
  94.    if julian  >= igreg then
  95.    do
  96.       jalpha=(((julian-1867216)-0.25)/36524.25)%1
  97.       ja=julian+1+jalpha-(0.25*jalpha)%1
  98.    end /* do */
  99.    else
  100.      ja=julian
  101.    jb=ja+1524
  102.    jc=(6680+((jb-2439870)-122.1)/365.25)%1
  103.    jd=(365*jc+(0.25*jc))%1
  104.    je=(((jb-jd)/30.6001))%1
  105.    id=jb-jd-(30.6001*je)%1
  106.    mm=je-1
  107.    if(mm>12) then
  108.      mm=mm-12
  109.    iyyy=jc-4715
  110.    if( mm>2) then
  111.      iyyy=iyyy-1
  112.    if( iyyy <= 0 ) then
  113.      iyyy=iyyy-1
  114.    return mm||'/'||id||'/'||iyyy
  115. end /* do */
  116.  
  117. whatday:procedure
  118. parse arg jul,mode    /* if mode='' then just the remainder is returned */
  119.  x=(jul+1)//7
  120.  if mode \= '' then
  121.  select
  122.    when x=0 then return 'Sunday'
  123.    when x=1 then return 'Monday'
  124.    when x=2 then return 'Tuesday'
  125.    when x=3 then return 'Wednesday'
  126.    when x=4 then return 'Thursday'
  127.    when x=5 then return 'Friday'
  128.    when x=6 then return 'Saturday'
  129.  end
  130.  else
  131.    return x
  132.  
  133. /*
  134. *  when is first Sunday in April? 
  135. */
  136. ToDST:procedure        
  137. do
  138.    parse arg year
  139.    day1=greg2jul(4,1,year)
  140.    x=whatday(day1,'');
  141.    if(x > 0) then
  142.      x=7-x
  143.    return day1+x
  144. end /* do */
  145.  
  146. /*
  147. *  when is last Sunday in October?
  148. */
  149. ToSTD: procedure        
  150. do
  151.    parse arg year
  152.    day31=greg2jul(10,31,year)
  153.    x=whatday(day31,'');
  154.    return day31-x
  155. end /* do */
  156.  
  157. IsLeap: procedure
  158. do
  159.   parse arg year
  160.   if year = -1 then
  161.     adder=2
  162.   else
  163.      adder=1
  164.   if greg2jul(1,1,year+adder)-greg2jul(1,1,year) = 366 then
  165. /*  if greg2jul(1,1,year+1)-greg2jul(1,1,year) = 366 then*/
  166.     return 1
  167.   return 0
  168. end
  169.  
  170.  
  171.  
  172.  
  173.  
  174.