How do you convert epoch time in C# ?

How do you convert epoch time in C# ?

Asked on December 19, 2018 in C#.
Add Comment


  • 5 Answer(s)

    Here is a solution:

    public static DateTime FromUnixTime(long unixTime)
    {
        return epoch.AddSeconds(unixTime);
    }
    private static readonly DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
    
    Answered on December 19, 2018.
    Add Comment

    Try this extension methods:

    public static DateTime FromUnixTime(this long unixTime)
    {
        var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
        return epoch.AddSeconds(unixTime);
    }
     
    public static long ToUnixTime(this DateTime date)
    {
        var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
        return Convert.ToInt64((date - epoch).TotalSeconds);
    }
    

    The comment  FromUnixTime entry a DateTime along a Kind of Utc, still the above ToUnixTime is  more suspect . To grant for date‘s Kind neither Utc or Local,then  use ToUniversalTime:

    public static long ToUnixTime(this DateTime date)
    {
        var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
        return Convert.ToInt64((date.ToUniversalTime() - epoch).TotalSeconds);
    }
    

    ToUniversalTime desire  convert a Local DateTime into Utc.

    Here can also do:

    public static long ToUnixTime(this DateTime date)
    {
        return (date.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
    }
    
    Answered on December 19, 2018.
    Add Comment

    The current version of .Net (v4.6)  combined  Unix time conversions, which includes  to and from  represented by each of two seconds or milliseconds.

    • Unix time in seconds to DateTimeOffset:
      DateTimeOffset dateTimeOffset = DateTimeOffset.FromUnixTimeSeconds(1000);
      
    • DateTimeOffset to Unix time in seconds:
      long unixTimeStampInSeconds = dateTimeOffset.ToUnixTimeSeconds();
      
    • Unix time in milliseconds to DateTimeOffset:
      DateTimeOffset dateTimeOffset = DateTimeOffset.FromUnixTimeMilliseconds(1000000);
      
    • DateTimeOffset to Unix time in milliseconds:
      long unixTimeStampInMilliseconds= dateTimeOffset.ToUnixTimeMilliseconds();
      

    In this  methods convert to and from DateTimeOffset to  get a DateTime representation commonly  use  DateTimeOffset.DateTime property:

    DateTime dateTime = dateTimeOffset.UtcDateTime;
    
    Answered on December 19, 2018.
    Add Comment
    public const long UnixEpochTicks = 621355968000000000;
    public const long TicksPerMillisecond = 10000;
    public const long TicksPerSecond = TicksPerMillisecond * 1000;
    
    //[MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static DateTime FromUnixTimestamp(this long unixTime)
    {
        return new DateTime(UnixEpochTicks + unixTime * TicksPerSecond);
    }

    From a quick benchmark (BenchmarkDotNet) under net471 I get this number:

            Method |     Mean |     Error |    StdDev | Scaled |
    -------------- |---------:|----------:|----------:|-------:|
             LukeH | 5.897 ns | 0.0897 ns | 0.0795 ns |   1.00 |
          MyCustom | 3.176 ns | 0.0573 ns | 0.0536 ns |   0.54 |
    Answered on January 15, 2019.
    Add Comment

    The Unix time is given as seconds since the epoch: the number of seconds (not counting leap seconds) that have passed since 00:00:00 Coordinated Universal Time (UTC), or Thursday, January 1st, 1970.
    Following are the methods to convert the Unix time to/from Local time

    // Local Time --> Unix Time
    private long ToUnixTime(DateTime dateTime)
    {
     var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
     return (long)(dateTime.ToUniversalTime() - epoch).TotalSeconds;
    }
    // Unix Time --> Local Time
    private DateTime FromUnixTime(long unixDateTime)
    {
     var timeSpan = TimeSpan.FromSeconds(unixDateTime);
     return new DateTime(timeSpan.Ticks).ToLocalTime();
    }
    Answered on January 15, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.