none
64-bit Little/Big Endian Values into and outfrom System.DateTime RRS feed

  • Question

  • I'm working on a project that's going to be deployed via .NET on Windows and via Mono on Linux; the same data needs to be exchangeable between the platforms, including where platform A is Little-Endian and platform B is Big-Endian.

    Where text is concerned, I know ASCII/Unicode is natively Big-Endian, so there's no problem with that.

    Where numbers are concerned, I know that byte arrays have to be reversed on Big-Endian platforms to store the data in Little-Endian format.

    But what exactly is the 64 bit value output from System.DateTime.ToBinary?  Is this a number representing the number of seconds elapsed since Jan 1 1970, is this some kind of serialized attribute that outputs some DotNET-recognizable value, or what?  Does this byte array need to be handled differently in Big and Little Endian environments?


    It never hurts to try. In a worst case scenario, you'll learn from it.
    Tuesday, July 26, 2011 2:51 PM

Answers

All replies

  • Hi Andrew,

    I believe the 64 bit long value of a Date / DateTime represents the value of the Ticks property.

    >>

    "A single tick represents one hundred nanoseconds or one ten-millionth of a second. There are 10,000 ticks in a millisecond.

    The value of this property represents the number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight, January 1, 0001, which representsDateTime.MinValue. It does not include the number of ticks that are attributable to leap seconds."

     

    For more details see:>>

    http://msdn.microsoft.com/en-us/library/system.datetime.ticks.aspx

     



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    XNA is coming to VB.Net

    App Hub forums



    Tuesday, July 26, 2011 4:22 PM
  • Perfect, thanks JAO.

    I actually read the documentation on DateTime.ToBinary and was about ready to build my own representation method since that one looks hard to swallow as accurate enough to be useful - I absolutely prefer the .Ticks method you point out.


    It never hurts to try. In a worst case scenario, you'll learn from it.
    Tuesday, July 26, 2011 4:29 PM
  • Hi again Andrew,

    I never even knew that a DateTime had a .ToBinary method so we both now know something extra.    :-D

    I've marked your above post as helpful as it has now made me aware of another method.    :-)



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    XNA is coming to VB.Net

    App Hub forums



    Tuesday, July 26, 2011 4:41 PM
  • Hi again Andrew,

    I had to do some checking seeing as you mentioned .ToBinary.  :-)

    .ToBinary serializes a DateTime object so that it can be reproduced via deserialization.

     

    Here is the documentation, which you say you have read.>>

    http://msdn.microsoft.com/en-us/library/system.datetime.tobinary.aspx

     

    Hover your mouse over .ToBinary in the following code to see what Intellisense mentions in your I.D.E. if you wish.>>

     

     

    Imports System.Environment
    
    Public Class Form1
    
     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
      Dim rightNow As DateTime = Now
    
      Dim outputString1 As String = Convert.ToString(rightNow.ToBinary, 2)
      Dim outputString2 As String = Convert.ToString(rightNow.Ticks, 2)
    
      MessageBox.Show("ToBinary = " & NewLine & outputString1.PadLeft(64, "0"c) & NewLine & NewLine & NewLine & NewLine & "Ticks = " & NewLine & outputString2.PadLeft(64, "0"c))
    
     End Sub
    
    End Class
    

     

     

    I have added this post and the above code should anyone else also view this thread at a later date.

    The binary values are obviously different.



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    XNA is coming to VB.Net

    App Hub forums



    Tuesday, July 26, 2011 5:02 PM
  • The DateTime object contains more than just ticks: it also contains timezone information (and, I believe, daylight savings time state). Obviously, more than just 'ticks'. Presumably, the .ToBinary allows correct reconstitution including timezones and daylight savings time.

    So, depending on your application, reconstitution to the correct time(zone) may or may not be important. Regardless, a 64-bit numeric value would be treated exactly the same, re. big-endian/little-endian.


    Stephen J Whiteley
    Tuesday, July 26, 2011 7:16 PM
    Moderator
  • Right, which is why I prefer Ticks - I understand how to handle it.  I figure timezone considerations can be handled by additional code at runtime.

    The problem with the serialized version seems to be that there's a partial bitwise integer involved, and it's difficult to understand how or whether the .Kind portion is numeric/endian-dependent or not and then how or whether the .Time portion is the same or not.

     


    It never hurts to try. In a worst case scenario, you'll learn from it.
    Tuesday, July 26, 2011 7:55 PM
  • If you can handle the ticks, then I'm sure you'll be fine.

    But as far as encoding using .ToBinary, it shouldn't really matter how the 'kind' portion is arranged in the bits (bytes). You'd transmit it as a long, and reconstitute it as a long. As long (no pun intended) as the Mono project can perform the .FromBinary method, and the Long value is byte swapped as appropriate, it should work. I don't have a Mono system to test that, so can only suggest that theoretically it would not be a problem in using toBinary and FromBinary on different endian systems.

    Either way, I suspect you are satisfied with using ticks.


    Stephen J Whiteley
    Wednesday, July 27, 2011 11:28 AM
    Moderator