Git Product home page Git Product logo

Comments (1)

miladHakimi avatar miladHakimi commented on May 2, 2024

Knowing more about time_point, duration, and clock helps solve this problem.
chrono::time_point: Is a point in time. It stores a duration type value indicating the time since the clock's epoch.
chrono::duration: Holds the number of ticks, where the tick period is a compile-time rational fraction representing the time in seconds from one tick to the next.
clock: Is a bundle containing a time_point and a duration. The origin of the clock's time_point is referred to as the clock's epoch.

To create a time_point from a number, we should create a TimestampClock::duration. The given number is used to set the ticks of the duration variable of the time_point. Since we want this number to represent a duration in nanoseconds, the period of the duration must be in nanoseconds. The problem with the current FromNanoseconds is that the TimestampClock can vary from one system to another. Hence, the result will not always be a duration in nanoseconds precision.
To fix this issue, we could write something like this:

std::chrono::nanoseconds duration_ns{nanos};

To create a time_point from the duration_ns, we must convert the ticks in duration_ns into the ticks in a TimestampClock::duration. To do that, we have to explicitly cast the duration; otherwise, the implicit cast leads to a compile time error when the precision of the TimestampClock::duration is less than nanoseconds. The explicit cast tells the compiler we are aware of the possible data loss.

TimestampClock::duration casted_dur = std::chrono::duration_cast<TimestampClock::duration>(duration_ns);

Now that we have a duration in TimestampClock::duration, we can create a TimestampClock::time_point from it.

 TimestampClock::time_point timestamp(casted_dur);

A sample code for different casting scenarios: https://godbolt.org/z/5cEEoGdon

references:

from vulkan-performance-layers.

Related Issues (19)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.