Sunset, sunrise, twilight calculations in LC?

Charles E Buchwald charles at buchwald.ca
Sat Aug 23 14:21:44 EDT 2014


Kay,
Thanks. I have been using Sarah Reichelt's Date & Time Library, which contains these functions by Mark Wieder. I actually didn't realize they were under copyright, but I see that now.
It says something nice about LC that this code is 10 years old and still valid and valuable.
- Charles

-- dateTimeToJulian(pYear, pMonth, pDate, [pHour], [pMins], [pSecs])
--
-- dateTimeToJulian()
-- dateTimeToJulian(2004,3,22)
-- dateTimeToJulian(2004,3,22,12,2)
-- dateTimeToJulian(2004,3,22,12,5,45)
--
-- This function returns the date and time in Julian date format.
-- If no date is specified, it will use the current date.
-- If supplied, the date must be in date items format: year, month, day, hour, minutes, seconds
-- This is because the internal date conversions may not work for early dates.
-- The time parameters are optional and seconds need not be specified.
-- 
-- Julian dates (abbreviated JD) are simply a continuous count of days and fractions
-- since noon Universal Time on January 1, 4713 BCE (on the Julian calendar).
-- Almost 2.5 million days have transpired since this date.
-- Julian dates are widely used as time variables within astronomical software.
-- Typically, a 64-bit floating point (double precision) variable can represent
-- an epoch expressed as a Julian date to about 1 millisecond precision.
-- Note that the time scale that is the basis for Julian dates is Universal Time,
-- and that 0h UT corresponds to a Julian date fraction of 0.5.
-- 
-- It is assumed that 7-day weeks have formed an uninterrupted sequence since ancient times.
-- Thus, the day of the week can be obtained from the remainder of the division of
-- the Julian date by 7.
--
-- This function is copyright 2004 Mark Wieder and Ah, Software
--
function dateTimeToJulian pYear, pMonth, pDate, pHour, pMins, pSecs
  if the paramcount < 3 then
    put the seconds into tNow
    convert tNow to dateItems
    put item 1 of tNow into pYear
    put item 2 of tNow into pMonth
    put item 3 of tNow into pDate
    put item 4 of tNow into pHour
    put item 5 of tNow into pMins
    put item 6 of tNow into pSecs
  end if
  
  if pHour is empty then put 0 into pHour
  if pMins is empty then put 0 into pMins
  if pSecs is empty then put 0 into pSecs
  
  -- calculate date part of Julian number
  put 100 * pYear + pMonth - 190002.5 into extra
  put 367 * pYear into julianDate
  subtract trunc(7.0 * (pYear + trunc((pMonth + 9) / 12)) / 4) from julianDate
  add trunc(275 * pMonth / 9 ) to julianDate
  add pDate to julianDate
   
  -- now add the time as a fractional day
  add (pHour + (pMins + pSecs / 60) / 60) / 24 to julianDate
  add 1721013.5 to julianDate
  subtract .5 * extra / abs(extra) from julianDate
   
  -- compensate for the fact the UTC starts at noon
  add .5 to julianDate
   
  return julianDate
end dateTimeToJulian



-- JulianToDateTime(pJulian)
--
-- JulianToDateTime(2453086.5)
-- JulianToDateTime(2453087.001389)
-- JulianToDateTime(2453087.003993)
--
-- This function returns the date in short English date format.
-- If the supplied Julian time includes a time component,
-- it returns the time in either long or short 24 hour format,
-- depending on whether the seconds are zero or not.
-- See dateTimeToJulian for more notes about Julian dates.
--
-- This function is copyright 2004 Mark Wieder and Ah, Software
--
function JulianToDateTime pJulian
   put trunc(pJulian + .5) into jd0
   if (jd0 < 2299161) then
      put jd0 + 1524 into c
   else
      put trunc((jd0 - 1867216.25) / 36524.25) into b
      put jd0 + (b - trunc(b/4)) + 1525 into c
   end if
   put trunc((c - 122.1)/365.25) into d
   put 365 * d + trunc(d/4) into e
   put trunc((c-e) / 30.6001) into f
    
   put trunc(c - e + .5) - trunc(30.6001 * f) into tDate
   put f - 1 - 12 * trunc(f/14) into tMonth
   put d - 4715 - trunc((7 + tMonth) /10) into tYear
    
   put 24 * (pJulian + .5 - jd0) into tempHour
   put trunc(tempHour * 60 + .5) / 60 into tHour
   put trunc(tHour) into tHour
    
   put (60 * (tempHour - tHour)) into tempMin
   put trunc(tempMin) into tMin
   put round(60 * (tempMin - tMin)) into tSec
    
   if tHour < 10 then put "0" before tHour
   if tMin < 10 then put "0" before tMin
   if tSec < 10 then put "0" before tSec
    
   if tHour = 0 and tMin = 0 and tSec = 0 then
      put tMonth & "/" & trunc(tDate) & "/" & tYear into tResult
   else if tSec = 0 then
      put tMonth & "/" & trunc(tDate) & "/" & tYear && tHour & ":" & tMin into tResult
   else
      put tMonth & "/" & trunc(tDate) & "/" & tYear && tHour & ":" & tMin & ":" & tSec into tResult
   end if
   return tResult
end JulianToDateTime()


On 23 Aug 2014, at 3:28 AM, Kay C Lan <lan.kc.macmail at gmail.com> wrote:

> Nope, but this will help you calculate the Julian Date Number, which
> you'll need:
> 
>   put the date into tDate
>   convert tDate from date to dateItems
>   if  ((item 2 of tDate = 1) or (item 2 of tDate = 2)) then
>      put 1 into tDay
>   else
>      put 0 into tDay
>   end if
>   put item 1 of tDate + 4800 - tDay into tYear
>   put item 2 of tDate + (12 * tDay) - 3 into tMonth
>   put item 3 of tDate + \
>         ((153 * tMonth + 2) div 5) + \
>         (365 * tYear) + \
>         (tYear div 4) - \
>         (tYear div 100) + \
>         (tYear div 400) - \
>         32045 into tJDN
> 
> On Sat, Aug 23, 2014 at 7:46 AM, Charles E Buchwald <charles at buchwald.ca> wrote:
>> Hi List People,
>> Does anyone know of an LC version of sunrise/sunset and twilight calculations?
>> [snip...]

--
Charles E. Buchwald
CEO/Director General
Museografica Digital
http://digital.museografica.com

Mac OSX 10.9.4, LC 6.6.2 Commercial

LC Developer Tools: http://buchwald.ca/developer-tools/

Email Notice: http://wp.me/P3aT4d-33





More information about the use-livecode mailing list