question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ZonedDateTime to ToUnixTimeSeconds

See original GitHub issue

Hi @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:closed
  • Created 3 years ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
jskeetcommented, May 17, 2020

No. Given that it would make relatively little difference compared with the application-side changes, I’d rather not do those.

0reactions
goforgoldcommented, May 17, 2020

Thanks @jskeet once again for helping me out.

You are really a wonderful person.

I think we can close this issue now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to convert ZonedDateTime to milliSecond in Java?
I'm trying to convert ZonedDateTime to milliseconds using below code. LocalDateTime ldt = LocalDateTime.now(); ZonedDateTime zonedDateTime =ldt.
Read more >
Struct Instant | Noda Time
Returns a new instant corresponding to the given UTC date and time in the ISO calendar. In most cases applications should use ZonedDateTime...
Read more >
Java 8 - Convert ZonedDateTime to Timestamp
Basically: Timestamp timestampGermany = Timestamp.valueOf(LocalDateTime.parse(“20200206-000001”, DateTimeFormatter.ofPattern(“yyyyMMdd-HHmmss”)) ...
Read more >
Code Editor
NodaTime. Noda Time is a date and time API acting as an alternative to the built-in DateTime/DateTimeOffset etc types built into the .NET...
Read more >
InstantTest.cs
A better date and time API for .NET. Contribute to nodatime/nodatime development by creating an account on GitHub.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found