Working with Unix epoch in .NET (changing between DateTime and time since epoch)


/ Published in: C#
Save to your folder(s)

Tentative source shows conversion to/from microseconds.


Copy this code and paste it in your HTML
  1. private static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1);
  2. const double MicrosecondsInAMillisecond = 1e3;
  3.  
  4. public static ulong ToHawkTime(this DateTime value)
  5. {
  6. double d = (value - UnixEpoch).TotalMilliseconds * MicrosecondsInAMillisecond;
  7. return (ulong) d;
  8. }
  9.  
  10. public static DateTime ConvertFromHawkTime(this ulong value)
  11. {
  12. DateTime d = UnixEpoch + TimeSpan.FromMilliseconds(value/MicrosecondsInAMillisecond);
  13. return d;
  14. }

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.