Calculate Time

  1. ‹ Older
  2. 3 weeks ago

    Norman P

    Jun 19 Testers, Xojo Pro outside admiring the sunshine,...

    @Alberto D My comment was not directed at your code, just confirming that we don't have enough information to give the exact answer.

    In another note, I'm testing API 2.0, I need to learn, and it is interesting for me that if I use this:

    Var shiftTime As DateInterval = shiftEnd - shiftStart - lunchTime

    Where shiftEnd and shiftStart are DateTime and lunchTime is DateInterval I get:

    Undefined operator. Type DateInterval does not define "Operator_Subtract" with type DateInterval
    Var shiftTime As DateInterval = shiftEnd - shiftStart - lunchTime

    and if I change the code to:

    Var shiftTime As DateInterval = shiftEnd - lunchTime - shiftStart

    there is no error.

    I'd report this as a bug

  3. Alberto D

    Jun 19 Testers, Xojo Pro Austin, Texas

    @Ian M Dim stTime As String

    // Function(s)...
    If(dtDate.Hour) <= 9 Then
    stTime = "0" + CStr(dtDate.Hour)
    Else
    stTime = CStr(dtDate.Hour)
    End If

    If(dtDate.Minute) >= 10 Then
    stTime = stTime + ":" + CStr(dtDate.Minute) + ":"
    Else
    stTime = stTime + ":0" + CStr(dtDate.Minute) + ":"
    End If

    If(dtDate.Second) >= 10 Then
    stTime = stTime + CStr(dtDate.Second)
    Else
    stTime = stTime + "0" + CStr(dtDate.Second)
    End If

    Return stTime

    The function can be changed to:

    stTime = dtDate.SQLDateTime.Right(8)

    instead of 3 if/else/end

    You can keep using Date if it is a personal/small project because we have DateTime now, I think Date will stay for a long time. When you are ready you can start looking at DateTime.

  4. Okay this has been a frustrating issue I have run into of late:

    modSysFunc.mthCalcTime, line 25
    There is more than one method with this name but this does not match any of the available signatures.
    shiftStart.Second = Right( strStart.ToInteger, 2 )

    shiftStart.Hour = Left( strStart.ToInteger, 2 )
    shiftStart.Minute = Mid( strStart.ToInteger, 4, 2 )
    shiftStart.Second = Right( strStart.ToInteger, 2 )
    
    shiftEnd.Hour = Left( strEnd.ToInteger, 2 )
    shiftEnd.Minute = Mid( strEnd.ToInteger, 4, 2 )
    shiftEnd.Second = Right( strEnd.ToInteger, 2 )
    
    lunchStart.Hour = Left( strLncStrt.ToInteger, 2 )
    lunchStart.Minute = Mid( strLncStrt.ToInteger, 4, 2 )
    lunchStart.Second = Right( strLncStrt.ToInteger, 2 )
    
    lunchEnd.Hour = Left( strLncEnd.ToInteger, 2 )
    lunchEnd.Minute = Mid( strLncEnd.ToInteger, 4, 2 )
    lunchEnd.Second = Right( strLncEnd.ToInteger, 2 )

    Why is this error happening? I am using the code with my changes, provided by Hector. So this is a Module.Method I am writing to calculate hours for shift and lunch, and it's set up to return a String and takes 5 parameters.

    • Start Time :: strStart // Start of Shift
    • End Time :: strEnd // End of Shift
    • Lunch Start :: strLncStrt // Start Lunch
    • Lunch End :: strLncEnd // End Lunch
    • Which to Calculate

    But this error:
    modSysFunc.mthCalcTime, line 25
    There is more than one method with this name but this does not match any of the available signatures.
    shiftStart.Second = Right( strStart.ToInteger, 2 )

    modSysFunc.mthCalcTime, line 26
    There is more than one method with this name but this does not match any of the available signatures.

    modSysFunc.mthCalcTime, line 27
    There is more than one method with this name but this does not match any of the available signatures.
    shiftEnd.Hour = Left( strEnd.ToInteger, 2 )

    modSysFunc.mthCalcTime, line 29
    There is more than one method with this name but this does not match any of the available signatures.
    shiftEnd.Second = Right( strEnd.ToInteger, 2 )

    modSysFunc.mthCalcTime, line 30
    There is more than one method with this name but this does not match any of the available signatures.

    modSysFunc.mthCalcTime, line 31
    There is more than one method with this name but this does not match any of the available signatures.
    lunchStart.Hour = Left( strLncStrt.ToInteger, 2 )

    modSysFunc.mthCalcTime, line 33
    There is more than one method with this name but this does not match any of the available signatures.
    lunchStart.Second = Right( strLncStrt.ToInteger, 2 )

    modSysFunc.mthCalcTime, line 34
    There is more than one method with this name but this does not match any of the available signatures.

    modSysFunc.mthCalcTime, line 35
    There is more than one method with this name but this does not match any of the available signatures.
    lunchEnd.Hour = Left( strLncEnd.ToInteger, 2 )

    you get the point... why am I getting this error?

  5. Alberto D

    Jun 20 Testers, Xojo Pro Austin, Texas
    Edited 3 weeks ago

    Left, Mid and Right need string, you are using strLncStrt.ToInteger (for example)

    Is strLncStrt a String? What format does it have?

    Edit: If strLncStrt has, for example, 01:10:15 and you want to get the hours, you can use strLncStrt.Left(2), seconds will be strLncStrt.Right(2). You can use Left(strLncStrt, 2) and Right(strLncStrt, 2) if you prefer.

    Edit2: then you need to convert the String to Integer if you are going to assign that number to a Date

  6. Alberto D

    Jun 20 Testers, Xojo Pro Austin, Texas

    Example: change this code

    shiftStart.Hour = Left( strStart.ToInteger, 2 )

    to

    shiftStart.Hour = strStart.Left(2).ToInteger

    Note: .ToInteger doesn't work in older Xojo versions, you can use .Val instead, that worked with Xojo2018r3

  7. Thom M

    Jun 20 Greater Hartford Area, CT

    Since you’re dealing with timeclocks and people’s money, I strongly recommend you consider the time zone. Either use Date.GMTOffset * 3600 to get the offset in seconds, or set Date.GMTOffset to 0 so you’re always working in GMT. I’d say in 99% of normal usage, ignoring the time zone would work fine. But this is money we’re talking about, where 99% accuracy isn’t good enough.

  8. Dale A

    Jun 20 San Diego, California, USA

    @Norman P: Albert posted this observation earlier in this thread.

    In another note, I'm testing API 2.0, I need to learn, and it is interesting for me that if I use this:

    Var shiftTime As DateInterval = shiftEnd - shiftStart - lunchTime

    Where shiftEnd and shiftStart are DateTime and lunchTime is DateInterval I get:

    Undefined operator. Type DateInterval does not define "Operator_Subtract" with type DateInterval
    Var shiftTime As DateInterval = shiftEnd - shiftStart - lunchTime

    and if I change the code to:

    Var shiftTime As DateInterval = shiftEnd - lunchTime - shiftStart

    there is no error.

    You replied that he should report it as a bug. I'm confused why it is a bug?

    According to the documentation for DateTime

    Operators
    
        You can use the + operator to add a DateTime and an Interval together to get a new DateTime.
        You can use the - operator to subtract a DateInterval from a DateTime to get a new DateTime.
        You can use the - operator to subtract a DateTime from a DateTime to get a new DateInterval.

    In Albert's first example, he's subtracting a DateTime from a DateTime, which yields a DateInterval, and then tries to subtract a DateInterval from that, which is an error. In his second example, he is subtracting a Dateinterval from a DateTime, which yields a DateTime, and then subtracts a DateTime from that which yields a DateInterval and is correct.

    So I'm not sure where you see something that should be reported as a bug.

  9. Markus R

    Jun 20 Testers, Xojo Pro Europe / Germany / Lower Saxon...
    Edited 3 weeks ago

    @Ian M So I have to calculate the lunch time taken and the total hours of a shift. Once I get that the rest is easy peasy. So... help?!

    if you have 2 datetimes objects (Api 2) use:

    System.DebugLog "Hours " + Str((d2.SecondsFrom1970 - d1.SecondsFrom1970) / 3600.0)

    one hour have 3600 seconds or 60*60

    you should always log a datetime so you can calculate from before midnight to next day morning.

  10. I am having issues with the Lunch time Calculation
    Here's the code:

    ' -- Preliminary Code --
    
    ' :--* None Defined * --:
    
    ' Primary Variable Declaration(s)
    Dim dtLnchStrt As New Date
    Dim dtLnchEnd As New Date
    Dim dblLnchHrs As Double
    Dim strLnchHrs As String
    
    ' Function(s) / Logic...
    dtLnchStrt.Hour = strLncStrt.Left( 2 ).ToInteger
    dtLnchStrt.Minute = strLncStrt.Middle( 4, 2 ).ToInteger
    dtLnchStrt.Second = strLncStrt.Right( 2 ).ToInteger
    
    dtLnchEnd.Hour = strLncEnd.Left( 2 ).ToInteger
    dtLnchEnd.Minute = strLncEnd.Middle( 4, 2 ).ToInteger
    dtLnchEnd.Second = strLncEnd.Right( 2 ).ToInteger
    
    // Calculate shift hours...
    dblLnchHrs = dtLnchEnd.TotalSeconds - dtLnchStrt.TotalSeconds
    dblLnchHrs = (dblLnchHrs / 60) / 60 
    strLnchHrs = dblLnchHrs.ToString
    
    If( dblLnchHrs >= 30 ) Then
      MessageBox( "Total Lunch Hours: " + dblLnchHrs.ToString )
      Return strLnchHrs
    Else
      MessageBox( "In Accordance with California State Law, you are required to take exactly 30 minutes for your lunch break. During this time you are not paid as is it your time." )
      Return strLnchHrs
    End If

    I am expecting .30 or 30 in return... and no... I get this instead --> "0002777777777778"
    The shift time calculations is working perfectly... but the Lunch calcs are not returning as expected. What am I doing wrong

  11. Markus R

    Jun 22 Testers, Xojo Pro Europe / Germany / Lower Saxon...

    @Ian M strLncStrt

    i don't see your input of strLncStrt & strLncEnd

    thant is wrong you compare hours with minutes ;)

    If( dblLnchHrs >= 30 ) Then

    use a break point and see debugger values and step through

    try this to make a double to string

    Var n2 As Double = 12
    Var s2 As String = n.ToString(Locale.Current, "#.00") // s2 = 12.00
  12. Thom M

    Jun 22 Greater Hartford Area, CT

    Half is 0.5, not 0.3. It's not immediately obvious why you'd get 0002777777777778 returned from anything though.

  13. Dirk C

    Jun 22 Testers, Xojo Pro Belgium, Zedelgem

    I has some fun letting SQLite calculate it.

    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
  14. "i don't see your input of strLncStrt & strLncEnd" -- They are Parameters sent to the Method.

  15. "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?

  16. Tim H

    Jun 22 Portland, OR USA

    @Ian M dblLnchHrs = (dblLnchHrs / 60) / 60

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

    What inputs are you using to test?

  17. Thom M

    Jun 22 Greater Hartford Area, CT

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

  18. 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

  19. 2 weeks ago

    Markus R

    Jun 22 Testers, Xojo Pro Answer Europe / Germany / Lower Saxon...
    Edited 2 weeks ago

    @Ian M dblLnchHrs = (dblLnchHrs / 60) / 60

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

    strLncStrt.Middle( 3, 2 ).ToInteger

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

  20. Keith D

    Jun 23 Xojo Pro

    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. ;-)

  21. @ 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!

or Sign Up to reply!