Calculate Time

I has some fun letting SQLite calculate it.

[code]Var inMemoryDB As New SQLiteDatabase
Var vSQL As String
// shift first part 09:05:38 -> 13:23:47 or 4:18:09
// lunch from 13:13:47 -> 13:54:02 or 0:41:45
// shift second part 13:54:02 -> 17:12:58 or 03:18:56
Var vShift_Start As String = “09:05:38”
Var vLunch_Start As String = “13:23:47”
Var vLunch_End As String = “13:54:02.5” // Even added down to the 10th of a second
Var vShift_End As String = “17:12:58.2” // Even added down to the 10th of a second

Var vFirstShiftTimeDouble As Double
Var vSecondShiftTimeDouble As Double
Var vTotalShiftTimeDouble As Double
Var vLunchTimeDouble As Double

Var vFirstShift As String
Var vSecondShift As String
Var vTotalShift As String
Var vTotalShiftUpTo_ms As String
Var vLunch As String

Try
inMemoryDB.Connect
Catch error As DatabaseException
inMemoryDB=nil
End Try

If inMemoryDB<>Nil Then
vSQL = “SELECT CAST((julianday(’” + vLunch_Start + “’) - julianday(’” + vShift_Start + “’)) * 24 As double) AS firstshifttimedouble,” + _
“CAST((julianday(’” + vShift_End + “’) - julianday(’” + vLunch_End + “’)) * 24 As double) AS secondshifttimedouble,” + _
“CAST((julianday(’” + vLunch_Start + “’) - julianday(’” + vShift_Start + "’)) * 24 As double) + " + _
“CAST((julianday(’” + vShift_End + “’) - julianday(’” + vLunch_End + "’)) * 24 As double) AS totalshifttimedouble, " + _
“CAST((julianday(’” + vLunch_End + “’) - julianday(’” + vLunch_Start + "’)) * 24 As double) AS lunchtimedouble, " + _
“time(CAST((julianday(’” + vLunch_Start + “’) - julianday(’” + vShift_Start + "’)) * 24 * 60 * 60 As double),‘unixepoch’) AS firstshift, " + _
“time(CAST((julianday(’” + vShift_End + “’) - julianday(’” + vLunch_End + "’)) * 24 * 60 * 60 As double),‘unixepoch’) AS secondshift, " + _
“time(CAST((julianday(’” + vLunch_Start + “’) - julianday(’” + vShift_Start + "’)) * 24 * 60 * 60 As double) + " + _
“CAST((julianday(’” + vShift_End + “’) - julianday(’” + vLunch_End + "’)) * 24 * 60 * 60 As double),‘unixepoch’) AS totalshift, " + _
“strftime(’%H:%M:%f’,CAST((julianday(’” + vLunch_Start + “’) - julianday(’” + vShift_Start + "’)) * 24 * 60 * 60 As double) + " + _
“CAST((julianday(’” + vShift_End + “’) - julianday(’” + vLunch_End + "’)) * 24 * 60 * 60 As double),‘unixepoch’) AS totalshift_up_to_ms, " + _
“time(CAST((julianday(’” + vLunch_End + “’) - julianday(’” + vLunch_Start + “’)) * 24 * 60 * 60 As double),‘unixepoch’) AS lunch;”
Var results As RowSet
Try
results = inMemoryDB.SelectSQL(vSQL)
If results <> Nil and Not results.AfterLastRow Then
//double values
vFirstShiftTimeDouble = results.ColumnAt(0).DoubleValue
vSecondShiftTimeDouble = results.ColumnAt(1).DoubleValue
vTotalShiftTimeDouble = results.ColumnAt(2).DoubleValue
vLunchTimeDouble = results.ColumnAt(3).DoubleValue
//string values
vFirstShift = results.ColumnAt(4).StringValue
vSecondShift = results.ColumnAt(5).StringValue
vTotalShift = results.ColumnAt(6).StringValue
vTotalShiftUpTo_ms = results.ColumnAt(7).StringValue
vLunch = results.ColumnAt(8).StringValue
results.Close
End If
Catch error As DatabaseException
MessageBox("Error: " + error.Message)
End Try
End If
[/code]

“i don’t see your input of strLncStrt & strLncEnd” – They are Parameters sent to the Method.

“Half is 0.5, not 0.3. It’s not immediately obvious why you’d get 0002777777777778 returned from anything though.” – Thanks for pointing out the .5…

As for the return I am getting, yeah, I am not understanding why either. Like I said the same code in a different method returns the work hours just fine. I have run multiple tests to make sure. But when testing the lunch time calculation… it’s not working?

This calculates hours. A 30 minute break would be 0.5, not 30.

What inputs are you using to test?

My guess is your inputs are bad. Garbage in, garbage out.

Better than that… in this code bit lies the error… --> dtLnchStrt.Minute = strLncStrt.Middle( 4, 2 ).ToInteger
This is what it SHOULD look like: dtLnchStrt.Minute = strLncStrt.Middle( 3, 2 ).ToInteger

you should use 60.0 to have a double value. or start analyse project.

ohh yes, the new commands are 0 based^^ …
i would check the boundaries for hours,minutes,seconds.

Or you could use Virtual TimeClock… $99 for up to 3 employees. How much is your time worth? We have over a million users. Made with Xojo since 1999 so you’re supporting the community. :wink:

@ Keith DeLong: I enjoy writing code… and if I can create my own solution for what I do… it’s worth my time and sanity. While I am a bit rusty ( last program I wrote was in 2009, a full blown utilities metering database used for utility use tracking and billing. Had to be a custom job as the UC I was working for was a unique animal ) so thank you but the enjoyment I get is well worth the time I invest… that and I own / control my product. I am also writing my own POS/Inventory that has features others don’t ( again we are a non-standard business ) or that they have but don’t function how I want. And to top that off, I use Linux/FreeBSD for my core OS’s. I don’t like the idea of the cloud and not controlling my systems/data directly. I suppose I am a bit of a control freak in that aspect. Thank you for the suggestion though, I will stick with writing my own solutions and the frustrations that come with it!