views:

290

answers:

4

We have a binary file from which we have identified the following dates (as Int64). We now the following facts about the Date/Time format;

  • The 64 bit Date has a resolution to the microsecond
  • The 64 bit Date has a range of 4095 years
  • The Int64 9053167636875050944 (0x7DA34FFFFFFFFFC0) = 9th March 2010
  • The Int64 9053176432968073152 (0x7DA357FFFFFFFFC0) = 10th March 2010
  • The Int64 9053185229061095360 (0x7DA35FFFFFFFFFC0) = 11th March 2010
  • The Int64 9053194025154117568 (0x7DA367FFFFFFFFC0) = 12th March 2010

Any help on figuring out a way to convert this to a valid C# Date/Time is appreciated.

+2  A: 

From the hex data, at least this much is clear:

0x7da = 2010

The 3 next is very likely March (month 3).

unwind
+2  A: 

0x7da = 2010
0x3 = 3 (March)
01001111 = the first 5 bits seem to be the day (9), the last three are always 1 from what you've shown us
The rest of the bits probably represent further resolution (hours, minutes etc.)

BlueRaja - Danny Pflughoeft
+2  A: 

Going off of what @BlueRaja The Green Unic said, here's how you'd parse it:

        var X = 0x7da34fffffffffc0L;
        var Year = X >> 52;
        var Month = (X >> 48) & 0xf;
        var Day = (X >> 43) & 0x1f;
Chris Haas
You'll need to cast to int to use the DateTime constructor.
Matthew Flaschen
A: 

Here is a solution:

string hex_data = "7DA34FFFFFFFFFC0";
        Int64 int_datetime = Convert.ToInt64(hex_data, 16);

        int year = (int)(int_datetime >> 52);
        int month = (int)((int_datetime >> 48) & 0x0F);
        int day = (int)(((int_datetime >> 43) & 0x1F));

        int hour = (int)(((int_datetime >> 38) & 0x1F)) > 24 ? 0 : (int)(((int_datetime >> 38) & 0x1F));
        int min = (int)(((int_datetime >> 32) & 0x3F)) > 60 ? 0 : (int)(((int_datetime >> 32) & 0x3F));
        int sec = (int)(((int_datetime >> 26) & 0x3F)) > 60 ? 0 : (int)(((int_datetime >> 26) & 0x3F));
        int mili_sec = (int)(((int_datetime >> 16) & 0x3FF)) > 100 ? 0 : (int)(((int_datetime >> 16) & 0x3FF));
        int micro_sec = (int)(((int_datetime >> 6) & 0x3FF)) > 100 ? 0 : (int)(((int_datetime >> 6) & 0x3FF));

        string str_date_time = year.ToString("D4") + "/" + month.ToString("D2") + "/" + day.ToString("D2") + " " + hour.ToString("D2") + ":" + min.ToString("D2") + ":" 
            + sec.ToString("D2") + "." + mili_sec.ToString("D3");

        DateTime date_time_dt=DateTime.Parse(str_date_time);

Returns:

09/03/2010 12:00:00 AM

in the date_time_dt object. I don't think the DateTime object supports microseconds in C#, but I may be wrong.

lithium81
"Time values are measured in 100-nanosecond units called ticks, and a particular date is the number of ticks since 12:00 midnight, January 1, 1 A.D. (C.E.) in the GregorianCalendar calendar." -- .NET's DateTime documentation
R. Bemrose
ok, so you could add the microseconds to the Tick property in the DateTime object to get a more accurate time?
lithium81