oj mrJudge
Toggle navigation
  • Login
    • Forget Password
      Login
User Image

Hello, Stranger

Guest
  • Analysis Mode
  • Problems
    • All Problems
    • Latest Problems
  • Join Us Now
  • Registration
  • Contact Us
  • Infomation
  • C++ Reference
  • About
    • Help
    • Terms of Use
    • Technical Specifications
    • Credits

C++ Reference as of 28 May 2018

Date and time utilities - cppreference.com

Date and time utilities

From cppreference.com
< cpp
 
C++
Language
Headers
Library concepts
Language support library
Diagnostics library
Utilities library
Strings library
Containers library
Algorithms library
Iterators library
Numerics library
Input/output library
Localizations library
Regular expressions library (C++11)
Atomic operations library (C++11)
Thread support library (C++11)
Filesystem library (C++17)
Technical Specifications
 
Utilities library
Type support (basic types, RTTI, type traits)
Dynamic memory management
Error handling
Program utilities
Variadic functions
Date and time
Function objects
initializer_list
(C++11)
bitset
hash
(C++11)
integer_sequence
(C++14)
Relational operators (deprecated)
rel_ops::operator!=rel_ops::operator>rel_ops::operator<=rel_ops::operator>=
Comparisons (C++20)
strong_order
weak_order
partial_order

strong_equal
weak_equal
strong_ordering
weak_ordering
partial_ordering

strong_equality
weak_equality
is_eqis_neqis_ltis_lteqis_gtis_gteq
common_comparison_category
Common vocabulary types
pair
tuple
(C++11)
apply
(C++17)
make_from_tuple
(C++17)
optional
(C++17)
any
(C++17)
variant
(C++17)

Swap, forward and move
swap
exchange
(C++14)
forward
(C++11)
move
(C++11)
move_if_noexcept
(C++11)
Elementary string conversions
to_chars
(C++17)
from_chars
(C++17)
chars_format
(C++17)
Type operations
declval
(C++11)
as_const
(C++17)
 
Date and time utilities
duration
(C++11)
time_point
(C++11)
time_of_day
(C++20)
clock_time_conversion
(C++20)
clock_cast
(C++20)

Clocks
system_clock
(C++11)
steady_clock
(C++11)
high_resolution_clock
(C++11)
is_clock
(C++20)
                                                  
utc_clock
(C++20)
tai_clock
(C++20)
gps_clock
(C++20)
file_clock
(C++20)
local_t
(C++20)
Calendars
day
(C++20)
month
(C++20)
year
(C++20)
weekday
(C++20)
operator/
(C++20)
year_month_day
(C++20)
year_month_day_last
(C++20)
year_month_weekday
(C++20)
year_month_weekday_last
(C++20)
weekday_indexed
(C++20)
weekday_last
(C++20)

month_day
(C++20)
month_day_last
(C++20)
month_weekday
(C++20)
month_weekday_last
(C++20)

year_month
(C++20)
Time zones
tzdb
(C++20)
tzdb_list
(C++20)
get_tzdbget_tzdb_listreload_tzdbremote_version
(C++20)(C++20)(C++20)(C++20)
time_zone
(C++20)
locate_zone
(C++20)
current_zone
(C++20)
zoned_time
(C++20)
link
(C++20)
leap
(C++20)
C-style date and time
 

C++ includes support for two types of time manipulation:

  • The chrono library, a flexible collection of types that track time with varying degrees of precision (e.g. std::chrono::time_point).
  • C-style date and time library (e.g. std::time)

std::chrono library

The chrono library defines three main types as well as utility functions and common typedefs.

  • clocks
  • time points
  • durations

Clocks

A clock consists of a starting point (or epoch) and a tick rate. For example, a clock may have an epoch of January 1, 1970 and tick every second. C++ defines three clock types:

Defined in header <chrono>
Defined in namespace std::chrono
system_clock
(C++11)
wall clock time from the system-wide realtime clock
(class)
steady_clock
(C++11)
monotonic clock that will never be adjusted
(class)
high_resolution_clock
(C++11)
the clock with the shortest tick period available
(class)
is_clockis_clock_v
(C++20)
determines if a type is a Clock
(class template) (variable template)
utc_clock
(C++20)
Clock for Coordinated Universal Time (UTC)
(class)
tai_clock
(C++20)
Clock for International Atomic Time (TAI)
(class)
gps_clock
(C++20)
Clock for GPS time
(class)
file_clock
(C++20)
Clock used for file time
(class)
local_t
(C++20)
pseudo-clock representing local time
(class)

Time point

A time point is a duration of time that has passed since the epoch of a specific clock.

