Author Topic: DateTime issues  (Read 296 times)

Nick_W

  • Full Member
  • ***
  • Posts: 215
    • View Profile
Re: DateTime issues
« on: January 11, 2017, 02:17:16 pm »
For your interest, here is my "Unix" style timestamp function. Bit of a kludge, but it works:

Code: [Select]
    Public Function GetTimestamp(format As Byte, TimestampOffset As Integer) As String
        'get unix type timestamp with different formats:
        '1: return just seconds
        '2: return seconds as milliseconds (ie ends in 000)
        '3: return milliseconds (fudge with GetTimeSpan())
        '4: return seconds.milliseconds (fudge with GetTimeSpan())
        'add offset 946670400 (seconds) to get epoch as unix format ie since 1st Jan 1970
        'otherwise epoc is 1st Jan 2000
        'return as string as we do not have a long data type
        'Shared MillisecondsOffset As Integer defined in Class
        If (TimestampOffset = -1) Then
            TimestampOffset = 946670400
        End If
       
        Dim Timestamp_seconds_string As String = (DateTime.Now().ToInteger() + TimestampOffset).ToString()
        Dim Timestamp_microseconds As Integer = Device.GetTimeSpan()
        Dim Timestamp_milliseconds As Integer = ((Timestamp_microseconds % 1000000) / 1000).ToInteger()
       
        If Timestamp_microseconds < 0 Then 'account for overflow
            Timestamp_milliseconds = 0
            MillisecondsOffset = 0
        End If
       
        Timestamp_milliseconds = (Timestamp_milliseconds + MillisecondsOffset) % 1000
        MillisecondsOffset = Timestamp_milliseconds
       
        Dim Timestamp_milliseconds_string As String = Timestamp_milliseconds.ToString("D3")
       
        'If I could figure out what the string formatters are, wouldn't need this
        '"D3", and "000" don't work.
        If (Timestamp_milliseconds_string.Length() < 3) Then
            If (Timestamp_milliseconds_string.Length() = 1) Then
                Timestamp_milliseconds_string = "00" + Timestamp_milliseconds_string
            ElseIf (Timestamp_milliseconds_string.Length() = 2) Then
                Timestamp_milliseconds_string = "0" + Timestamp_milliseconds_string
            Else
                Timestamp_milliseconds_string = "000"
            End If
        End If
       
        Dim formatted_string As String
       
        Select format
            Case 1
                formatted_string = Timestamp_seconds_string
            Case 2
                formatted_string = Timestamp_seconds_string + "000"
            Case 3
                formatted_string = Timestamp_seconds_string + Timestamp_milliseconds_string
            Case 4
                formatted_string = Timestamp_seconds_string + "." + Timestamp_milliseconds_string
            Case Else
                formatted_string = Timestamp_seconds_string + Timestamp_milliseconds_string
        End Select
        //Dim jDebug As Json = New Json
        //jDebug.Add("Timestamp_seconds_string", Timestamp_seconds_string)
        //jDebug.Add("Timestamp_microseconds", Timestamp_microseconds)
        //jDebug.Add("Timestamp_milliseconds_string", Timestamp_milliseconds_string)
        //jDebug.Add("formatted_string", formatted_string)
        //MQTT.Publish("debug", jDebug)
        Return formatted_string
    End Function

here is the example output, the values between the json mqtt posts are decoded beacons:

