Announcement

Collapse
No announcement yet.

I need to truncate a string

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • aa6vh
    replied
    Originally posted by mikee123 View Post
    Ok think I am slowly getting it. As (no matter how I convert the Laston/LastOff) the way the values are stored, I cannot just subtract. I think on Vera that was possible, thats why I was trying the same here. Could have been another system. Anyway, I get that, that is why I need to use the DateDiff function. And to be sure it will always work, it would be best to add DateTime.Parse when retrieving the variables to make sure they are getting converted to a proper date format.

    The microsoft docs do not make 'light reading' ... quite hard to digest and usually triggers a barrage of swear words
    I hear you. You can't be a computer programmer unless you can do swear words in Hexadecimal.

    Vera and the LUA language uses the Linux date field, which is a long integer microsecond count from 1/1/1970. VB.Net uses a floating point number, with the whole number part being a count of days from (I beleive) 1/1/0000, and the fraction part holding the time of day (with .5 meaning noon). This is why simple subtraction worked in LUA, but on in vb.net.

    Leave a comment:


  • jon00
    replied
    This is one way to do it:

    Code:
    Sub Main(Byval Parm as String)
    Dim LastOn as Date
    Dim LastOff as Date
    Dim TS As Timespan
    Dim TimeCalc As Integer
    LastOn = DateTime.Parse(hs.GetIniSetting("Device6","LastOn","","Jon00UptimeDataHS3.ini"))
    LastOff = DateTime.Parse(hs.GetIniSetting("Device6","LastOff","","Jon00UptimeDataHS3.ini"))
    TS = LastOff - LastOn
    TimeCalc = TS.TotalMinutes
    hs.writelog("TimeCalc",TimeCalc.ToString)
    End Sub

    Leave a comment:


  • mikee123
    replied
    Ok think I am slowly getting it. As (no matter how I convert the Laston/LastOff) the way the values are stored, I cannot just subtract. I think on Vera that was possible, thats why I was trying the same here. Could have been another system. Anyway, I get that, that is why I need to use the DateDiff function. And to be sure it will always work, it would be best to add DateTime.Parse when retrieving the variables to make sure they are getting converted to a proper date format.

    The microsoft docs do not make 'light reading' ... quite hard to digest and usually triggers a barrage of swear words

    Leave a comment:


  • sparkman
    replied
    Originally posted by mikee123 View Post
    Yes I thought there was an issue, I tried just subtracting LastOn and LastOff, the resulting error messages pointed me in the direction of a wrong format. You cannot subtract strings I think. But even with Dim LastOn and LastOff as date, I could not subtract them.
    So if I did this:

    Code:
    LastOn = DateTime.Parse(hs.GetIniSetting("Device6","LastOn","","Jon00 UptimeDataHS3.ini"))
    LastOff = DateTime.Parse(hs.GetIniSetting("Device6","LastOn","","Jon00 UptimeDataHS3.ini"))
    I should be able to say

    Code:
    TimeCalc = LastOn - LastOff
    ?
    You need to use the DateDiff function: https://docs.microsoft.com/en-us/dot...ramework-4.7.2

    Leave a comment:


  • mikee123
    replied
    Yes I thought there was an issue, I tried just subtracting LastOn and LastOff, the resulting error messages pointed me in the direction of a wrong format. You cannot subtract strings I think. But even with Dim LastOn and LastOff as date, I could not subtract them.
    So if I did this:

    Code:
    LastOn = DateTime.Parse(hs.GetIniSetting("Device6","LastOn","","Jon00 UptimeDataHS3.ini"))
    LastOff = DateTime.Parse(hs.GetIniSetting("Device6","LastOn","","Jon00 UptimeDataHS3.ini"))
    I should be able to say

    Code:
    TimeCalc = LastOn - LastOff
    ?

    Leave a comment:


  • aa6vh
    replied
    Just a programming note: You are taking a string ("02/02/2019 07:36:12"), which is not a date field, and which needs to be converted into a date field before you can do the subtraction. (Basically a datefield internally is a number counting a number of time units, usually seconds or microseconds, from some arbitrary but consistant time in the past). Fortunately, your compiler is doing that conversion for you, based on how your code is written. But if you are not aware of this silent conversion, and you change the flow, things might stop working.

    The code could be written like this so that an explicit conversion is done:

    LastOn = DateTime.Parse(hs.GetIniSetting("Device6","LastOn","","Jon00 UptimeDataHS3.ini"))

    You can find documentation on the parse statement here:

    https://docs.microsoft.com/en-us/dot...ramework-4.7.2

    Leave a comment:


  • mikee123
    replied
    Multitasking - FAIL

    Trying to program while at work... copy pasted and forgot to change the variables, and didnt even see it

    Now its working

    Leave a comment:


  • jon00
    replied
    TotalHotWaterTime = hs.GetIniSetting("Device6","LastOn","","Jon00UptimeDataHS3.i ni")
    TotalHotWaterTime = hs.GetIniSetting("Device6","LastOff","","Jon00UptimeDataHS3. ini")

    Should be:

    LastOn = hs.GetIniSetting("Device6","LastOn","","Jon00UptimeDataHS3.i ni")
    Lastoff = hs.GetIniSetting("Device6","LastOff","","Jon00UptimeDataHS3. ini")

    Leave a comment:


  • mikee123
    replied
    Something is not quite right yet. I am at work so cannot check the Jon00UptimeDataHS3.ini, but LastOn and LastOff should be populated. But my script returns 0, I guess there is something else wrong

    Code:
    Dim LastOn as Date
    Dim LastOff as Date
    
    TotalHotWaterTime = hs.GetIniSetting("Device6","LastOn","","Jon00UptimeDataHS3.ini")
    TotalHotWaterTime = hs.GetIniSetting("Device6","LastOff","","Jon00UptimeDataHS3.ini")
    
    Dim TimeCalc As Long = DateDiff(DateInterval.Minute, LastOn, LastOff)

    Leave a comment:


  • mikee123
    replied
    OK that is great. Just got in so did not have a chance to look at the .ini, but that gives me another option of getting the total time for today, just retrieve LastOn and LastOff and subtract.

    Leave a comment:


  • jon00
    replied
    These are the metrics available:

    [Device1]
    LastOn=02/02/2019 07:36:12
    LastOff=03/02/2019 22:10:09
    Status=0
    LastTotalUptime=1.14:33:57
    CurrentUptime=1.14:33:57
    CurrentDays=1
    CurrentHours=14
    CurrentMinutes=33
    CurrentSeconds=57
    TotalDays=1
    TotalHours=38
    TotalMinutes=2313
    TotalSeconds=138837
    TotalUptime=1 day 14 hrs 33 mins 57 secs
    Counter=1
    LastReset=02/02/2019 07:36:12

    All can be read using the hs.GetIniSetting method I showed you earlier.

    Leave a comment:


  • mikee123
    replied
    Originally posted by jon00 View Post
    I'm at work at the moment but there are numerous metrics stored in theJon00UptimeDataHS3.ini

    Open it up in a text editor and have a peek.
    Yes I am at work too, but will have a look

    Leave a comment:


  • jon00
    replied
    I'm at work at the moment but there are numerous metrics stored in theJon00UptimeDataHS3.ini

    Open it up in a text editor and have a peek.

    Leave a comment:


  • mikee123
    replied
    I think I have found (with your help) an easier solution, 4:02am minus the actual stop time, which is I wanted this here. But your solution gives me something else to play with, I am sure I can use that for something else... :-)

    Leave a comment:


  • sooty
    replied
    Originally posted by mikee123 View Post
    Now I have a new 'problem'. I need to convert or retrieve the TotalHotWaterTime which is stored in the Jon00UptimeDataHS3.ini as a number (for a calculation, subtraction). It is stored as a string, so subtraction does not work. But can I either convert it to for example number of minutes, or is the total time stored somewhere as a number ?
    Assuming the structure of the input string is always the same, then you could do something like below to get the total number of seconds.

    Code:
     Function CalcTotalSeconds(sInput As String) As Long
            If sInput.Length > 0 Then
                Try
                    Dim arr() As String = sInput.Split(" ")
                    Dim iDays, iHours, iMinutes, iSeconds As Integer
    
                    For i As Integer = 0 To UBound(arr)
                        Select Case arr(i)
                            Case "days"
                                iDays = arr(i - 1)
    
                            Case "hrs"
                                iHours = arr(i - 1)
    
                            Case "mins"
                                iMinutes = arr(i - 1)
    
                            Case "secs"
                                iSeconds = arr(i - 1)
                        End Select
                    Next
    
                    Return (iDays * 86400) + (iHours * 3600) + (iMinutes * 60) + iSeconds
    
                Catch ex As Exception
                    Return -1
                End Try
            Else
                Return -1
            End If
    
        End Function
    Paul..

    Leave a comment:

Working...
X