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

  • SEARCH
  • Home
  • 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 9185993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:24:51+00:00 2026-06-17T19:24:51+00:00

How can I get an std::chrono::duration since a fixed date? I need this to

  • 0

How can I get an std::chrono::duration since a fixed date? I need this to convert a std::chrono::time_point to an unix timestamp.

Insert code into XXX

auto unix_epoch_start = XXX;
auto time = std::chrono::system_clock::now();
auto delta = time - unix_epoc_start;
auto timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(delta).count();

I know time_point has a method time_since_epoch() but it’s not guaranteed that this is the same as the unix epoch (00:00:00 UTC on 1 January 1970).

  • 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-06-17T19:24:52+00:00Added an answer on June 17, 2026 at 7:24 pm

    A unix time stamp is defined as the number of seconds since January 1, 1970 UTC, except not counting all the seconds. This is somewhat ridiculous and one has to wonder what the point of it is, so I agree that this is a silly question.

    Anyway, lets look at some platform documentation for time_t and time().

    Linux:

    time() returns the time as the number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC).

    POSIX.1 defines seconds since the Epoch using a formula that approximates the number of seconds between a specified time and the Epoch. This formula takes account of the facts that all years that are evenly divisible by 4 are leap years, but years that are evenly divisible by 100 are not leap years unless they are also evenly divisible by 400, in which case they are leap years. This value is not the same as the actual number of seconds between the time and the Epoch, because of leap seconds and because system clocks are not required to be synchronized to a standard reference. The intention is that the interpretation of seconds since the Epoch values be consistent; see POSIX.1-2008 Rationale A.4.15 for further rationale.

    Windows:

    The time function returns the number of seconds elapsed since midnight (00:00:00), January 1, 1970, Coordinated Universal Time (UTC), according to the system clock.

    Mac OS X:

    The functions ctime(), gmtime(), and localtime() all take as an argument
    a time value representing the time in seconds since the Epoch (00:00:00
    UTC, January 1, 1970;

    The asctime(), ctime(), difftime(), gmtime(), localtime(), and mktime()
    functions conform to ISO/IEC 9899:1990 (ISO C90''), and conform to
    ISO/IEC 9945-1:1996 (
    POSIX.1”) provided the selected local timezone
    does not contain a leap-second table (see zic(8)).

    Similar documentation can be found for other systems, such as AIX, HP-UX, Solaris, etc.

    So although not specified in C++ there is an easy and widely portable way to get a Unix timestamp:

    auto unix_timestamp = std::chrono::seconds(std::time(NULL));
    

    And if you want a number of milliseconds since 1 Jan 1970 UTC (similarly not counting all of them) then you can do this:

    int unix_timestamp_x_1000 = std::chrono::milliseconds(unix_timestamp).count();
    

    Just remember that these values aren’t real times, so you can’t in general use unix timestamps in arithmetic. For example subtracting unix timestamps does not give you an accurate count of seconds between the times. Or if you did something like:

    std::chrono::steady_clock::now() - unix_timestamp;
    

    you would not get a time point actually corresponding to 1970-01-01 00:00:00+0000.


    As Andy Prowl suggests you could do something silly like:

    // 1 Jan 1970 (no time zone)
    std::tm c = { 0, 0, 0, 1, 0, 70, 0, 0, -1};
    
    // treat it as 1 Jan 1970 (your system's time zone) and get the
    // number of seconds since your system's epoch (leap seconds may
    // or may not be included)
    std::time_t l = std::mktime(&c);
    
    // get a calender time for that time_point in UTC. When interpreted
    // as UTC this represents the same calendar date and time as the
    // original, but if we change the timezone to the system TZ then it
    // represents a time offset from the original calendar time by as
    // much as UTC differs from the local timezone.
    std::tm m = *std::gmtime(&l);
    
    // Treat the new calendar time as offset time in the local TZ. Get
    // the number of seconds since the system epoch (again, leap seconds
    // may or may not be counted).
    std::time_t n = std::mktime(&m);
    
    l -= (n-l); // subtract the difference
    

    l should now represent the (wrong) number of seconds since 1 Jan 1970 UTC. As long as there are no leap seconds between the system epoch and 1 Jan 1970 (system time zone), or within an equal amount of time in the other direction from the system epoch, then any counted leap seconds should cancel out and l will be wrong in just the way that unix timestamps are wrong.


    Another option is to use a decent date library such as Howard Hinnant’s chrono::date. (Howard Hinnant was one of the guys that worked on the C++11 <chrono> library.)

    auto now = system_clock::now();
    sys_days today = time_point_cast<days>(now);
    system_clock::time_point this_morning = today;
    
    sys_days unix_epoch = day(1)/jan/1970;
    days days_since_epoch = today - unix_epoch;
    
    auto s = now - this_morning;
    
    auto tz_offset = hours(0);
    int unix_timestamp = (days_since_epoch + s + tz_offset) / seconds(1);
    

    If you want to handle leap seconds Howard Hinnant also provides a library that includes facilities for handling them as well as for parsing time zone databases as the source for leap second data.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want understand how to work std::put_time, and how can I get date stamp
can you get the last or newly added element in std::set? for example say
I can get this to work: [<DllImport(user32.dll)>] extern bool GetClientRect(nativeint, RECT*) let getClientRect hwnd
I can get the current selected row in this way: private void DataGridView1_CellContentClick(object sender,
I can get std::bind to work fine with an instance, but I can't get
Suppose I have a vector std::vector<a> A; I can get access to its member-functions
How I can get the elements in the vectors set? This is the code
I can't get std::bind to work the same way boost::bind works. Either I'm not
In C++11, we can get an efficiency boost by using std::move when we want
One can get an element from std::tuple by index using std::get . Analogically, how

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.