Click here to Skip to main content
15,888,461 members
Home / Discussions / C#
   

C#

 
GeneralRe: ProgressBar Pin
Mycroft Holmes9-Jul-09 19:52
professionalMycroft Holmes9-Jul-09 19:52 
Questionurgntz codez wanted. WinForms - show a usercontrol, docked, as Modal Pin
_Maxxx_9-Jul-09 13:56
professional_Maxxx_9-Jul-09 13:56 
AnswerRe: urgntz codez wanted. WinForms - show a usercontrol, docked, as Modal Pin
Mycroft Holmes9-Jul-09 14:06
professionalMycroft Holmes9-Jul-09 14:06 
GeneralRe: urgntz codez wanted. WinForms - show a usercontrol, docked, as Modal Pin
_Maxxx_9-Jul-09 15:51
professional_Maxxx_9-Jul-09 15:51 
GeneralRe: urgntz codez wanted. WinForms - show a usercontrol, docked, as Modal Pin
Mycroft Holmes9-Jul-09 19:50
professionalMycroft Holmes9-Jul-09 19:50 
QuestionIE automation Pin
yogesh_softworld1239-Jul-09 13:02
yogesh_softworld1239-Jul-09 13:02 
AnswerRe: IE automation Pin
schiebel-t9-Jul-09 21:14
schiebel-t9-Jul-09 21:14 
Question64bit fixed point timestamp to and from DateTime Pin
DaveyM699-Jul-09 11:32
professionalDaveyM699-Jul-09 11:32 
I've been battling this for a few hours so now I need help!

I'm converting a 64bit fixed point timestamp (32bit integer part, 32bit fractional part) to a DateTime by converting it to a TimeSpan adding that to an epoch, as the timestamp represents the number of seconds since the epoch.

The problem is I'm losing precision (I assume because of the cast to/from double and UInt64 - see code), so converting back from DateTime to timestamp gets a different value.

Any ideas?
using System;

class Program
{
    static void Main(string[] args)
    {
        UInt64 timestampA = 14844064336383267476;
        Console.WriteLine("TimestampA: {0}", timestampA);

        DateTime dateTime = NTPConversion.TimestampToDateTime(timestampA);
        Console.WriteLine("DateTime: {0}", dateTime);

        UInt64 timestampB = NTPConversion.DateTimeToTimestamp(dateTime);
        Console.WriteLine("TimestampB: {0}", timestampB);

        Console.WriteLine("Discrepancy: {0}", timestampB - timestampA);

        Console.ReadKey();
    }
}

/// <summary>
/// NTP Timestamp is 64 bit fixed point.
/// The upper 32 bits are the integer part and the lower 32 bits are the fractional part.
/// A timestamp is the time in seconds since 1st Jan 1900 (Epoch).
/// </summary>
public static class NTPConversion
{
    public static readonly DateTime Epoch = new DateTime(1900, 1, 1);

    public static DateTime TimestampToDateTime(UInt64 timestamp)
    {
        // effectively shift right 32 bits
        TimeSpan timeSpan = TimeSpan.FromSeconds((double)timestamp / 4294967296);
        DateTime dateTime = Epoch + timeSpan;
        return dateTime;
    }

    public static UInt64 DateTimeToTimestamp(DateTime dateTime)
    {
        TimeSpan timeSpan = dateTime - Epoch;
        // effectively shift left 32 bits
        UInt64 timestamp = (UInt64)((timeSpan.TotalSeconds * 4294967296d));
        return timestamp;
    }
}


Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
Why are you using VB6? Do you hate yourself? (Christian Graus)

AnswerRe: 64bit fixed point timestamp to and from DateTime Pin
Luc Pattyn9-Jul-09 12:24
sitebuilderLuc Pattyn9-Jul-09 12:24 
GeneralRe: 64bit fixed point timestamp to and from DateTime Pin
DaveyM699-Jul-09 22:28
professionalDaveyM699-Jul-09 22:28 
AnswerRe: 64bit fixed point timestamp to and from DateTime Pin
Henry Minute9-Jul-09 12:29
Henry Minute9-Jul-09 12:29 
GeneralRe: 64bit fixed point timestamp to and from DateTime Pin
DaveyM699-Jul-09 22:20
professionalDaveyM699-Jul-09 22:20 
AnswerRe: 64bit fixed point timestamp to and from DateTime [modified] Pin
Luc Pattyn9-Jul-09 13:41
sitebuilderLuc Pattyn9-Jul-09 13:41 
GeneralRe: 64bit fixed point timestamp to and from DateTime Pin
DaveyM699-Jul-09 22:16
professionalDaveyM699-Jul-09 22:16 
GeneralRe: 64bit fixed point timestamp to and from DateTime Pin
DaveyM699-Jul-09 23:05
professionalDaveyM699-Jul-09 23:05 
GeneralRe: 64bit fixed point timestamp to and from DateTime Pin
Luc Pattyn9-Jul-09 23:22
sitebuilderLuc Pattyn9-Jul-09 23:22 
GeneralRe: 64bit fixed point timestamp to and from DateTime [modified] Pin
DaveyM6910-Jul-09 4:00
professionalDaveyM6910-Jul-09 4:00 
GeneralRe: 64bit fixed point timestamp to and from DateTime Pin
Luc Pattyn10-Jul-09 7:28
sitebuilderLuc Pattyn10-Jul-09 7:28 
GeneralRe: 64bit fixed point timestamp to and from DateTime [modified] Pin
DaveyM6910-Jul-09 8:48
professionalDaveyM6910-Jul-09 8:48 
QuestionIsolatedStorageFileStream Pin
AAKAra9-Jul-09 10:32
AAKAra9-Jul-09 10:32 
AnswerRe: IsolatedStorageFileStream Pin
Henry Minute9-Jul-09 11:54
Henry Minute9-Jul-09 11:54 
QuestionMail Merged Word document in IE , Need Control and want to update it ..Please help Pin
Sandumone9-Jul-09 10:03
Sandumone9-Jul-09 10:03 
AnswerRe: Mail Merged Word document in IE , Need Control and want to update it ..Please help Pin
Mycroft Holmes9-Jul-09 11:45
professionalMycroft Holmes9-Jul-09 11:45 
QuestionLDAP Question Pin
mypicturefaded9-Jul-09 8:23
mypicturefaded9-Jul-09 8:23 
AnswerRe: LDAP Question Pin
Jeremy Likness9-Jul-09 8:27
professionalJeremy Likness9-Jul-09 8:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.