views:

10529

answers:

2

As the title says really.

There is this example code, but then it starts talking about millisecond / nanosecond problems.

http://blogs.msdn.com/brada/archive/2004/03/20/93332.aspx

Edit: This is what I've got so far:

public Double CreatedEpoch
     {
      get
      {
       DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime();
       TimeSpan span = (this.Created.ToLocalTime() - epoch);
       return span.TotalSeconds;
      }
      set
      {
       DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime();
       this.Created = epoch.AddSeconds(value);
      }
     }
A: 

A Unix tick is 1 second (if I remember well), a .Net tick is 100 nanoseconds.

If you've been encountering problems with nanoseconds, you might want to try using AddTick(10000000 * value).

Luk
Unix is seconds past epoch - which is 1/1/70.
ScottCher
+8  A: 

Here's what you need:

public static DateTime UnixTimeStampToDateTime( double unixTimeStamp )
{
    // Unix timestamp is seconds past epoch
    System.DateTime dtDateTime = new DateTime(1970,1,1,0,0,0,0);
    dtDateTime = dtDateTime.AddSeconds( unixTimeStamp ).ToLocalTime();
    return dtDateTime;
}

Or, for Java (which is different):

public static DateTime JavaTimeStampToDateTime(double javaTimeStamp)
{
    // Java timestamp is millisecods past epoch
    System.DateTime dtDateTime = new DateTime(1970,1,1,0,0,0,0);
    dtDateTime = dtDateTime.AddSeconds(Math.Round(javaTimeStamp / 1000)).ToLocalTime();
    return dtDateTime;
}
ScottCher
AddSeconds doesn't seem to behave very well for values lesser than 0.1 milliseconds (iirc)
Luk