Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7433335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:39:12+00:00 2026-05-29T09:39:12+00:00

I have a fractional epoch timestamp, represented as double , that I would like

  • 0

I have a fractional epoch timestamp, represented as double, that I would like to convert to an appropriate std::chrono::time_point. The epoch is the usual UNIX epoch since 1/1/1970. I know that there exists std::chrono::system_clock::from_time_t, but a time_t does not have a fractional part. What would be the best way to do this with C++11 means?

This question is related to unix timestamp to boost::posix_time::ptime, except that it’s asking for the C++11 rather than Boost version of it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:39:13+00:00Added an answer on May 29, 2026 at 9:39 am

    Assuming the epoch is the same as a known clock type you can use a duration with a double representation and convert to the duration used by that clock.

    // change period to appropriate units - I'm assuming seconds
    typedef std::chrono::duration<double, std::ratio<1>> d_seconds;
    
    d_seconds since_epoch_full(324324.342);
    auto since_epoch = std::chrono::duration_cast<clock::duration>(since_epoch_full);
    clock::time_point point(since_epoch);
    

    This should be ok for any calculation involving that clock, since you’re using the same precision as the clock, but it may lose some of the precision in the conversion. If you don’t want to lose that you’ll have to use a time_point specialization that uses that double-based duration type. And then use that in your calculations (of course, with all the caveats of floating-point math).

    typedef std::chrono::time_point<clock, d_seconds> d_time_point;
    

    However, this will complicate any calculations involving that same clock, as it will require conversions. To make this easier, you can build your own clock wrapper that does the conversions and use that:

    template <typename Clock>
    struct my_clock_with_doubles {
        typedef double rep;
        typedef std::ratio<1> period;
        typedef std::chrono::duration<rep, period> duration;
        typedef std::chrono::time_point<my_clock_with_doubles<Clock>> time_point;
        static const bool is_steady = Clock::is_steady;
    
        static time_point now() noexcept {
            return time_point(std::chrono::duration_cast<duration>(
                       Clock::now().time_since_epoch()
                   ));
        }
    
        static time_t to_time_t(const time_point& t) noexcept {
            return Clock::to_time_t(typename Clock::time_point(
                                 std::chrono::duration_cast<typename Clock::duration>(
                                     t.time_since_epoch()
                                 )
                            ));
        }
        static time_point from_time_t(time_t t) noexcept {
            return time_point(std::chrono::duration_cast<duration>(
                       Clock::from_time_t(t).time_since_epoch()
                   ));
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to get fractional timer resolution in an Excel VBA project that
I have already searched Google and I am finding no tool to convert fractional
I have to convert a double value x into two integers as specified by
I would like to define a generic function that gets the next value in
I have a section of code that reads: std::cerr << val1 << << val2
I have a functional datagrid that responds to itemClick events. Everything works, except that
I have a functional JSP page that accepts URL parameters and updates a table
I have a functional AdjacencyListGraph class that adheres to a defined interface GraphStructure. In
In Vaadin's website it says that only Eclipse and NetBeans have fully functional (which
I have created some functional categories that I use to show/hide markup elements. However,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.