Able
Askowl Base Library Enabler
Clock.cs
1 // Copyright 2018 (C) paul@marrington.net http://www.askowl.net/unity-packages
2 
3 namespace Askowl {
4  using System;
5 
6  /// <a href="http://bit.ly/2Rj0RMi">Static helper class to move between time systems.</a>
7  public static class Clock {
8  private static DateTime EpochStart =
9  new DateTime(year: 1970, month: 1, day: 1, hour: 0, minute: 0, second: 0, kind: DateTimeKind.Utc);
10 
11  /// <a href="http://bit.ly/2NWvKYI">Retrieve epoch time at the time of this call.</a>
12  public static double EpochTimeNow => EpochTimeAt(DateTime.UtcNow);
13 
14  /// <a href="http://bit.ly/2NYOFSy">Convert from C# native time representation to epoch time. Epoch time is the number of seconds since the start of 1970 in Greenwich.</a>
15  public static double EpochTimeAt(DateTime when) => (when.ToUniversalTime() - EpochStart).TotalSeconds;
16 
17  /// <a href="http://bit.ly/2OpmeNj">Given a value in epoch time UTC, retrieve the local time</a>
18  public static DateTime FromEpochTime(double epochTime) => EpochStart.AddSeconds(epochTime).ToLocalTime();
19  }
20 }
Definition: Clock.cs:3