#include <iostream>
#include <chrono>
#include <cxxabi.h>

int main()
{
    auto now = std::chrono::system_clock::now();
    
    // What type is "now" since it was automatically determined from the rhs?
    // (rhs = right hand side.. of the assignment)
    
    // this prints a messy "mangled" type name... more on what that means later
    printf("%s\n",typeid(now).name());
    
    // this is the human-readable version:
    printf("%s\n",abi::__cxa_demangle(typeid(now).name(),nullptr,nullptr,nullptr));
    // std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long, std::ratio<1l, 1000000000l> > >
    
    // reading about "time_point" and "duration" via google searches, we learn
    // that the time_point will store a "long" number of ticks since the 1970 epoch...
    
    // So, what size is long?
    printf("%d\n",sizeof(long)); // -> 8, which is 8 bytes so 64 bits
    
    // the "duration" specifies that a tick is 1/1000000000 of a second, so this
    // time_point is measuring the number of nanoseconds since Jan 1, 1970.  As of
    // this writing, that is about 1,615,678,163,000,000,000 which is less than the
    // max 64 bit signed value of 9,223,372,036,854,775,807.  So this method will
    // stop working in a couple/few hundred years from now, at which point 128bit
    // storage will probably be more common.
    
    // So, what is time_since_epoch()'s type?
    auto since = now.time_since_epoch();
    printf("%s\n", abi::__cxa_demangle(typeid(since).name(),nullptr,nullptr,nullptr));
    // std::chrono::duration<long, std::ratio<1l, 1000000000l> >
    // ...looks like it just gets the number of ticks, let's see:
    
    printf("nanoseconds since 1970: %ld\n", since.count()); // count() gets the ticks from a duration
    // -> 1615678553559336279
    
    // so std::chrono::duration_cast is just going to change the resolution
    auto microseconds = std::chrono::duration_cast<std::chrono::microseconds>(since);
    printf("microseconds since 1970: %ld\n", microseconds.count());
    // -> 1615678716531564
    
    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: