Sub RandomTest
For i = i To 40
jdSave = Math.GetRandomNumber(4000000) - 0.5
jd = jdSave
Clock_JDToDate()
Clock_DateToJD()
If jd = jdSave Then
TextWindow.ForegroundColor = "Green"
TextWindow.WriteLine("Test " + i + ": jd=" + jd + " date=" + date + " : PASS")
pass = pass + 1
Else
TextWindow.ForegroundColor = "Red"
TextWindow.WriteLine("Test " + i + ": jd=" + jd + " date=" + date + " : FAILED")
failed = failed + 1
EndIf
EndFor
n = 40
EndSub
Sub Run
While "True"
If buttonClicked Then
date = Controls.GetTextBoxText(tboxDate1)
Clock_DateToJD()
jd1 = jd
Controls.SetTextBoxText(tboxJD1, jd1)
date = Controls.GetTextBoxText(tboxDate2)
Clock_DateToJD()
jd2 = jd
Controls.SetTextBoxText(tboxJD2, jd2)
days = jd2 - jd1
Controls.SetTextBoxText(tboxDays, days)
buttonClicked = "False"
Else
Program.Delay(200)
EndIf
EndWhile
EndSub
Sub UnitTest
n = Array.GetItemCount(testCase)
For i = 1 To n
jd = testCase[i]["JD"]
Clock_JDToDate()
failure = "False"
If date = testCase[i]["Date"] Then
TextWindow.ForegroundColor = "Green"
TextWindow.WriteLine("Test " + i + ": JDToDate(" + jd + ")=" + date + " : PASS")
Else
TextWindow.ForegroundColor = "Red"
TextWindow.WriteLine("Test " + i + ": JDToDate(" + jd + ")=" + date + " : FAILED")
failure = "True"
EndIf
date = testCase[i]["Date"]
Clock_DateToJD()
If jd = testCase[i]["JD"] Then
TextWindow.ForegroundColor = "Green"
TextWindow.WriteLine("Test " + i + ": DateToJD(" + date + ")=" + jd + " : PASS")
Else
TextWindow.ForegroundColor = "Red"
TextWindow.WriteLine("Test " + i + ": DateToJD(" + date + ")=" + jd + " : FAILED")
failure = "True"
EndIf
If failure Then
falied = failed + 1
Else
pass = pass + 1
EndIf
EndFor
EndSub
Sub Clock_DateToJD
' param date - date formatted as YYYY/MM/DD
' return jd - Julian Day [day]
s1 = Text.GetIndexOf(date, "/")
year = Text.ConvertToUpperCase(Text.GetSubText(date, 1, s1 - 1))
bc = Text.GetIndexOf(year, "BC")
If 0 < bc Then
year = Text.GetSubText(year, 1, bc - 1) + Text.GetSubTextToEnd(year, bc + 2)
year = -(year - 1)
EndIf
s2 = Text.GetIndexOf(Text.GetSubTextToEnd(date, s1 + 1), "/") + s1
month = Text.GetSubText(date, s1 + 1, s2 - s1 - 1)
day = Text.GetSubTextToEnd(date, s2 + 1)
jc = "False" ' Julian calendar
If year < 1582 Then
jc = "True"
ElseIf year = 1582 Then
If month < 10 Then
jc = "True"
ElseIf month = 10 Then
If day <= 4 Then
jc = "True"
ElseIf 4 < day And day < 15 Then
TextWindow.WriteLine("ERROR:Invalid date " + date)
EndIf
EndIf
EndIf
If jc Then ' Julian calendar
y4801BC = year + 4800
d4801BC = Math.Ceiling(y4801BC * 365.25)
If Math.Remainder(y4801BC, 4) = 0 Then
dom[2] = 29
Else
dom[2] = 28
EndIf
For mm = 1 To month - 1
d4801BC = d4801BC + dom[mm]
EndFor
d4801BC = d4801BC + day - 1
jd = d4801BC - 32142.5
Else ' Gregorian calendar
y1200 = year - 1200
nol = Math.Ceiling(y1200 / 4) - Math.Ceiling(y1200 / 100) + Math.Ceiling(y1200 / 400) ' number of leap year
d1200 = y1200 * 365 + nol
If Math.Remainder(y1200, 4) = 0 And Math.Remainder(y1200, 100) <> 0 Or Math.Remainder(y1200, 400) = 0 Then
dom[2] = 29
Else
dom[2] = 28
EndIf
For mm = 1 To month - 1
d1200 = d1200 + dom[mm]
EndFor
d1200 = d1200 + day - 1
jd = d1200 + 2159350.5
EndIf
EndSub
Sub Clock_JDToDate
' param jd - Julian Day [day] ≧ -32142.5 (-4801 BC/01/01)
' return date - date formatted as YYYY/MM/DD
If jd < 2299160.5 Then ' Julian calendar
d4801BC = Math.Floor(jd + 0.5) - 0.5 + 32142.5
year = (-4800) + Math.Floor(d4801BC / 365.25)
If Math.Remainder(year, 4) = 0 Then
dom[2] = 29
Else
dom[2] = 28
EndIf
day = Math.Floor(Math.Remainder(d4801BC, 365.25))
If year < 1 Then
year = (Math.Abs(year) + 1) + " BC"
EndIF
Else ' Gregorian calendar
d1200 = Math.Floor(jd + 0.5) - 0.5 - 2159350.5
y1200 = Math.Floor(d1200 / 365.2425)
year = 1200 + y1200
If Math.Remainder(year, 4) = 0 And Math.Remainder(year, 100) <> 0 Or Math.Remainder(year, 400) = 0 Then
dom[2] = 29
Else
dom[2] = 28
EndIf
nol = Math.Ceiling(y1200 / 4) - Math.Ceiling(y1200 / 100) + Math.Ceiling(y1200 / 400) ' number of leap year
day = d1200 - nol
day = Math.Floor(Math.Remainder(d1200 - nol, 365))
EndIf
For mm = 1 To 12
If day < dom[mm] Then
month = mm
mm = 12 ' exit For
Else
day = day - dom[mm]
EndIf
EndFor
If month < 10 Then
month = Text.Append(0, month * 1)
EndIf
day = day + 1
If day < 10 Then
day = Text.Append(0, day * 1)
EndIf
date = year + "/" + month + "/" + day
EndSub
Sub Clock_Init
dom = "1=31;2=28;3=31;4=30;5=31;6=30;7=31;8=31;9=30;10=31;11=30;12=31;"
EndSub