Sub Process_Globals
Type Test(d1 As String, t1 As String, d2 As String, t2 As String, result As Int)
Dim SQL1 As SQL
Dim txt, OrigDateFormat, OrigTimeFormat As String
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
End Sub
Sub CreateTest(d1 As String, t1 As String, d2 As String, t2 As String) As Test
Dim t As Test
t.Initialize
t.d1 = d1
t.d2 = d2
t.t1 = t1
t.t2 = t2
Return t
End Sub
Sub Activity_Create(FirstTime As Boolean)
Dim tests() As Test = Array As Test( _
CreateTest("1/17/2012", "16:30:00", "1/17/2013", "16:30:00"), _
CreateTest("1/17/2013", "16:30:00", "1/17/2014", "16:30:00"), _
CreateTest("03/28/2013", "05:00:00", "03/29/2013", "05:00:00"))
'Calculate correct results
For Each t As Test In tests
t.result = HoursBetween(t.d1, t.t1, t.d2, t.t2)
Log(t.result)
Next
Log("Galimpic")
For Each t As Test In tests
Dim r As Int = GalimpicHoursBetween(t.d1, t.t1, t.d2, t.t2)
If r <> t.result Then Log("ERROR!!!!!! " & r & " " & t)
Next
Log("Mahares")
For Each t As Test In tests
Dim r As Int = MaharesHoursBetween(t.d1, t.t1, t.d2, t.t2)
If r <> t.result Then Log("ERROR!!!!!! " & r & " " & t)
Next
Log("Sorex")
For Each t As Test In tests
Dim r As Int = SorexHoursBetween(t.d1, t.t1, t.d2, t.t2)
If r <> t.result Then Log("ERROR!!!!!! " & r & " " & t)
Next
Log("Roger") 'GOOD JOB!!!
For Each t As Test In tests
Dim r As Int = RogerHoursBetween(t.d1 & " " & t.t1, t.d2 & " " & t.t2)
If r <> t.result Then Log("ERROR!!!!!! " & r & " " & t)
Next
End Sub
Sub SorexHoursBetween(StartDate As String,StartTime As String,Enddate As String, EndTime As String) As Int
startticks=DateTime.DateParse(StartDate)+DateTime.timeparse(StartTime)
endticks=DateTime.DateParse(Enddate)+DateTime.timeparse(EndTime)
Return (endticks-startticks)/DateTime.TicksPerHour
End Sub
Sub GalimpicHoursBetween(StartDate As String, StartTime As String, _
EndDate As String, EndTime As String) As Int
Dim ed As Long = DateTime.DateParse(EndDate)
Dim sd As Long = DateTime.DateParse(StartDate)
Dim et As Long = DateTime.TimeParse(EndTime)
Dim st As Long = DateTime.TimeParse(StartTime)
Dim ticks As Long = ed - sd + et - st
Dim hours As Int = ticks / 3600000
Return hours
End Sub
Sub MaharesHoursBetween(StartDate As String, StartTime As String, EndDate As String, EndTime As String) As Int
OrigDateFormat=DateTime.DateFormat
OrigTimeFormat=DateTime.Timeformat
Dim MyHours As Int
Dim ED,ET,SD,ST As Long
SD=DateTime.DateParse(StartDate) :ST=DateTime.TimeParse(StartTime)
ED=DateTime.DateParse(EndDate) :ET=DateTime.TimeParse(EndTime)
DateTime.DateFormat = "yyyy-MM-dd"
DateTime.TimeFormat = "HH:mm:ss"
StartDate=DateTime.Date(SD) :StartTime=DateTime.Time(ST)
EndDate=DateTime.Date(ED) :EndTime=DateTime.Time(ET)
SQL1.Initialize("","",False) 'Create and initialize a dummy database
txt= "SELECT julianday('" & EndDate & " " & EndTime & "') - julianday('" & StartDate & " " & StartTime & "')"
MyHours= SQL1.ExecQuerySingleResult(txt) *24
DateTime.DateFormat=OrigDateFormat 'return format to original
DateTime.Timeformat=OrigTimeFormat
Return MyHours
End Sub
Sub RogerHoursBetween(StartDate As String, Enddate As String) As Int
DateTime.DateFormat= "MM/dd/yyyy HH:mm:ss"
Return (DateTime.DateParse(Enddate)-DateTime.DateParse(StartDate))/DateTime.TicksPerHour
End Sub
Sub HoursBetween(StartDate As String, StartTime As String, _
EndDate As String, EndTime As String) As Int
Dim s, e As Long
s = ParseDateAndTime(StartDate, StartTime)
e = ParseDateAndTime(EndDate, EndTime)
Return (e - s) / DateTime.TicksPerHour
End Sub
Sub ParseDateAndTime(d As String, t As String) As Long
Dim dd = DateTime.DateParse(d), tt = DateTime.TimeParse(t) As Long
tt = (tt + DateTime.TimeZoneOffset * DateTime.TicksPerHour) Mod DateTime.TicksPerDay
Dim total As Long
total = dd + tt + _
(DateTime.GetTimeZoneOffsetAt(dd) - DateTime.GetTimeZoneOffsetAt(dd + tt)) _
* DateTime.TicksPerHour
Return total
End Sub