home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / hcshdemo.zip / csh-os2.zip / SAMPLES / JULIAN.CSH < prev    next >
Text File  |  1993-09-28  |  1KB  |  56 lines

  1. #    Calculate the Julian day number for the specified date.
  2. #    Based on algorithms published in Computer Language magazine,
  3. #    December, 1990.  Used with permission.
  4.  
  5. proc julian(month, day, year)
  6.     # The Julian day number is the number of days between noon GMT
  7.     # Jan 1, 4713 BC and that date.  (Years B.C. are negative.)
  8.     local a, b, year_corr, Month, i
  9.     set Month = January February March April May June ^
  10.         July August September October November December
  11.     if (year == '') then
  12.         local dt
  13.         set dt = `dt`
  14.         @ year = $dt:3
  15.         if (day == '') then
  16.             @ day = month == '' ? $dt:2 : month
  17.             @ month = $dt:1
  18.         end
  19.     end
  20.     if (month !~ '[0-9]*') then
  21.         @ month = "$lower(month)*"
  22.         for i = 0 to 11 do
  23.             if (lower(Month[i]) =~ month) break
  24.         end
  25.         @ month = i + 1
  26.     end
  27.     if (month < 1 || month > 12 || year < -4713 || year > 2900) then
  28.         echo -2 julian: Invalid Date
  29.         return -1
  30.     end
  31.     # Correct for negative year.
  32.     if (year < 0) then
  33.         @ year++
  34.         @ year_corr = 1
  35.         @ b = 1
  36.     else
  37.         @ year_corr = b = 0
  38.     end
  39.     if (month <= 2) then
  40.         @ year--
  41.         @ month += 12
  42.     end
  43.     # Cope with the Gregorian calendar reform
  44.     if (year * 10000 + month * 100 + day > 15821015) then
  45.         @ a = year//100
  46.         @ b = 2 - a + a//4
  47.     end
  48.     @ a = floor(365.25 * year - year_corr) + ^
  49.         floor(30.6001 * (month + 1)) + day + 1720994 + b
  50.     # 4713 BC was not a leap year.
  51.     if (a < 59) @ a++
  52.     return a
  53. end
  54.  
  55. julian $argv
  56.