Defined in header <chrono>
Defined in namespace std::chrono
time_point
(C++11)
a point in time
(class template)
clock_time_conversion
(C++20)
traits class defining how to convert time points of one clock to another
(class template)
clock_cast
(C++20)
convert time points of one clock to another
(function template)

Duration

A duration consists of a span of time, defined as some number of ticks of some time unit. For example, "42 seconds" could be represented by a duration consisting of 42 ticks of a 1-second time unit.

Defined in header <chrono>
Defined in namespace std::chrono
duration
(C++11)
a time interval
(class template)

Time of day

time_of_day splits a duration representing time elapsed since midnight into hours, minutes, seconds, and fractional seconds, as applicable. It is primarily a formatting tool.

Defined in header <chrono>
Defined in namespace std::chrono
time_of_day
(C++20)
represents a time of day
(class template)

Calendar

Defined in header <chrono>
Defined in namespace std::chrono
last_spec
(C++20)
tag class indicating the last day or weekday in a month
(class)
day
(C++20)
represents a day of a month
(class)
month
(C++20)
represents a month of a year
(class)
year
(C++20)
represents a year in the Gregorian calendar
(class)
weekday
(C++20)
represents a day of the week in the Gregorian calendar
(class)
weekday_indexed
(C++20)
represents the n-th weekday of a month
(class)
weekday_last
(C++20)
represents the last weekday of a month
(class)
month_day
(C++20)
represents a specific day of a specific month
(class)
month_day_last
(C++20)
represents the last day of a specific month
(class)
month_weekday
(C++20)
represents the n-th weekday of a specific month
(class)
month_weekday_last
(C++20)
represents the last weekday of a specific month
(class)
year_month
(C++20)
represents a specific month of a specific year
(class)
year_month_day
(C++20)
represents a specific year, month, and day
(class)
year_month_day_last
(C++20)
represents the last day of a specific year and month
(class)
year_month_weekday
(C++20)
represents the n-th weekday of a specific year and month
(class)
year_month_weekday_last
(C++20)
represents the last weekday of a specific year and month
(class)
operator/
(C++20)
conventional syntax for Gregorian calendar date creation
(function)

Time zone

Defined in header <chrono>
Defined in namespace std::chrono
tzdb
(C++20)
describes a copy of the IANA time zone database
(class)
tzdb_list
(C++20)
represents a linked list of tzdb
(class)
get_tzdbget_tzdb_listreload_tzdbremote_version
(C++20)
accesses and controls the global time zone database information
(function)
locate_zone
(C++20)
locates a time_zone based on its name
(function)
current_zone
(C++20)
returns the current time_zone
(function)
time_zone
(C++20)
represents a time zone
(class)
sys_info
(C++20)
represents information about a time zone at a particular time point
(class)
local_info
(C++20)
represents information about a local time to UNIX time conversion
(class)
zoned_time
(C++20)
represents a time zone and a time point
(class)
leap
(C++20)
contains information about a leap second insertion
(class)
link
(C++20)
represents an alternative name for a time zone
(class)
nonexistent_local_time
(C++20)
exception thrown to report that a local time is nonexistent
(class)
ambiguous_local_time
(C++20)
exception thrown to report that a local time is ambiguous
(class)

chrono I/O

Defined in header <chrono>
Defined in namespace std::chrono
format
formats a streamable chrono object for insertion
(function template)
parse
parses a chrono object from a stream
(function template)


C-style date and time library

Also provided are the C-style date and time functions, such as std::time_t, std::difftime, and CLOCKS_PER_SEC.

Example

This example displays information about the execution time of a function call:

Run this code
#include <iostream>
#include <chrono>
#include <ctime>
 
long fibonacci(unsigned n)
{
    if (n < 2) return n;
    return fibonacci(n-1) + fibonacci(n-2);
}
 
int main()
{
    auto start = std::chrono::system_clock::now();
    std::cout << "f(42) = " << fibonacci(42) << '\n';
    auto end = std::chrono::system_clock::now();
 
    std::chrono::duration<double> elapsed_seconds = end-start;
    std::time_t end_time = std::chrono::system_clock::to_time_t(end);
 
    std::cout << "finished computation at " << std::ctime(&end_time)
              << "elapsed time: " << elapsed_seconds.count() << "s\n";
}

Possible output:

f(42) = 267914296
finished computation at Mon Oct  2 00:59:08 2017
elapsed time: 1.88232s
Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/chrono&oldid=100945"
Navigation
  • Online version
  • Offline version retrieved 2018-04-14 22:05.
  • This page was last modified on 6 April 2018, at 14:57.
  • This page has been accessed 697,452 times.
mrJudge 31.05.2018 (F43DD)
Copyright © 2018 mrJudge. All rights reserved.

Under Construction

Stats Tab Content

Under Construction too