Time Point

Contents[Show]

The starting point (epoch) and the additional time duration define the time point. It consists of two components, clock and time duration.

 

Time Point

Time point is a class template. std::chrono::time_point requires a clock. By default, the time duration is derived from the clock. 

template<
  class Clock,
  class Duration= typename Clock::duration
>
class time_point;

 

There a four special time points depending on the clock.

  • epoch: The starting point of the clock.
  • now: The current time.
  • min: The minimum time point that the clock can display.
  • max: The maximum time point that the clock can display.

The accuracy of the values depends on the used clock:  std::system::system_clock, std::chrono::steady_clock, or std::chrono::high_resolution_clock.

C++ gives no guarantee about a clock's accuracy, starting point, or valid time range. The starting point of std::chrono::system_clock is typically the 1.1.1970, the so-called UNIX-epoch. It holds further that the std::chrono::high_resolution clock has the highest accuracy.

 

Rainer D 6 P2 540x540Modernes C++ Mentoring

Be part of my mentoring programs:

 

 

 

 

Do you want to stay informed about my mentoring programs: Subscribe via E-Mail.

Displaying a time duration as a date

If the time point uses internally std::chrono::system_clock, you can convert the time point with the help of std::chrono::system_clock::to_time_t in an object of type std::time_t. Thanks to further conversions with the functions std::gmtime and std::asctime, you have the time points as dates in textual representation available.

 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// timepoint.cpp

#include <chrono>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <string>

int main(){  
    
    std::cout << std::endl;
    
    std::chrono::time_point<std::chrono::system_clock> sysTimePoint;
    std::time_t tp= std::chrono::system_clock::to_time_t(sysTimePoint);
    std::string sTp= std::asctime(std::gmtime(&tp));
    std::cout << "Epoch: " << sTp << std::endl;
    
    tp= std::chrono::system_clock::to_time_t(sysTimePoint.min());
    sTp= std::asctime(std::gmtime(&tp));
    std::cout << "Time min: " << sTp << std::endl;
    
    tp= std::chrono::system_clock::to_time_t(sysTimePoint.max());
    sTp= std::asctime(std::gmtime(&tp));
    std::cout << "Time max: " << sTp << std::endl;
    
    sysTimePoint= std::chrono::system_clock::now();
    tp= std::chrono::system_clock::to_time_t(sysTimePoint);
    sTp= std::asctime(std::gmtime(&tp));
    std::cout << "Time now: " << sTp << std::endl;
    
}

 

The output of the program shows the valid range of std::chrono::system_clock. std::chrono::system_clock has the UNIX-epoch as starting point and can have time points between 1677 and 2262.

 

timepoint

You can add time durations to time points to get new time points. Now I'm curious. What will happen if I'm out of the valid range of the time points?

Beyond the boundaries of the valid time range

My experiment uses the current time and adds 1000 years or subtracts 1000 years from it. I ignore leap years and assume that the year has 365 days for simplicity.

 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// timepointAddition.cpp

#include <chrono>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <string>

using namespace std::chrono;

std::string timePointAsString(const time_point<system_clock>& timePoint){
    std::time_t tp= system_clock::to_time_t(timePoint);
    return std::asctime(std::gmtime(&tp));
}

int main(){  
    
    std::cout << std::endl;
    
    time_point<system_clock> nowTimePoint= system_clock::now();
    std::cout << "Now:              " << timePointAsString(nowTimePoint) << std::endl;
     
    auto thousandYears=  hours(24*365*1000);
    time_point<system_clock>  historyTimePoint= nowTimePoint - thousandYears;
    std::cout << "Now - 1000 years: " << timePointAsString(historyTimePoint) << std::endl;
    
    time_point<system_clock>  futureTimePoint= nowTimePoint + thousandYears;
    std::cout << "Now + 1000 years: " << timePointAsString(futureTimePoint) << std::endl;
     
}

 

For the sake of readability, I introduced the namespace std::chrono. The program's output shows that an overflow of the time points in lines 24 and 27 causes wrong results. Subtracting 1000 years from the current time point gives a time point in the future; adding 1000 years to the current time point gives a time point in the past, respectively.

timepointAddition

What's next?  

The difference between two time points is the time duration. Time durations support the basic arithmetic and can be displayed in different time ticks. How? Wait for the next post

 

 

 

 

Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Animus24, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Matthieu Bolt, Stephen Kelley, Kyle Dean, Tusar Palauri, Dmitry Farberov, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, and Rob North.

 

Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, and Slavko Radman.

 

 

My special thanks to Embarcadero CBUIDER STUDIO FINAL ICONS 1024 Small

 

My special thanks to PVS-Studio PVC Logo

 

My special thanks to Tipi.build tipi.build logo

 

My special thanks to Take Up Code TakeUpCode 450 60

 

Seminars

I'm happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.

Bookable (Online)

German

Standard Seminars (English/German)

Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.

  • C++ - The Core Language
  • C++ - The Standard Library
  • C++ - Compact
  • C++11 and C++14
  • Concurrency with Modern C++
  • Design Pattern and Architectural Pattern with C++
  • Embedded Programming with Modern C++
  • Generic Programming (Templates) with C++

New

  • Clean Code with Modern C++
  • C++20

Contact Me

Modernes C++,

RainerGrimmDunkelBlauSmall

Tags: Time

Comments   

-1 #1 Glenn 2017-01-04 14:20
Pretty! This has been a really wonderful article.
Many thanks for supplying this information.
Quote
0 #2 Newton 2017-04-16 06:22
I could not resist commenting. Very well written!
Quote
0 #3 datetime 2018-02-21 06:57
Keep this going please, great job!
Quote

Stay Informed about my Mentoring

 

Mentoring

English Books

Course: Modern C++ Concurrency in Practice

Course: C++ Standard Library including C++14 & C++17

Course: Embedded Programming with Modern C++

Course: Generic Programming (Templates)

Course: C++ Fundamentals for Professionals

Course: The All-in-One Guide to C++20

Course: Master Software Design Patterns and Architecture in C++

Subscribe to the newsletter (+ pdf bundle)

All tags

Blog archive

Source Code

Visitors

Today 3508

Yesterday 5555

Week 33716

Month 55390

All 12133599

Currently are 145 guests and no members online

Kubik-Rubik Joomla! Extensions

Latest comments