Sunrise/Sunset times with LotusScript
Why would you want to do this? Why not? In my case, I just go tired of all the free services that provide this data going down for extended periods. That, and the uber-geeky desire to know how it was computed. I use this same code, translated to PHP, for my new Weather Plug-in that is a pure XML solution.
All you need is a day, month, year, latitude, longitude (both in DECIMAL format), suns zenith (90.833 degrees for civil twilight), boolean for sunrise, and your local offset from GMT. There is definitely a better way to grab the local time than parsing out the Timedate string, but I think I just got lazy near the end. Note: I originally did this in Notes R5 so the “rise As Variant” could be boolean now.
Function findTime(d As Integer,mon As Integer,yr As Integer, lat As Double,lon As Double,zen As Double,rise As Variant, offset As Integer) As String
N1 = Int(275*(mon/9))
N2 = Int((mon+9)/12)
N3 = (1+Int((yr-4*Int(yr/4)+2)/3))
N = N1-(N2*N3)+d-30
lngHour = lon/15
If rise Then
t = N+((6-lngHour)/24)
Else
t = N+((18-lngHour)/24)
End If
M = (0.9856*t)-3.289
L = M+(1.916*Sin(M*3.1415926/180))+ (0.020*Sin(2*(M*3.1415926/180)))+282.634
If L>360 Then
L= L-360
End If
RA = (Atn(0.91764*Tan(L*3.1415926/180))*180)/3.1415926
Lquadrant = Int(L/90)*90
RAquadrant = Int(RA/90)*90
RA = RA+(Lquadrant-RAquadrant)
RA = RA/15
sinDec = 0.39782*Sin(L*3.1415926/180)
cosDec = Cos(Asin(sinDec))
cosH = (Cos(zen*3.1415926/180)-(sinDec*Sin(lat*3.1415926/180))) / (cosDec* Cos(lat*3.1415926/180))
If rise Then
H = 360-(Acos(cosH)*180)/3.1415926
Else
H = (Acos(cosH)*180)/3.1415926
End If
H = H/15
TT = H+RA-(0.06571*t)-6.622
UT = TT-lngHour
If UT<0 Then
UT = UT+24
Elseif UT>24 Then
UT=UT-24
End If
Set endTime = New NotesDateTime(mon & "/" & d & "/" & yr & " " & Int(UT)+offset & ":" & Fraction(UT)*60 & ":" & "00")
findTime = Mid$(endTime.LocalTime, 12,5)
findTime = findTime + " " + Mid$(endTime.LocalTime,21,2)
End Function
April 17th, 2006 at 10:51 am
Thought this was cool — I tried this real quick and got bad results. I pasted your code as the function. I created a form with a button and the click has this call :
answer = findTime(2, 4, 2006, 41.51, 87.41, 90.833, True, -6)
Msgbox “Sunrise for Chicago = ” + answer
The answer was 5:28pm for Chicago. The Chicago Tribune said it was 6:08am. Is the zenith constant?
thanks,
troy…
April 17th, 2006 at 10:05 pm
Bad assumption on my part Troy; when referring to longitude in decimal format, the format is to use a negative longitude when west of the prime meridian, so your 87.41 should be -87.41. I should have specified that.
BTW, at the Naval Observatory site, they have the sunrise for your coordinates as 5:31 AM on April 2nd 2006. Since Chicago is NW of your location , they would have a later sunrise.