1

Closed

computing ts should consider timezone

description

    protected int GetTs(DateTime targetDate)
    {
        return (int)(targetDate.ToUnixTicks() / 1000);
    }


public static class DateTimeExtensions
{
    /// <summary>
    /// returns the number of milliseconds since Jan 1, 1970 (useful for converting C# dates to JS dates)
    /// </summary>
    public static double ToUnixTicks(this DateTime time)
    {
        var origin = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
        time = time.ToUniversalTime();
        var diff = new TimeSpan(time.Ticks - origin.Ticks);
        return Math.Round(diff.TotalMilliseconds, 0);
    }

    public static DateTime FromUnixTicks(double ticks, DateTimeKind kind)
    {
        var origin = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
        var result = origin.AddMilliseconds(ticks);

        return kind == DateTimeKind.Utc
                   ? result
                   : result.ToLocalTime();
    }
}
Closed Mar 14, 2013 at 5:19 PM by KZmorzynski
Fixed in commit bc84f6da53d0

comments

KZmorzynski wrote Jan 30, 2013 at 8:09 PM

Nice one, this is definitely a bug I wasn't aware of, thank you, I'll handle it.