Skip to content

Time.UnixTime

NotCoffee418 edited this page Oct 22, 2022 · 2 revisions

Shortcut functions to .NET's implementation for converting DateTime to UnixTime and back. The following methods are made accessible through CoffeeToolkit.Time.

// UnixTime class
long UnixTime.FromDateTimeToSeconds(DateTime dt, bool castToUtc = false);
long UnixTime.FromDateTimeToMilliseconds(DateTime dt, bool castToUtc = false);
DateTime SecondsToDateTimeUtc(long unixTimeSeconds);
DateTime MillisecondsToDateTimeUtc(long unixTimeMilliseconds);

// Extension methods
long DateTime.ToUnixTimeSeconds(castToUtc = false);
long DateTime.ToUnixTimeMilliseconds(castToUtc = false);
DateTimeOffset DateTime.ToDateTimeOffsetUtc(DateTimeKind? overrideKind)

By default, all DateTime instances will interpret the UTC time and generate a UTC timestamp, even if the DateTime's kind is not UTC.
Setting castToUtc to true will cause the DateTime's literal time to be interpreted as UTC and generate a timestamp based on that, rather than the actual equivalent UTC time. This can be useful with non-UTC parsed timestamps.

Clone this wiki locally