Author Topic: DateTime issues  (Read 270 times)

Nick_W

  • Full Member
  • ***
  • Posts: 215
    • View Profile
DateTime issues
« on: November 08, 2016, 07:54:33 am »
I was constructing an uptime string recently, and found that DateTime.AddSeconds() does not seem to work. It doesn't cause an error, it just doesn't add anything to the DateTime object.

Also we still don't know what the format strings are for DateTime.toString().

Just to add to your list of ToDo's.

Regards,

Share on Facebook Share on Twitter


mc-Abe

  • Full Member
  • ***
  • Posts: 167
    • View Profile
    • mc-Things
Re: DateTime issues
« Reply #1 on: November 14, 2016, 10:57:34 am »
I believe the description of this function is incorrect. The current DateTime instance will not be modified. The function returns a new DateTime instance that must be assigned to a new DateTime object.

Nick_W

  • Full Member
  • ***
  • Posts: 215
    • View Profile
Re: DateTime issues
« Reply #2 on: January 08, 2017, 10:18:19 pm »
Any info on the DateTime.toSting() formatters?

I'd like to construct an ISO format DateTime string, or possibly a unix type timestamp (seconds since epoch).

Anyone have any suggestions?

Thanks.

mc-T2

  • Administrator
  • Sr. Member
  • *****
  • Posts: 252
  • mc-Things! The opportunities are endless!
  • Location: Canada
    • View Profile
Re: DateTime issues
« Reply #3 on: January 10, 2017, 10:18:48 am »
Hey Nick_W,
can you check out this post and see if this helps

http://mcthings.createaforum.com/mc-studio/current-time-in-hhmmss/msg719/#msg719

Thanks
Need more mc-Modules, mc-Gateways or other mc-Things? Check out our product page: www.mcthings.com/products. mc-Development kits are available too!
Check out a live Dashboard using mcThings and Losant! Click here: https://goo.gl/ST43hB

Nick_W

  • Full Member
  • ***
  • Posts: 215
    • View Profile
Re: DateTime issues
« Reply #4 on: January 10, 2017, 05:58:47 pm »
Thanks, that helped, don't know how I missed that.

Having a couple of other problems you might be able to help with:

Can't figure out the string format modifiers. Trying to create a string with leading 0's, ie 024, 005, 543 etc. I tried .toString("D3") and .toString("000") but these didn't work. Not a big deal, I'm working round it, but I'm sure if I knew what the formatters were it would be easy to do.

I'm also trying to get a timestamp (for including in json strings) with millisecond accuracy, but the RTC only has 1 second resolution. Any progress on the long data type or millis functions?

The reason I'm doing this is that I'm publishing some real time data, via both MQTT and via beacon. Right now I'm getting a conflict on rapid (sub 1second) events where the beacon data arrives at the same time as the mqtt data, and I can't tell which is more recent. I can tell when I get the data, but not when it was sent.

Any suggestions?

Nick_W

  • Full Member
  • ***
  • Posts: 215
    • View Profile
Re: DateTime issues
« Reply #5 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 »