The Boost.Date_Time[1] library provides functions and classes to work with times and dates.

Getting the current time

The Date_Time library provides the following templates to get the current time.

Using the templates directly is unnecessary in general as the boost::gregorian and the boost::posix_time namespaces provide easier to use instantiations of the templates: boost::gregorian::day_clock, boost::posix_time::second_clock and boost::posix_time::microsec_clock.

In the following example we use each of these classes to get the current day or time and print it to standard output. The full example is available from our GitHub repository: BoostTutorials/Date_Time/Clocks1.

 File: main.cpp
#include <boost/date_time/posix_time/posix_time.hpp>
#include <iostream>

int main(int argc, char* argv[])
    // In this example we will use different clocks to get the current time
    // with different resolution levels
    // Get the current day only
    boost::gregorian::date currentDay = boost::gregorian::day_clock::universal_day();
    std::cout << "Day: " << currentDay << std::endl;

    // Get the current time with a resolution of 1 second
    boost::posix_time::ptime currentSecond = boost::posix_time::second_clock::universal_time();
    std::cout << "Time: " << currentSecond << std::endl;

    // Get the current time with a resolution of 1 microsecond
    boost::posix_time::ptime currentMicrosecond = boost::posix_time::microsec_clock::universal_time();
    std::cout << "Time: " << currentMicrosecond << std::endl;

    return 0;

This will produce the following output.

 Console output
Day: 2015-Dec-26
Time: 2015-Dec-26 17:39:26
Time: 2015-Dec-26 17:39:26.320328
Press any key to continue . . .


  1. Boost.Date_Time: official documentation (
  2. day_clock: official documentation (
  3. second_clock: official documentation (
  4. microsec_clock: official documentation (