Manipulating Old Dates

Mark Wieder mwieder at ahsoftware.net
Tue Feb 10 11:51:00 EST 2004


Ray-

Julian dates aren't all that hard to deal with - here are a couple of
functions I use for getting back and forth. If memory serves, julian
dates are useful back to 4716 BC, as long as you don't mind some
weirdness around 1582 AD when they switched calendar formats.

constant kDaysInYear = 365.25
constant kGregJD = 2299161

-- convert a YYYY MM DD to julian date
-- curtime is in "long time" format: HH:MM:SS
function GetJulian year, month, day, curtime
   local JulianDate
   local extra
   local uhr, umin, usec

   set the itemDelimiter to ":"
   put word 1 of curtime into uhr
   put word 2 of curtime into umin
   put word 3 of curtime into usec

   put 100 * year + month - 190002.5 into extra
   put 367 * year into JulianDate
   subtract trunc(7.0 * (year + trunc((month + 9) / 12)) / 4) from JulianDate
   add trunc(275 * month / 9 ) to JulianDate
   add day to JulianDate
   add (uhr + (umin + usec / 60) / 60) / 24 to JulianDate
   add 1721013.5 to JulianDate
   subtract .5 * extra / abs(extra) from JulianDate
   add .5 to JulianDate

   return JulianDate
end GetJulian

-- convert a julian date to yyyy.mm.dd.hh.mm.ss format
function FromJulian julian
   local tmp
   local dayt, month, year
   local uhr, umin, usec
   local a, b, c, d, e, f
   local jd

   put julian + .5 into jd
   put trunc(jd) into z
   put jd - z into f

   if (z >= kGregJD) then
     put trunc((z - 1867216.25)/36524.25) into tmp
     put z + 1 + tmp - trunc(tmp/4) into a
   else
     put z into a
   end if

   put a + 1524 into b
   put trunc((b - 122.1) / kDaysInYear) into c
   put trunc(kDaysInYear * c) into d
   put trunc((b - d) / 30.6001) into e

   put b - d - trunc(30.6001 * e) + f into dayt
   put trunc(dayt) into field "txtTempJulian1"

   if (e < 13.5) then
     put e-1 into month
   else
     put e-13 into month
   end if

   if (month > 2.5) then
     put c - 4716 into year
   else
     put c - 4715 into year
   end if

   put trunc(24 * (dayt - trunc(dayt))) into uhr
   put trunc(1440 * (dayt - trunc(dayt) - uhr/24)) into umin
   put 86400 * (dayt - trunc(dayt) - uhr/24 - umin/1440) into usec

   return year & "." & month & "." & trunc(dayt) & "." & uhr & "." &
   umin & "." & usec
   
end FromJulian

-- 
-Mark Wieder
 mwieder at ahsoftware.net



More information about the use-livecode mailing list