Skip to content

Instantly share code, notes, and snippets.

@nonkit
Last active April 1, 2020 06:27
Show Gist options
  • Save nonkit/5aa70330bef3af0bb33c43749cf7569a to your computer and use it in GitHub Desktop.
Save nonkit/5aa70330bef3af0bb33c43749cf7569a to your computer and use it in GitHub Desktop.
Small Basic Pseudo Clock Object
Sub Clock_DateToJD
' Clock | convert date to Julian Day
' 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_Init
' Clock | initialize days of months
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
Sub Clock_JDNow
' Clock | get Julian Day for now
' return jd - Julian Day [day]
jd = Clock.ElapsedMilliseconds / 1000 / 3600 / 24 + 2415020.5
EndSub
Sub Clock_JDToDate
' Clock | convert Julian Day to date
' 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
@nonkit
Copy link
Author

nonkit commented May 16, 2019

Added comments for revision 2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment