ZonedDateTime to ToUnixTimeSeconds
See original GitHub issueHi @jskeet and team!
Thanks for doing this wonderful library for .NET developers. I recently moved my trading system from .NET DateTime APIs to NodaTime because of lack of features and consistency in .NET DateTime APIs.
I am mostly storing times in Unix Epoch Seconds inside different structures and I frequently convert UTC to Local and vice-versa at different places in my project.
This document describes (quoting):
The Instant and ZonedDateTime types both unambiguously refer to a point in time (with the latter additionally holding time zone and calendar information).
I can instantly convert from an object of Instant
to Unix Epoch Seconds using ToUnixTimeSeconds()
method. Really that is very fast (below are some very basic test results).
Since there is no direct way to convert ZonedDateTime
object to Unix Epoch Seconds. I am doing zoned.ToInstant().ToUnixTimeSeconds()
. That is a bit slow.
I am required to it on average 100 million times for running simulations. I was hoping if there is any faster way to get Unix Epoch Seconds from ZonedDateTime
object to Instant.
Below is how I am testing it. I am not sure this would qualify as the standard test, I just did this for my own testing.
LocalDateTime local = new LocalDateTime().PlusSeconds(86400);
ZonedDateTime zoned = local.InZoneStrictly(DateTimeZoneProviders.Tzdb["America/New_York"]);
Instant instant = zoned.ToInstant();
DateTime now = DateTime.Now;
for (int i = 0; i < 100000000; i++)
{
instant.ToUnixTimeSeconds();
}
Console.WriteLine("Instant: " + (DateTime.Now - now).TotalSeconds);
now = DateTime.Now;
for (int i = 0; i < 100000000; i++)
{
zoned.ToInstant().ToUnixTimeSeconds();
}
Console.WriteLine("Zoned: " + (DateTime.Now - now).TotalSeconds);
Output:
Instant: 0.2982887
Zoned: 2.9066556
Issue Analytics
- State:
- Created 3 years ago
- Comments:12 (7 by maintainers)
No. Given that it would make relatively little difference compared with the application-side changes, I’d rather not do those.
Thanks @jskeet once again for helping me out.
You are really a wonderful person.
I think we can close this issue now.