Code: [Select]
MCThings/000111B4/Rssi -61
MCThings/000111B4/Data {"time":"1484165430738","Door":true}
MCThings/000111B4/Data {"time":"1484165430969","Door":false}
MCThings/000111B4/Door 0.0
MCThings/000111B4/Rssi -54
MCThings/000111B4/Door 0.0
MCThings/000111B4/Rssi -66
MCThings/000111B4/Data {"time":"1484165452945","Door":true}
MCThings/000111B4/Data {"time":"1484165453178","Door":false}
MCThings/000111B4/Door 0.0
MCThings/000111B4/Rssi -53
MCThings/000111B4/Door 1.0
MCThings/000111B4/Rssi -64
MCThings/000111B4/Data {"time":"1484165464987","Door":true}
MCThings/000111B4/Data {"time":"1484165465223","Door":false}
MCThings/000111B4/BatteryVoltage 2875.0
MCThings/000111B4/Rssi -58
MCThings/000111B4/Data {"time":"1484165479287","Uptime":120,"UptimeString":"0:0:2:0","BatteryVoltage":2875,"Temperature":24.437500,"Door":false,"KnockEnable":true,"PublishEnable":true}
MCThings/000111B4/Door 0.0
MCThings/000111B4/Rssi -64
MCThings/000111B4/Data {"time":"1484165500901","Door":true}
MCThings/000111B4/Data {"time":"1484165501094","Door":false}
MCThings/000111B4/Data {"time":"1484165501306","Door":true}
MCThings/000111B4/Data {"time":"1484165501451","Door":false}
MCThings/000111B4/Data {"time":"1484165501672","Door":true}
MCThings/000111B4/Data {"time":"1484165501848","Door":false}
MCThings/000111B4/Door 0.0
MCThings/000111B4/Rssi -52
MCThings/000111B4/Door 0.0
MCThings/000111B4/Rssi -72
MCThings/000111B4/Data {"time":"1484165533038","Door":true}
MCThings/000111B4/Data {"time":"1484165533172","Door":false}
MCThings/000111B4/Data {"time":"1484165533408","Door":true}
MCThings/000111B4/Data {"time":"1484165533489","Door":false}
MCThings/000111B4/Data {"time":"1484165534060","Door":true}
MCThings/000111B4/Data {"time":"1484165534134","Door":false}
MCThings/000111B4/Data {"time":"1484165534387","Door":true}
MCThings/000111B4/Data {"time":"1484165534482","Door":false}
MCThings/000111B4/Data {"time":"1484165534724","Door":true}
MCThings/000111B4/Data {"time":"1484165534849","Door":false}
MCThings/000111B4/Data {"time":"1484165535088","Door":true}
MCThings/000111B4/Data {"time":"1484165535211","Door":false}
MCThings/000111B4/Data {"time":"1484165535457","Door":true}
MCThings/000111B4/Data {"time":"1484165535579","Door":false}
MCThings/000111B4/Data {"time":"1484165535803","Door":true}
MCThings/000111B4/Data {"time":"1484165535947","Door":false}
MCThings/000111B4/Data {"time":"1484165536180","Door":true}
MCThings/000111B4/Data {"time":"1484165536299","Door":false}
MCThings/000111B4/Data {"time":"1484165536546","Door":true}
MCThings/000111B4/Data {"time":"1484165536690","Door":false}
MCThings/000111B4/Data {"time":"1484165536937","Door":true}
MCThings/000111B4/Data {"time":"1484165537106","Door":false}
MCThings/000111B4/Data {"time":"1484165537319","Door":true}
MCThings/000111B4/Data {"time":"1484165537507","Door":false}
MCThings/000111B4/Data {"time":"1484165537731","Door":true}
MCThings/000111B4/Data {"time":"1484165537891","Door":false}
MCThings/000111B4/Data {"time":"1484165538120","Door":true}
MCThings/000111B4/Data {"time":"1484165538245","Door":false}
MCThings/000111B4/Data {"time":"1484165538485","Door":true}
MCThings/000111B4/Data {"time":"1484165538658","Door":false}
MCThings/000111B4/Data {"time":"1484165538869","Door":true}
MCThings/000111B4/Data {"time":"1484165539061","Door":false}
MCThings/000111B4/Data {"time":"1484165539286","Door":true}
MCThings/000111B4/Data {"time":"1484165539317","Uptime":180,"UptimeString":"0:0:3:0","BatteryVoltage":2861,"Temperature":24.250000,"Door":true,"KnockEnable":true,"PublishEnable":true}
MCThings/000111B4/Data {"time":"1484165539439","Door":false}
MCThings/000111B4/Data {"time":"1484165539674","Door":true}
MCThings/000111B4/Data {"time":"1484165539832","Door":false}
MCThings/000111B4/Data {"time":"1484165540037","Door":true}
MCThings/000111B4/Data {"time":"1484165540220","Door":false}
MCThings/000111B4/Data {"time":"1484165540429","Door":true}
MCThings/000111B4/Data {"time":"1484165540615","Door":false}
MCThings/000111B4/Data {"time":"1484165540806","Door":true}
MCThings/000111B4/Data {"time":"1484165541050","Door":false}
MCThings/000111B4/Door 0.0
MCThings/000111B4/Rssi -54
MCThings/000111B4/Data {"time":"1484165599361","Uptime":240,"UptimeString":"0:0:4:0","BatteryVoltage":2872,"Temperature":24.000000,"Door":false,"KnockEnable":true,"PublishEnable":true}
MCThings/000111B4/Door 0.0
MCThings/000111B4/Rssi -56
MCThings/000111B4/Door 0.0
MCThings/000111B4/Rssi -56
MCThings/000111B4/Door 0.0
MCThings/000111B4/Rssi -57
MCThings/000111B4/Door 0.0
MCThings/000111B4/Rssi -57
MCThings/000111B4/Data {"time":"1484165659119","KnockEnable":true,"PublishEnable":true}
MCThings/000111B4/Data {"time":"1484165659410","Uptime":300,"UptimeString":"0:0:5:0","BatteryVoltage":2872,"Temperature":23.750000,"Door":false,"KnockEnable":true,"PublishEnable":true}

And yes, this is me going crazy with a magnet over a 5 minute period to see what happens when you overload the mqtt queue.
Note: if you try to use Lplan.SendBeacon() and publish to mqtt at the same time, the module resets.
« Last Edit: January 11, 2017, 02:35:39 pm by Nick_W »