Here is the code translation for sun and tsun:
[CODE lang="b4x" title="sun & tsun"]' /**
' * Returns the Sunrise And Sunset times in an Array of two strings.
' * At Latitudes greater Then 65 Or smaller Then -65 there are cases that
' * there Is no sunrise And sunset. The result in this Case Is "--:--"
' */
Public Sub Sun( day As Int, month As Int , year As Int, latitude As Double , longitude As Double, deltahour As Double) As String()
Dim RiseSet(2) As String
' test the input
If ((day < 1 ) Or ( day > 31) Or (month<1) Or (month>12) Or(year <100)) Then
RiseSet(0) = "Date error"
RiseSet(1) = "Date error"
else if ((year = 1582) And (month = 10) And ( day > 4) And (day < 15)) Then
RiseSet(0) = "Date error"
RiseSet(1) = "Date error"
Else
Dim N1 As Int = 275 * month / 9
Dim N2 As Int = Floor((month + 9) / 12)
Dim N3 As Int = (1 + Floor((year - 4 *Floor(year / 4) + 2) / 3))
Dim N As Int = N1 - N2 * N3 + day - 30
RiseSet(0) = tsun( 0, N, latitude, longitude, deltahour) ' rise time
RiseSet(1) = tsun( 1, N, latitude, longitude, deltahour) ' set time
End If
Return RiseSet
End Sub
' use the library to get the value of zenith
Private Sub tsun(order As Int, N As Int, latit As Double, longit As Double, deltahour As Double) As String
Dim RiseSet(2) As String
Dim RtoD As Double= 180/cPI
' double DtoR = Math.PI/180 ;
Dim lngHour As Double = longit / 15
Dim H , TT , UT, localT , ht , mint As Double
Dim st1,st2 As String
Dim t As Double = N + (6 + 12 * order - lngHour) / 24
Dim M As Double= (0.9856 * t) - 3.289
Dim L As Double = M + (1.916 * SinD(M)) + (0.020 * SinD( 2 * M)) + 282.634
L = (L + 720 ) Mod 360
Dim RA As Double = RtoD * ATan(0.91764 * TanD(L))
RA = (RA + 720 ) Mod 360
Dim Lquadrant As Double = Floor( L/90) * 90
Dim RAquadrant As Double = Floor(RA/90) * 90
RA = RA + Lquadrant - RAquadrant
RA = (RA + 720 ) Mod 360
RA = RA / 15
Dim sinDec As Double = 0.39782 *SinD(L)
Dim cosDec As Double = Cos(ASin(sinDec))
Dim cosH As Double = (CosD(zenith) - (sinDec * SinD(latit))) / (cosDec * CosD(latit))
If Abs(cosH) > 1 Then
RiseSet(order)= "--:--"
Else
If order = 0 Then
H = 360 - RtoD * ACos(cosH)
Else
H = RtoD * ACos(cosH)
End If
H = H / 15
TT = H + RA - 0.06571 * t - 6.622
UT = TT - lngHour
localT = (UT + deltahour + 48) Mod 24
ht = Floor (localT)
mint = Floor((localT - Floor (localT)) *60 + 0.5 )
If (mint = 60) Then
ht = ht + 1
mint = 0
End If
If (ht < 10) Then
st1 = "0" & ht
Else
st1 = ht
End If
If (mint < 10.0 ) Then
st2 = "0" & Floor(mint)
Else
st2 = Floor(mint)
End If
RiseSet(order) = st1 & ":" & st2
If (st2.length = 1) Then RiseSet(order) = "--:--" ' Case that mint Is Null
End If
Return RiseSet(order)
End Sub
[/CODE]
I compared the results to the library and the results are the same.