Click here to Skip to main content
15,890,557 members
Home / Discussions / C#
   

C#

 
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 
I think I've sorted it Luc. Part of the problem was TimeSpan.FromSeconds and FromMilliseconds are only accurate to 1 millisecond so it was bound to do wierd stuff!

The answer to that was to use TimeSpan.FromTicks, in combination with Math.Round(), and now I appear get 1 picosecond 10 nanosecond precision. [Edit] The resolution of Ticks per second [/Edit]

Obviously the first pass results in a different timestamp still - but it's acceptably close. I just run it through a TimestampRound method first so future comparisons are made against that.
public static class NTPConversion
{
    public static readonly DateTime Epoch = new DateTime(1900, 1, 1);

    public static UInt64 TimestampRound(UInt64 timestamp)
    {
        long ticks = (long)(Math.Round(
            (double)timestamp / 0x100000000, 7, MidpointRounding.AwayFromZero)
            * TimeSpan.TicksPerSecond);
        UInt64 newTimestamp = (UInt64)(((double)ticks / TimeSpan.TicksPerSecond) * 0x100000000);
        return newTimestamp;
    }

    public static DateTime TimestampToDateTime(UInt64 timestamp)
    {
        long ticks = (long)(Math.Round(
            (double)timestamp / 0x100000000, 7, MidpointRounding.AwayFromZero)
            * TimeSpan.TicksPerSecond);
        TimeSpan timeSpan = TimeSpan.FromTicks(ticks);
        DateTime dateTime = Epoch + timeSpan;
        return dateTime;
    }

    public static UInt64 DateTimeToTimestamp(DateTime dateTime)
    {
        TimeSpan timeSpan = dateTime - Epoch;
        long ticks = timeSpan.Ticks;
        UInt64 timestamp = (UInt64)(((double)ticks / TimeSpan.TicksPerSecond) * 0x100000000);
        return timestamp;
    }
}
Thanks for your help! If you see any obvious flaws in this - let me know!

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)

modified on Friday, July 10, 2009 2:48 PM

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 
AnswerRe: LDAP Question Pin
Manas Bhardwaj9-Jul-09 8:29
professionalManas Bhardwaj9-Jul-09 8:29 
GeneralRe: LDAP Question Pin
mypicturefaded9-Jul-09 8:37
mypicturefaded9-Jul-09 8:37 
GeneralRe: LDAP Question Pin
Manas Bhardwaj9-Jul-09 8:41
professionalManas Bhardwaj9-Jul-09 8:41 
GeneralRe: LDAP Question Pin
mypicturefaded9-Jul-09 8:46
mypicturefaded9-Jul-09 8:46 
GeneralRe: LDAP Question Pin
Manas Bhardwaj9-Jul-09 8:49
professionalManas Bhardwaj9-Jul-09 8:49 
GeneralRe: LDAP Question Pin
mypicturefaded9-Jul-09 8:54
mypicturefaded9-Jul-09 8:54 
GeneralRe: LDAP Question Pin
Manas Bhardwaj9-Jul-09 8:57
professionalManas Bhardwaj9-Jul-09 8:57 
QuestionPassing a Method to a second form Pin
bwood20209-Jul-09 7:13
bwood20209-Jul-09 7:13 
AnswerRe: Passing a Method to a second form Pin
harold aptroot9-Jul-09 7:25
harold aptroot9-Jul-09 7:25 

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.