├── sublibs ├── .gitignore ├── README.md ├── test ├── quick.cpp ├── test_12176.cpp ├── CMakeLists.txt ├── intmax_c.cpp ├── test_10778.cpp ├── cmake_install_test │ └── CMakeLists.txt ├── test_11006.cpp ├── another_obj.cpp ├── test_9337.cpp ├── one_obj.cpp ├── duration │ ├── nonmember │ │ ├── times_rep2_lhs_fail.cpp │ │ ├── times_rep2_rhs_fail.cpp │ │ ├── divide_rep2_fail.cpp │ │ └── modulus_rep2_fail.cpp │ ├── cons │ │ ├── implicit_constructot_fail.cpp │ │ ├── treat_as_floating_point_Rep2_true_fail.cpp │ │ ├── convert_inexact_fail.cpp │ │ ├── non_implicit_convertible_rep_fail.cpp │ │ └── convert_float_to_int_fail.cpp │ ├── duration_cast_int_fail.cpp │ ├── ratio_fail.cpp │ ├── positive_num_fail.cpp │ ├── duration_duration_fail.cpp │ ├── ratio_alias_pass.cpp │ ├── default_ratio_pass.cpp │ ├── types_pass.cpp │ ├── typedefs_pass.cpp │ ├── duration_cast_pass.cpp │ ├── duration_values_pass.cpp │ ├── constructor_pass.cpp │ └── rounding_pass.cpp ├── time_point │ ├── duration.fail.cpp │ ├── not_duration_fail.cpp │ ├── cons │ │ ├── implicit_fail.cpp │ │ └── non_implicit_convertible_duration_fail.cpp │ ├── default_duration.pass.cpp │ ├── default_duration_pass.cpp │ ├── time_point_cast_int_fail.cpp │ ├── comparisons │ │ ├── equal_fail.cpp │ │ └── less_fail.cpp │ ├── min_max_pass.cpp │ ├── arithmetic_ext_pass.cpp │ ├── time_point_cast_pass.cpp │ ├── constructor_pass.cpp │ └── comparisons_pass.cpp ├── test_11012.cpp ├── cmake_subdir_test │ └── CMakeLists.txt ├── clock.h ├── traits │ ├── treat_as_floating_point_pass.cpp │ ├── duration_values_pass.cpp │ ├── common_type_duration_pass.cpp │ └── common_type_time_point_pass.cpp ├── rep.h ├── test_10631.cpp ├── clock │ └── errored_clock.hpp ├── test_7868.cpp └── io │ └── duration_output.cpp ├── example ├── test_minmax.cpp ├── run_timer_example.cpp ├── clock_name.cpp ├── run_timer_example2.cpp ├── io_ex4.cpp ├── io_ex2.cpp ├── chrono_unit_test.cpp ├── test_thread_clock.cpp ├── io_ex3.cpp ├── rounding.cpp ├── manipulate_clock_object.cpp ├── test_special_values.cpp ├── explore_limits.cpp ├── timer.hpp ├── clock_name.hpp ├── await_keystroke.cpp ├── io_ex5.cpp ├── min_time_point.cpp ├── xtime.cpp ├── io_ex1.cpp ├── i_dont_like_the_default_duration_behavior.cpp ├── french.cpp └── miscellaneous.cpp ├── meta └── libraries.json ├── include └── boost │ ├── chrono │ ├── chrono.hpp │ ├── detail │ │ ├── system.hpp │ │ ├── is_evenly_divisible_by.hpp │ │ ├── requires_cxx11.hpp │ │ ├── static_assert.hpp │ │ ├── no_warning │ │ │ └── signed_unsigned_cmp.hpp │ │ └── inlined │ │ │ ├── process_cpu_clocks.hpp │ │ │ ├── thread_clock.hpp │ │ │ ├── chrono.hpp │ │ │ ├── posix │ │ │ ├── thread_clock.hpp │ │ │ └── chrono.hpp │ │ │ ├── win │ │ │ ├── thread_clock.hpp │ │ │ └── chrono.hpp │ │ │ └── mac │ │ │ └── thread_clock.hpp │ ├── clock_string.hpp │ ├── include.hpp │ ├── typeof │ │ └── boost │ │ │ ├── ratio.hpp │ │ │ └── chrono │ │ │ └── chrono.hpp │ ├── floor.hpp │ ├── ceil.hpp │ ├── io │ │ ├── timezone.hpp │ │ ├── duration_style.hpp │ │ ├── utility │ │ │ ├── to_string.hpp │ │ │ └── manip_base.hpp │ │ └── ios_base_state.hpp │ ├── chrono_io.hpp │ ├── round.hpp │ └── thread_clock.hpp │ └── chrono.hpp ├── index.html ├── src ├── chrono.cpp ├── thread_clock.cpp └── process_cpu_clocks.cpp ├── appveyor.yml ├── CMakeLists.txt ├── doc └── Jamfile.v2 ├── perf ├── store_now_in_vector.cpp └── Jamfile.v2 └── .gitattributes /sublibs: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.project 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Chrono 2 | ====== 3 | 4 | Useful time utilities. C++11. 5 | 6 | ### License 7 | 8 | Distributed under the [Boost Software License, Version 1.0](http://boost.org/LICENSE_1_0.txt). 9 | -------------------------------------------------------------------------------- /test/quick.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2024 Peter Dimov 2 | // Distributed under the Boost Software License, Version 1.0. 3 | // https://www.boost.org/LICENSE_1_0.txt 4 | 5 | #include 6 | #include 7 | 8 | int main() 9 | { 10 | std::cout << boost::chrono::system_clock::now() << std::endl; 11 | } 12 | -------------------------------------------------------------------------------- /test/test_12176.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2016 Vicente J. Botet Escriba 2 | // Distributed under the Boost Software License, Version 1.0. 3 | // See http://www.boost.org/LICENSE_1_0.txt 4 | // See http://www.boost.org/libs/chrono for documentation. 5 | 6 | #include 7 | 8 | int main() 9 | { 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /test/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright 2018, 2019, 2021, 2022 Peter Dimov 2 | # Distributed under the Boost Software License, Version 1.0. 3 | # See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt 4 | 5 | include(BoostTestJamfile OPTIONAL RESULT_VARIABLE HAVE_BOOST_TEST) 6 | 7 | if(HAVE_BOOST_TEST) 8 | 9 | boost_test_jamfile(FILE Jamfile.v2 10 | LINK_LIBRARIES Boost::chrono Boost::core) 11 | 12 | endif() 13 | -------------------------------------------------------------------------------- /example/test_minmax.cpp: -------------------------------------------------------------------------------- 1 | // test_duration.cpp ----------------------------------------------------------// 2 | 3 | // Copyright 2009 Vicente J. Botet Escriba 4 | 5 | // Distributed under the Boost Software License, Version 1.0. 6 | // See http://www.boost.org/LICENSE_1_0.txt 7 | 8 | #if !defined(__GNUC__) 9 | 10 | #define min(A,B) ((A)<(B)?(A):(B)) 11 | #define max(A,B) ((A)>(B)?(A):(B)) 12 | 13 | #include 14 | 15 | #endif 16 | 17 | 18 | -------------------------------------------------------------------------------- /meta/libraries.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "key": "chrono", 4 | "name": "Chrono", 5 | "authors": [ 6 | "Howard Hinnant", 7 | "Beman Dawes", 8 | "Vicente J. Botet Escriba" 9 | ], 10 | "maintainers": [ 11 | "Vicente J. Botet Escriba " 12 | ], 13 | "description": 14 | "Useful time utilities. C++11.", 15 | "std": [ "proposal" ], 16 | "category": [ 17 | "Domain", "System" 18 | ], 19 | "cxxstd": "11" 20 | } 21 | ] 22 | -------------------------------------------------------------------------------- /test/intmax_c.cpp: -------------------------------------------------------------------------------- 1 | // intmax_c.cpp --------------------------------------------------------------// 2 | 3 | // Copyright 2010 Vicente J. Botet Escriba 4 | 5 | // Distributed under the Boost Software License, Version 1.0. 6 | // See http://www.boost.org/LICENSE_1_0.txt 7 | 8 | #include 9 | 10 | #ifdef INTMAX_C 11 | #define BOOST_INTMAX_C(a) INTMAX_C(a) 12 | #else 13 | #define BOOST_INTMAX_C(a) a##LL 14 | #endif 15 | 16 | boost::intmax_t i = BOOST_INTMAX_C(1000000000); 17 | int main() { 18 | return (i); 19 | } 20 | -------------------------------------------------------------------------------- /include/boost/chrono/chrono.hpp: -------------------------------------------------------------------------------- 1 | // chrono.hpp --------------------------------------------------------------// 2 | 3 | // Copyright 2009-2011 Vicente J. Botet Escriba 4 | 5 | // Distributed under the Boost Software License, Version 1.0. 6 | // See http://www.boost.org/LICENSE_1_0.txt 7 | 8 | #ifndef BOOST_CHRONO_CHRONO_HPP 9 | #define BOOST_CHRONO_CHRONO_HPP 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | #endif // BOOST_CHRONO_CHRONO_HPP 16 | -------------------------------------------------------------------------------- /include/boost/chrono/detail/system.hpp: -------------------------------------------------------------------------------- 1 | // Copyright 2009-2010 Vicente J. Botet Escriba 2 | 3 | // Distributed under the Boost Software License, Version 1.0. 4 | // See http://www.boost.org/LICENSE_1_0.txt 5 | 6 | #ifndef BOOST_CHRONO_DETAIL_SYSTEM_HPP 7 | #define BOOST_CHRONO_DETAIL_SYSTEM_HPP 8 | 9 | #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING 10 | 11 | #include 12 | 13 | namespace boost { 14 | namespace chrono { 15 | inline bool is_throws(system::error_code & ec) { return (&ec==&boost::throws()); } 16 | } 17 | } 18 | 19 | #endif 20 | #endif 21 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Automatic redirection failed, please go to 7 | ../../doc/html/chrono.html 8 |

© Copyright 2009-2010 Vicente J. Botet Escribá. 9 | Distributed under the Boost Software 10 | License, Version 1.0. (See accompanying file 11 | LICENSE_1_0.txt or copy at 12 | http://www.boost.org/LICENSE_1_0.txt) 13 |

14 | 15 | 16 | -------------------------------------------------------------------------------- /src/chrono.cpp: -------------------------------------------------------------------------------- 1 | // chrono.cpp --------------------------------------------------------------// 2 | 3 | // Copyright Beman Dawes 2008 4 | // Copyright Vicente J. Botet Escriba 2009-2010 5 | 6 | // Distributed under the Boost Software License, Version 1.0. 7 | // See http://www.boost.org/LICENSE_1_0.txt 8 | 9 | // define BOOST_CHRONO_SOURCE so that knows 10 | // the library is being built (possibly exporting rather than importing code) 11 | 12 | #ifndef BOOST_CHRONO_SOURCE 13 | # define BOOST_CHRONO_SOURCE 14 | #endif 15 | 16 | #include 17 | 18 | -------------------------------------------------------------------------------- /test/test_10778.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2015 Vicente J. Botet Escriba 2 | // Distributed under the Boost Software License, Version 1.0. 3 | // See http://www.boost.org/LICENSE_1_0.txt 4 | // See http://www.boost.org/libs/chrono for documentation. 5 | 6 | //#define BOOST_CHRONO_PROVIDES_DEPRECATED_IO_SINCE_V2_0_0 7 | #include 8 | #include 9 | #include 10 | 11 | void test() 12 | { 13 | std::atomic ms; // error C2338: atomic requires T to be trivially copyable. 14 | } 15 | 16 | int main() { 17 | test(); 18 | return 1; 19 | } 20 | -------------------------------------------------------------------------------- /test/cmake_install_test/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright 2018, 2019, 2023 Peter Dimov 2 | # Distributed under the Boost Software License, Version 1.0. 3 | # See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt 4 | 5 | cmake_minimum_required(VERSION 3.5...3.16) 6 | 7 | project(cmake_install_test LANGUAGES CXX) 8 | 9 | find_package(boost_chrono REQUIRED) 10 | 11 | add_executable(quick ../quick.cpp) 12 | target_link_libraries(quick Boost::chrono) 13 | 14 | enable_testing() 15 | add_test(quick quick) 16 | 17 | add_custom_target(check VERBATIM COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure -C $) 18 | -------------------------------------------------------------------------------- /test/test_11006.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2015 Vicente J. Botet Escriba 2 | // Distributed under the Boost Software License, Version 1.0. 3 | // See http://www.boost.org/LICENSE_1_0.txt 4 | // See http://www.boost.org/libs/chrono for documentation. 5 | 6 | //#define BOOST_CHRONO_PROVIDES_DEPRECATED_IO_SINCE_V2_0_0 7 | #define BOOST_CHRONO_VERSION 2 8 | #include 9 | #include 10 | 11 | 12 | int main() { 13 | { 14 | boost::chrono::time_fmt_io_saver<> tmp(std::cout); 15 | } 16 | { 17 | boost::chrono::time_fmt_io_saver<> tmp(std::cout, "%Y-%m-%d %H:%M:%S"); 18 | } 19 | return 0; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /example/run_timer_example.cpp: -------------------------------------------------------------------------------- 1 | // run_timer_example.cpp ---------------------------------------------------// 2 | 3 | // Copyright Beman Dawes 2006, 2008 4 | // Copyright 2009/2010 Vicente J. Botet Escriba 5 | 6 | // Distributed under the Boost Software License, Version 1.0. 7 | // See http://www.boost.org/LICENSE_1_0.txt 8 | 9 | // See http://www.boost.org/libs/chrono for documentation. 10 | 11 | #include 12 | #include 13 | 14 | int main() 15 | { 16 | boost::chrono::run_timer t; 17 | 18 | for ( long i = 0; i < 10000; ++i ) 19 | std::sqrt( 123.456L ); // burn some time 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /include/boost/chrono/clock_string.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // (C) Copyright 2010-2011 Vicente J. Botet Escriba 3 | // Use, modification and distribution are subject to the Boost Software License, 4 | // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at 5 | // http://www.boost.org/LICENSE_1_0.txt). 6 | // 7 | 8 | #ifndef BOOST_CHRONO_CLOCK_STRING_HPP 9 | #define BOOST_CHRONO_CLOCK_STRING_HPP 10 | 11 | #include 12 | 13 | namespace boost 14 | { 15 | namespace chrono 16 | { 17 | 18 | template 19 | struct clock_string; 20 | 21 | } // chrono 22 | 23 | } // boost 24 | 25 | #endif // BOOST_CHRONO_CLOCK_STRING_HPP 26 | -------------------------------------------------------------------------------- /test/another_obj.cpp: -------------------------------------------------------------------------------- 1 | // boost win32_test.cpp -----------------------------------------------------// 2 | 3 | // Copyright 2010 Vicente J. Botet Escriba 4 | 5 | // Distributed under the Boost Software License, Version 1.0. 6 | // See http://www.boost.org/LICENSE_1_0.txt 7 | 8 | // See http://www.boost.org/libs/chrono for documentation. 9 | #include 10 | #include 11 | #include 12 | 13 | void another() { 14 | boost::chrono::steady_clock::time_point t1=boost::chrono::steady_clock::now(); 15 | boost::chrono::steady_clock::time_point t2=boost::chrono::steady_clock::now(); 16 | std::cout << t2-t1 << std::endl; 17 | return ; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /example/clock_name.cpp: -------------------------------------------------------------------------------- 1 | // stopclock_perf.cpp ---------------------------------------------------// 2 | 3 | // Copyright 2009 Vicente J. Botet Escriba 4 | 5 | // Distributed under the Boost Software License, Version 1.0. 6 | // See http://www.boost.org/LICENSE_1_0.txt 7 | 8 | // See http://www.boost.org/libs/chrono for documentation. 9 | 10 | #include "clock_name.hpp" 11 | #include 12 | 13 | int main() 14 | { 15 | std::cout << name::apply() << '\n'; 16 | #ifdef BOOST_CHRONO_HAS_CLOCK_STEADY 17 | std::cout << name::apply() << '\n'; 18 | #endif 19 | std::cout << name::apply() << '\n'; 20 | } 21 | -------------------------------------------------------------------------------- /test/test_9337.cpp: -------------------------------------------------------------------------------- 1 | //#define BOOST_CHRONO_PROVIDES_DEPRECATED_IO_SINCE_V2_0_0 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | using namespace boost::chrono; 9 | 10 | template 11 | void test() 12 | { 13 | typename Clock::time_point start=Clock::now(); 14 | sleep(1); 15 | typename Clock::time_point stop=Clock::now(); 16 | cout<(); 23 | test(); 24 | test(); 25 | test(); 26 | return 1; 27 | } 28 | -------------------------------------------------------------------------------- /test/one_obj.cpp: -------------------------------------------------------------------------------- 1 | // boost win32_test.cpp -----------------------------------------------------// 2 | 3 | // Copyright 2010 Vicente J. Botet Escriba 4 | 5 | // Distributed under the Boost Software License, Version 1.0. 6 | // See http://www.boost.org/LICENSE_1_0.txt 7 | 8 | // See http://www.boost.org/libs/chrono for documentation. 9 | #include 10 | #include 11 | #include 12 | 13 | void another(); 14 | 15 | int main() 16 | { 17 | boost::chrono::steady_clock::time_point t1=boost::chrono::steady_clock::now(); 18 | another(); 19 | boost::chrono::steady_clock::time_point t2=boost::chrono::steady_clock::now(); 20 | std::cout << t2-t1 << std::endl; 21 | return 0; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /src/thread_clock.cpp: -------------------------------------------------------------------------------- 1 | // boost thread_clock.cpp -----------------------------------------------------------// 2 | 3 | // Copyright 2010 Vicente J. Botet Escriba 4 | 5 | // Distributed under the Boost Software License, Version 1.0. 6 | // See http://www.boost.org/LICENSE_1_0.txt 7 | 8 | // See http://www.boost.org/libs/chrono for documentation. 9 | 10 | //--------------------------------------------------------------------------------------// 11 | 12 | 13 | // define BOOST_CHRONO_SOURCE so that knows 14 | // the library is being built (possibly exporting rather than importing code) 15 | 16 | #ifndef BOOST_CHRONO_SOURCE 17 | # define BOOST_CHRONO_SOURCE 18 | #endif 19 | 20 | #include 21 | 22 | -------------------------------------------------------------------------------- /example/run_timer_example2.cpp: -------------------------------------------------------------------------------- 1 | // run_timer_example.cpp ---------------------------------------------------// 2 | 3 | // Copyright Beman Dawes 2006, 2008 4 | 5 | // Distributed under the Boost Software License, Version 1.0. 6 | // See http://www.boost.org/LICENSE_1_0.txt 7 | 8 | // See http://www.boost.org/libs/chrono for documentation. 9 | 10 | #include 11 | #include 12 | 13 | int main( int argc, char * argv[] ) 14 | { 15 | const char * format = argc > 1 ? argv[1] : "%t cpu seconds\n"; 16 | int places = argc > 2 ? std::atoi( argv[2] ) : 2; 17 | 18 | boost::chrono::run_timer t( format, places ); 19 | 20 | for ( long i = 0; i < 10000; ++i ) 21 | std::sqrt( 123.456L ); // burn some time 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /src/process_cpu_clocks.cpp: -------------------------------------------------------------------------------- 1 | // boost process_cpu_clocks.cpp -----------------------------------------------------------// 2 | 3 | // Copyright 2009-2010 Vicente J. Botet Escriba 4 | 5 | // Distributed under the Boost Software License, Version 1.0. 6 | // See http://www.boost.org/LICENSE_1_0.txt 7 | 8 | // See http://www.boost.org/libs/chrono for documentation. 9 | 10 | //--------------------------------------------------------------------------------------// 11 | 12 | // define BOOST_CHRONO_SOURCE so that knows 13 | // the library is being built (possibly exporting rather than importing code) 14 | 15 | #ifndef BOOST_CHRONO_SOURCE 16 | # define BOOST_CHRONO_SOURCE 17 | #endif 18 | 19 | #include 20 | 21 | -------------------------------------------------------------------------------- /include/boost/chrono.hpp: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////// 2 | // 3 | // (C) Copyright Vicente J. Botet Escriba 2010. 4 | // Distributed under the Boost 5 | // Software License, Version 1.0. 6 | // (See accompanying file LICENSE_1_0.txt or 7 | // copy at http://www.boost.org/LICENSE_1_0.txt) 8 | // 9 | // See http://www.boost.org/libs/stm for documentation. 10 | // 11 | ////////////////////////////////////////////////////////////////////////////// 12 | 13 | #ifndef BOOST_CHRONO_HPP 14 | #define BOOST_CHRONO_HPP 15 | 16 | //----------------------------------------------------------------------------- 17 | #include 18 | //----------------------------------------------------------------------------- 19 | 20 | #endif // BOOST_CHRONO_HPP 21 | -------------------------------------------------------------------------------- /test/duration/nonmember/times_rep2_lhs_fail.cpp: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // The LLVM Compiler Infrastructure 4 | // 5 | // This file is dual licensed under the MIT and the University of Illinois Open 6 | // Source Licenses. See LICENSE.TXT for details. 7 | // 8 | //===----------------------------------------------------------------------===// 9 | // Adaptation to Boost of the libcxx 10 | // Copyright 2010 Vicente J. Botet Escriba 11 | // Distributed under the Boost Software License, Version 1.0. 12 | // See http://www.boost.org/LICENSE_1_0.txt 13 | 14 | #include 15 | 16 | #include "../../rep.h" 17 | 18 | void test() 19 | { 20 | boost::chrono::duration d; 21 | d = 5 * d; 22 | } 23 | -------------------------------------------------------------------------------- /test/duration/nonmember/times_rep2_rhs_fail.cpp: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // The LLVM Compiler Infrastructure 4 | // 5 | // This file is dual licensed under the MIT and the University of Illinois Open 6 | // Source Licenses. See LICENSE.TXT for details. 7 | // 8 | //===----------------------------------------------------------------------===// 9 | // Adaptation to Boost of the libcxx 10 | // Copyright 2010 Vicente J. Botet Escriba 11 | // Distributed under the Boost Software License, Version 1.0. 12 | // See http://www.boost.org/LICENSE_1_0.txt 13 | 14 | #include 15 | 16 | #include "../../rep.h" 17 | 18 | void test() 19 | { 20 | boost::chrono::duration d; 21 | d = d * 5; 22 | } 23 | -------------------------------------------------------------------------------- /test/duration/nonmember/divide_rep2_fail.cpp: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // The LLVM Compiler Infrastructure 4 | // 5 | // This file is dual licensed under the MIT and the University of Illinois Open 6 | // Source Licenses. See LICENSE.TXT for details. 7 | // 8 | //===----------------------------------------------------------------------===// 9 | // Adaptation to Boost of the libcxx 10 | // Copyright 2010 Vicente J. Botet Escriba 11 | // Distributed under the Boost Software License, Version 1.0. 12 | // See http://www.boost.org/LICENSE_1_0.txt 13 | 14 | #include 15 | 16 | #include "../../rep.h" 17 | 18 | void test() 19 | { 20 | boost::chrono::duration d(Rep(15)); 21 | d = d / 5; 22 | } 23 | -------------------------------------------------------------------------------- /test/duration/nonmember/modulus_rep2_fail.cpp: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // The LLVM Compiler Infrastructure 4 | // 5 | // This file is dual licensed under the MIT and the University of Illinois Open 6 | // Source Licenses. See LICENSE.TXT for details. 7 | // 8 | //===----------------------------------------------------------------------===// 9 | // Adaptation to Boost of the libcxx 10 | // Copyright 2010 Vicente J. Botet Escriba 11 | // Distributed under the Boost Software License, Version 1.0. 12 | // See http://www.boost.org/LICENSE_1_0.txt 13 | 14 | #include 15 | 16 | #include "../../rep.h" 17 | 18 | void test() 19 | { 20 | boost::chrono::duration d(Rep(15)); 21 | d = d % 5; 22 | } 23 | -------------------------------------------------------------------------------- /test/duration/cons/implicit_constructot_fail.cpp: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // The LLVM Compiler Infrastructure 4 | // 5 | // This file is dual licensed under the MIT and the University of Illinois Open 6 | // Source Licenses. See LICENSE.TXT for details. 7 | // 8 | //===----------------------------------------------------------------------===// 9 | // Adaptation to Boost of the libcxx 10 | // Copyright 2010 Vicente J. Botet Escriba 11 | // Distributed under the Boost Software License, Version 1.0. 12 | // See http://www.boost.org/LICENSE_1_0.txt 13 | 14 | // test for explicit 15 | 16 | #include 17 | 18 | #include "../../rep.h" 19 | 20 | void test() 21 | { 22 | boost::chrono::duration d = 1; 23 | } 24 | -------------------------------------------------------------------------------- /test/duration/cons/treat_as_floating_point_Rep2_true_fail.cpp: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // The LLVM Compiler Infrastructure 4 | // 5 | // This file is dual licensed under the MIT and the University of Illinois Open 6 | // Source Licenses. See LICENSE.TXT for details. 7 | // 8 | //===----------------------------------------------------------------------===// 9 | // Adaptation to Boost of the libcxx 10 | // Copyright 2010 Vicente J. Botet Escriba 11 | // Distributed under the Boost Software License, Version 1.0. 12 | // See http://www.boost.org/LICENSE_1_0.txt 13 | 14 | // treat_as_floating_point::value must be false 15 | 16 | #include 17 | 18 | void test() 19 | { 20 | boost::chrono::duration d(1.); 21 | } 22 | -------------------------------------------------------------------------------- /test/duration/duration_cast_int_fail.cpp: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // The LLVM Compiler Infrastructure 4 | // 5 | // This file is dual licensed under the MIT and the University of Illinois Open 6 | // Source Licenses. See LICENSE.TXT for details. 7 | // 8 | //===----------------------------------------------------------------------===// 9 | // Adaptation to Boost of the libcxx 10 | // Copyright 2010 Vicente J. Botet Escriba 11 | // Distributed under the Boost Software License, Version 1.0. 12 | // See http://www.boost.org/LICENSE_1_0.txt 13 | 14 | // ToDuration must be an instantiation of duration. 15 | 16 | #include 17 | 18 | void test() 19 | { 20 | boost::chrono::duration_cast(boost::chrono::milliseconds(3)); 21 | } 22 | -------------------------------------------------------------------------------- /test/time_point/duration.fail.cpp: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // The LLVM Compiler Infrastructure 4 | // 5 | // This file is dual licensed under the MIT and the University of Illinois Open 6 | // Source Licenses. See LICENSE.TXT for details. 7 | // 8 | //===----------------------------------------------------------------------===// 9 | // Adaptation to Boost of the libcxx 10 | // Copyright 2010 Vicente J. Botet Escriba 11 | // Distributed under the Boost Software License, Version 1.0. 12 | // See http://www.boost.org/LICENSE_1_0.txt 13 | 14 | // Duration shall be an instance of duration. 15 | 16 | #include 17 | 18 | void test() 19 | { 20 | typedef boost::chrono::time_point T; 21 | T t; 22 | } 23 | -------------------------------------------------------------------------------- /test/time_point/not_duration_fail.cpp: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // The LLVM Compiler Infrastructure 4 | // 5 | // This file is dual licensed under the MIT and the University of Illinois Open 6 | // Source Licenses. See LICENSE.TXT for details. 7 | // 8 | //===----------------------------------------------------------------------===// 9 | // Adaptation to Boost of the libcxx 10 | // Copyright 2010 Vicente J. Botet Escriba 11 | // Distributed under the Boost Software License, Version 1.0. 12 | // See http://www.boost.org/LICENSE_1_0.txt 13 | 14 | // Duration shall be an instance of duration. 15 | 16 | #include 17 | 18 | void test() 19 | { 20 | typedef boost::chrono::time_point T; 21 | T t; 22 | } 23 | -------------------------------------------------------------------------------- /test/duration/cons/convert_inexact_fail.cpp: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // The LLVM Compiler Infrastructure 4 | // 5 | // This file is dual licensed under the MIT and the University of Illinois Open 6 | // Source Licenses. See LICENSE.TXT for details. 7 | // 8 | //===----------------------------------------------------------------------===// 9 | // Adaptation to Boost of the libcxx 10 | // Copyright 2010 Vicente J. Botet Escriba 11 | // Distributed under the Boost Software License, Version 1.0. 12 | // See http://www.boost.org/LICENSE_1_0.txt 13 | 14 | // inexact conversions disallowed for integral reps 15 | 16 | #include 17 | 18 | void test() 19 | { 20 | boost::chrono::microseconds us(1); 21 | boost::chrono::milliseconds ms = us; 22 | } 23 | -------------------------------------------------------------------------------- /test/duration/cons/non_implicit_convertible_rep_fail.cpp: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // The LLVM Compiler Infrastructure 4 | // 5 | // This file is dual licensed under the MIT and the University of Illinois Open 6 | // Source Licenses. See LICENSE.TXT for details. 7 | // 8 | //===----------------------------------------------------------------------===// 9 | // Adaptation to Boost of the libcxx 10 | // Copyright 2010 Vicente J. Botet Escriba 11 | // Distributed under the Boost Software License, Version 1.0. 12 | // See http://www.boost.org/LICENSE_1_0.txt 13 | 14 | // Rep2 shall be implicitly convertible to rep 15 | 16 | #include 17 | 18 | #include "../../rep.h" 19 | 20 | void test() 21 | { 22 | boost::chrono::duration d(1); 23 | } 24 | -------------------------------------------------------------------------------- /test/duration/ratio_fail.cpp: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // The LLVM Compiler Infrastructure 4 | // 5 | // This file is dual licensed under the MIT and the University of Illinois Open 6 | // Source Licenses. See LICENSE.TXT for details. 7 | // 8 | //===----------------------------------------------------------------------===// 9 | // Adaptation to Boost of the libcxx 10 | // Copyright 2010 Vicente J. Botet Escriba 11 | // Distributed under the Boost Software License, Version 1.0. 12 | // See http://www.boost.org/LICENSE_1_0.txt 13 | 14 | // duration 15 | // Period shall be a specialization of ratio, diagnostic required. 16 | 17 | #include 18 | 19 | void test() 20 | { 21 | typedef boost::chrono::duration D; 22 | D d; 23 | } 24 | -------------------------------------------------------------------------------- /include/boost/chrono/include.hpp: -------------------------------------------------------------------------------- 1 | 2 | // include 3 | // 4 | // (C) Copyright 2011 Vicente J. Botet Escriba 5 | // Use, modification and distribution are subject to the Boost Software License, 6 | // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at 7 | // http://www.boost.org/LICENSE_1_0.txt). 8 | // 9 | // This code was adapted by Vicente from Howard Hinnant's experimental work 10 | // on chrono i/o under lvm/libc++ to Boost 11 | 12 | #ifndef BOOST_CHRONO_INCLUDE_HPP 13 | #define BOOST_CHRONO_INCLUDE_HPP 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #endif // BOOST_CHRONO_INCLUDE_HPP 24 | -------------------------------------------------------------------------------- /test/duration/positive_num_fail.cpp: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // The LLVM Compiler Infrastructure 4 | // 5 | // This file is dual licensed under the MIT and the University of Illinois Open 6 | // Source Licenses. See LICENSE.TXT for details. 7 | // 8 | //===----------------------------------------------------------------------===// 9 | // Adaptation to Boost of the libcxx 10 | // Copyright 2010 Vicente J. Botet Escriba 11 | // Distributed under the Boost Software License, Version 1.0. 12 | // See http://www.boost.org/LICENSE_1_0.txt 13 | 14 | // duration 15 | // Period::num must be positive, diagnostic required. 16 | 17 | #include 18 | 19 | void test() 20 | { 21 | typedef boost::chrono::duration > D; 22 | D d; 23 | } 24 | -------------------------------------------------------------------------------- /test/duration/cons/convert_float_to_int_fail.cpp: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // The LLVM Compiler Infrastructure 4 | // 5 | // This file is dual licensed under the MIT and the University of Illinois Open 6 | // Source Licenses. See LICENSE.TXT for details. 7 | // 8 | //===----------------------------------------------------------------------===// 9 | // Adaptation to Boost of the libcxx 10 | // Copyright 2010 Vicente J. Botet Escriba 11 | // Distributed under the Boost Software License, Version 1.0. 12 | // See http://www.boost.org/LICENSE_1_0.txt 13 | 14 | // conversions from floating point to integral durations disallowed 15 | 16 | #include 17 | 18 | void test() 19 | { 20 | boost::chrono::duration d; 21 | boost::chrono::duration i = d; 22 | } 23 | -------------------------------------------------------------------------------- /test/test_11012.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2015 Vicente J. Botet Escriba 2 | // Distributed under the Boost Software License, Version 1.0. 3 | // See http://www.boost.org/LICENSE_1_0.txt 4 | // See http://www.boost.org/libs/chrono for documentation. 5 | 6 | //#define BOOST_CHRONO_VERSION 1 7 | #define BOOST_CHRONO_VERSION 2 8 | #include 9 | #include 10 | #include 11 | //#define BOOST_CHRONO_DONT_PROVIDES_DEPRECATED_IO_SINCE_V2_0_0 12 | #include 13 | 14 | int main() 15 | { 16 | { 17 | typedef boost::chrono::duration RationalSeconds; 18 | RationalSeconds d(0.5); 19 | std::cout << d << std::endl; 20 | } 21 | { 22 | typedef boost::chrono::duration > RationalSeconds; 23 | RationalSeconds d; 24 | std::cout << d << std::endl; 25 | } 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /include/boost/chrono/detail/is_evenly_divisible_by.hpp: -------------------------------------------------------------------------------- 1 | // is_evenly_divisible_by.hpp --------------------------------------------------------------// 2 | 3 | // Copyright 2009-2010 Vicente J. Botet Escriba 4 | 5 | // Distributed under the Boost Software License, Version 1.0. 6 | // See http://www.boost.org/LICENSE_1_0.txt 7 | 8 | #ifndef BOOST_CHRONO_DETAIL_IS_EVENLY_DIVISIBLE_BY_HPP 9 | #define BOOST_CHRONO_DETAIL_IS_EVENLY_DIVISIBLE_BY_HPP 10 | 11 | #include 12 | 13 | namespace boost { 14 | namespace chrono { 15 | namespace chrono_detail { 16 | 17 | template 18 | struct is_evenly_divisible_by : public boost::ratio_detail::is_evenly_divisible_by 19 | {}; 20 | 21 | } // namespace chrono_detail 22 | } // namespace detail 23 | } // namespace chrono 24 | 25 | #endif // BOOST_CHRONO_DETAIL_IS_EVENLY_DIVISIBLE_BY_HPP 26 | -------------------------------------------------------------------------------- /test/time_point/cons/implicit_fail.cpp: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // The LLVM Compiler Infrastructure 4 | // 5 | // This file is dual licensed under the MIT and the University of Illinois Open 6 | // Source Licenses. See LICENSE.TXT for details. 7 | // 8 | //===----------------------------------------------------------------------===// 9 | // Adaptation to Boost of the libcxx 10 | // Copyright 2010 Vicente J. Botet Escriba 11 | // Distributed under the Boost Software License, Version 1.0. 12 | // See http://www.boost.org/LICENSE_1_0.txt 13 | 14 | // test for explicit 15 | 16 | #include 17 | 18 | void test() 19 | { 20 | typedef boost::chrono::system_clock Clock; 21 | typedef boost::chrono::milliseconds Duration; 22 | boost::chrono::time_point t = Duration(3); 23 | } 24 | -------------------------------------------------------------------------------- /include/boost/chrono/typeof/boost/ratio.hpp: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////// 2 | // 3 | // (C) Copyright Vicente J. Botet Escriba 20010. 4 | // Distributed under the Boost Software License, Version 1.0. 5 | // (See accompanying file LICENSE_1_0.txt or 6 | // copy at http://www.boost.org/LICENSE_1_0.txt) 7 | // 8 | // Based on the unique_threader/unique_joiner design from of Kevlin Henney (n1883) 9 | // 10 | // See http://www.boost.org/libs/chrono for documentation. 11 | // 12 | ////////////////////////////////////////////////////////////////////////////// 13 | 14 | #ifndef BOOST_CHRONO_TYPEOF_RATIO_HPP 15 | #define BOOST_CHRONO_TYPEOF_RATIO_HPP 16 | 17 | #include 18 | #include 19 | 20 | #include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP() 21 | 22 | BOOST_TYPEOF_REGISTER_TEMPLATE(boost::ratio, (boost::intmax_t)(boost::intmax_t)) 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /test/duration/duration_duration_fail.cpp: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // The LLVM Compiler Infrastructure 4 | // 5 | // This file is dual licensed under the MIT and the University of Illinois Open 6 | // Source Licenses. See LICENSE.TXT for details. 7 | // 8 | //===----------------------------------------------------------------------===// 9 | // Adaptation to Boost of the libcxx 10 | // Copyright 2010 Vicente J. Botet Escriba 11 | // Distributed under the Boost Software License, Version 1.0. 12 | // See http://www.boost.org/LICENSE_1_0.txt 13 | 14 | // duration 15 | // If a program instantiates duration with a duration type for the template 16 | // argument Rep a diagnostic is required. 17 | 18 | #include 19 | 20 | void test() 21 | { 22 | typedef boost::chrono::duration D; 23 | D d; 24 | } 25 | -------------------------------------------------------------------------------- /include/boost/chrono/detail/requires_cxx11.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BOOST_CHRONO_DETAIL_REQUIRES_CXX11_HPP_INCLUDED 2 | #define BOOST_CHRONO_DETAIL_REQUIRES_CXX11_HPP_INCLUDED 3 | 4 | // Copyright 2023 Peter Dimov 5 | // Distributed under the Boost Software License, Version 1.0. 6 | // https://www.boost.org/LICENSE_1_0.txt 7 | 8 | #include 9 | #include 10 | 11 | #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || \ 12 | defined(BOOST_NO_CXX11_RVALUE_REFERENCES) || \ 13 | defined(BOOST_NO_CXX11_DECLTYPE) || \ 14 | defined(BOOST_NO_CXX11_CONSTEXPR) || \ 15 | defined(BOOST_NO_CXX11_NOEXCEPT) || \ 16 | defined(BOOST_NO_CXX11_HDR_CHRONO) || \ 17 | defined(BOOST_NO_CXX11_HDR_RATIO) 18 | 19 | BOOST_PRAGMA_MESSAGE("C++03 support was deprecated in Boost.Chrono 1.82 and was removed in Boost.Chrono 1.84.") 20 | 21 | #endif 22 | 23 | #endif // #ifndef BOOST_CHRONO_DETAIL_REQUIRES_CXX11_HPP_INCLUDED 24 | -------------------------------------------------------------------------------- /example/io_ex4.cpp: -------------------------------------------------------------------------------- 1 | // io_ex1.cpp ----------------------------------------------------------// 2 | 3 | // Copyright 2010 Howard Hinnant 4 | // Copyright 2010 Vicente J. Botet Escriba 5 | 6 | // Distributed under the Boost Software License, Version 1.0. 7 | // See http://www.boost.org/LICENSE_1_0.txt 8 | 9 | /* 10 | This code was adapted by Vicente J. Botet Escriba from Hinnant's html documentation. 11 | Many thanks to Howard for making his code available under the Boost license. 12 | 13 | */ 14 | 15 | #include 16 | #include 17 | 18 | int main() 19 | { 20 | using std::cout; 21 | using namespace boost; 22 | using namespace boost::chrono; 23 | 24 | #ifdef BOOST_CHRONO_HAS_CLOCK_STEADY 25 | typedef time_point > > T; 26 | T tp = steady_clock::now(); 27 | std::cout << tp << '\n'; 28 | #endif 29 | return 0; 30 | } 31 | 32 | //~ 17.8666 hours since boot 33 | -------------------------------------------------------------------------------- /include/boost/chrono/floor.hpp: -------------------------------------------------------------------------------- 1 | // boost/chrono/round.hpp ------------------------------------------------------------// 2 | 3 | // (C) Copyright Howard Hinnant 4 | // Copyright 2011 Vicente J. Botet Escriba 5 | 6 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 7 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 8 | 9 | // See http://www.boost.org/libs/chrono for documentation. 10 | 11 | #ifndef BOOST_CHRONO_FLOOR_HPP 12 | #define BOOST_CHRONO_FLOOR_HPP 13 | 14 | #include 15 | 16 | namespace boost 17 | { 18 | namespace chrono 19 | { 20 | 21 | /** 22 | * rounds down 23 | */ 24 | template 25 | To floor(const duration& d) 26 | { 27 | To t = duration_cast(d); 28 | if (t>d) --t; 29 | return t; 30 | } 31 | 32 | 33 | } // namespace chrono 34 | } // namespace boost 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /include/boost/chrono/ceil.hpp: -------------------------------------------------------------------------------- 1 | // boost/chrono/round.hpp ------------------------------------------------------------// 2 | 3 | // (C) Copyright Howard Hinnant 4 | // Copyright 2011 Vicente J. Botet Escriba 5 | 6 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 7 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 8 | 9 | // See http://www.boost.org/libs/chrono for documentation. 10 | 11 | #ifndef BOOST_CHRONO_CEIL_HPP 12 | #define BOOST_CHRONO_CEIL_HPP 13 | 14 | #include 15 | 16 | namespace boost 17 | { 18 | namespace chrono 19 | { 20 | 21 | /** 22 | * rounds up 23 | */ 24 | template 25 | To ceil(const duration& d) 26 | { 27 | To t = duration_cast(d); 28 | if (t < d) 29 | ++t; 30 | return t; 31 | } 32 | 33 | } // namespace chrono 34 | } // namespace boost 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /test/duration/ratio_alias_pass.cpp: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // The LLVM Compiler Infrastructure 4 | // 5 | // This file is dual licensed under the MIT and the University of Illinois Open 6 | // Source Licenses. See LICENSE.TXT for details. 7 | // 8 | //===----------------------------------------------------------------------===// 9 | // Adaptation to Boost of the libcxx 10 | // Copyright 2010 Vicente J. Botet Escriba 11 | // Distributed under the Boost Software License, Version 1.0. 12 | // See http://www.boost.org/LICENSE_1_0.txt 13 | 14 | // duration 15 | // Period shall be a specialization of ratio, diagnostic required. 16 | 17 | #include 18 | 19 | void test() 20 | { 21 | typedef boost::chrono::duration, 24 | boost::ratio<1,3> 25 | > 26 | > D; 27 | D d; 28 | (void)d; 29 | } 30 | -------------------------------------------------------------------------------- /include/boost/chrono/io/timezone.hpp: -------------------------------------------------------------------------------- 1 | // (C) Copyright Howard Hinnant 2 | // (C) Copyright 2010-2011 Vicente J. Botet Escriba 3 | // Use, modification and distribution are subject to the Boost Software License, 4 | // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at 5 | // http://www.boost.org/LICENSE_1_0.txt). 6 | // 7 | // This code was adapted by Vicente from Howard Hinnant's experimental work 8 | // on chrono i/o to Boost 9 | 10 | #ifndef BOOST_CHRONO_IO_TIMEZONE_HPP 11 | #define BOOST_CHRONO_IO_TIMEZONE_HPP 12 | 13 | #include 14 | 15 | namespace boost 16 | { 17 | namespace chrono 18 | { 19 | /** 20 | * Scoped enumeration emulation stating whether the time_point for system_clock I/O is UTC or local. 21 | */ 22 | BOOST_SCOPED_ENUM_DECLARE_BEGIN(timezone) 23 | { 24 | utc, local 25 | } 26 | BOOST_SCOPED_ENUM_DECLARE_END(timezone) 27 | 28 | } // chrono 29 | } // boost 30 | 31 | #endif // header 32 | -------------------------------------------------------------------------------- /test/time_point/default_duration.pass.cpp: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // The LLVM Compiler Infrastructure 4 | // 5 | // This file is dual licensed under the MIT and the University of Illinois Open 6 | // Source Licenses. See LICENSE.TXT for details. 7 | // 8 | //===----------------------------------------------------------------------===// 9 | // Adaptation to Boost of the libcxx 10 | // Copyright 2010 Vicente J. Botet Escriba 11 | // Distributed under the Boost Software License, Version 1.0. 12 | // See http://www.boost.org/LICENSE_1_0.txt 13 | 14 | #include 15 | #include 16 | #if !defined(BOOST_NO_CXX11_STATIC_ASSERT) 17 | #define NOTHING "" 18 | #endif 19 | 20 | void test() 21 | { 22 | BOOST_CHRONO_STATIC_ASSERT((boost::is_same::duration>::value), NOTHING, ()); 24 | } 25 | -------------------------------------------------------------------------------- /test/time_point/default_duration_pass.cpp: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // The LLVM Compiler Infrastructure 4 | // 5 | // This file is dual licensed under the MIT and the University of Illinois Open 6 | // Source Licenses. See LICENSE.TXT for details. 7 | // 8 | //===----------------------------------------------------------------------===// 9 | // Adaptation to Boost of the libcxx 10 | // Copyright 2010 Vicente J. Botet Escriba 11 | // Distributed under the Boost Software License, Version 1.0. 12 | // See http://www.boost.org/LICENSE_1_0.txt 13 | 14 | #include 15 | #include 16 | #if !defined(BOOST_NO_CXX11_STATIC_ASSERT) 17 | #define NOTHING "" 18 | #endif 19 | 20 | void test() 21 | { 22 | BOOST_CHRONO_STATIC_ASSERT((boost::is_same::duration>::value), NOTHING, ()); 24 | } 25 | -------------------------------------------------------------------------------- /example/io_ex2.cpp: -------------------------------------------------------------------------------- 1 | // io_ex2.cpp ----------------------------------------------------------// 2 | 3 | // Copyright 2010 Howard Hinnant 4 | // Copyright 2010 Vicente J. Botet Escriba 5 | 6 | // Distributed under the Boost Software License, Version 1.0. 7 | // See http://www.boost.org/LICENSE_1_0.txt 8 | 9 | /* 10 | This code was adapted by Vicente J. Botet Escriba from Hinnant's html documentation. 11 | Many thanks to Howard for making his code available under the Boost license. 12 | 13 | */ 14 | 15 | #include 16 | #include 17 | #include 18 | 19 | int main() 20 | { 21 | using namespace boost::chrono; 22 | 23 | std::istringstream in("5000 milliseconds 4000 ms 3001 ms"); 24 | seconds d(0); 25 | in >> d; 26 | BOOST_ASSERT(in.good()); 27 | BOOST_ASSERT(d == seconds(5)); 28 | in >> d; 29 | BOOST_ASSERT(in.good()); 30 | BOOST_ASSERT(d == seconds(4)); 31 | in >> d; 32 | BOOST_ASSERT(in.fail()); 33 | BOOST_ASSERT(d == seconds(4)); 34 | return 0; 35 | } 36 | 37 | -------------------------------------------------------------------------------- /example/chrono_unit_test.cpp: -------------------------------------------------------------------------------- 1 | // chrono_unit_test.cpp ----------------------------------------------------// 2 | 3 | // Copyright 2008 Beman Dawes 4 | 5 | // Distributed under the Boost Software License, Version 1.0. 6 | // See http://www.boost.org/LICENSE_1_0.txt 7 | 8 | #define _CRT_SECURE_NO_WARNINGS // disable VC++ foolishness 9 | 10 | #include 11 | #include 12 | 13 | 14 | int main() 15 | { 16 | std::time_t sys_time 17 | = boost::chrono::system_clock::to_time_t(boost::chrono::system_clock::now()); 18 | 19 | #ifdef UNDER_CE 20 | // Windows CE does not define asctime() 21 | struct tm * t = std::gmtime(&sys_time); 22 | std::cout 23 | << "system_clock::to_time_t(system_clock::now()) is " 24 | << t->tm_mon << "/" << t->tm_mday << "/" << (1900 + t->tm_year) << " " << t->tm_hour << ":" << t->tm_min << ":" << t->tm_sec << std::endl; 25 | #else 26 | std::cout 27 | << "system_clock::to_time_t(system_clock::now()) is " 28 | << std::asctime(std::gmtime(&sys_time)) << std::endl; 29 | #endif 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /test/cmake_subdir_test/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright 2018, 2019, 2023 Peter Dimov 2 | # Distributed under the Boost Software License, Version 1.0. 3 | # See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt 4 | 5 | cmake_minimum_required(VERSION 3.5...3.16) 6 | 7 | project(cmake_subdir_test LANGUAGES CXX) 8 | 9 | add_subdirectory(../.. boostorg/chrono) 10 | 11 | set(deps 12 | # Primary dependencies 13 | 14 | assert 15 | config 16 | core 17 | integer 18 | move 19 | mpl 20 | predef 21 | ratio 22 | static_assert 23 | system 24 | throw_exception 25 | type_traits 26 | typeof 27 | utility 28 | winapi 29 | 30 | # Secondary dependencies 31 | 32 | preprocessor 33 | variant2 34 | io 35 | mp11 36 | ) 37 | 38 | foreach(dep IN LISTS deps) 39 | 40 | add_subdirectory(../../../${dep} boostorg/${dep}) 41 | 42 | endforeach() 43 | 44 | add_executable(quick ../quick.cpp) 45 | target_link_libraries(quick Boost::chrono) 46 | 47 | enable_testing() 48 | add_test(quick quick) 49 | 50 | add_custom_target(check VERBATIM COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure -C $) 51 | -------------------------------------------------------------------------------- /test/time_point/time_point_cast_int_fail.cpp: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // The LLVM Compiler Infrastructure 4 | // 5 | // This file is dual licensed under the MIT and the University of Illinois Open 6 | // Source Licenses. See LICENSE.TXT for details. 7 | // 8 | //===----------------------------------------------------------------------===// 9 | // Adaptation to Boost of the libcxx 10 | // Copyright 2010 Vicente J. Botet Escriba 11 | // Distributed under the Boost Software License, Version 1.0. 12 | // See http://www.boost.org/LICENSE_1_0.txt 13 | 14 | // ToDuration shall be an instantiation of duration. 15 | 16 | #include 17 | 18 | void test() 19 | { 20 | typedef boost::chrono::system_clock Clock; 21 | typedef boost::chrono::time_point FromTimePoint; 22 | typedef boost::chrono::time_point ToTimePoint; 23 | boost::chrono::time_point_cast(FromTimePoint(boost::chrono::milliseconds(3))); 24 | } 25 | -------------------------------------------------------------------------------- /test/duration/default_ratio_pass.cpp: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // The LLVM Compiler Infrastructure 4 | // 5 | // This file is dual licensed under the MIT and the University of Illinois Open 6 | // Source Licenses. See LICENSE.TXT for details. 7 | // 8 | //===----------------------------------------------------------------------===// 9 | // Adaptation to Boost of the libcxx 10 | // Copyright 2010 Vicente J. Botet Escriba 11 | // Distributed under the Boost Software License, Version 1.0. 12 | // See http://www.boost.org/LICENSE_1_0.txt 13 | 14 | // duration 15 | // Test default template arg: 16 | 17 | // template > 18 | // class duration; 19 | 20 | #include 21 | #include 22 | #if !defined(BOOST_NO_CXX11_STATIC_ASSERT) 23 | #define NOTHING "" 24 | #endif 25 | 26 | BOOST_CHRONO_STATIC_ASSERT((boost::is_same< 27 | boost::chrono::duration >, 28 | boost::chrono::duration 29 | >::value), NOTHING, ()); 30 | -------------------------------------------------------------------------------- /test/time_point/cons/non_implicit_convertible_duration_fail.cpp: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // The LLVM Compiler Infrastructure 4 | // 5 | // This file is dual licensed under the MIT and the University of Illinois Open 6 | // Source Licenses. See LICENSE.TXT for details. 7 | // 8 | //===----------------------------------------------------------------------===// 9 | // Adaptation to Boost of the libcxx 10 | // Copyright 2010 Vicente J. Botet Escriba 11 | // Distributed under the Boost Software License, Version 1.0. 12 | // See http://www.boost.org/LICENSE_1_0.txt 13 | 14 | // Duration2 shall be implicitly convertible to duration. 15 | 16 | #include 17 | 18 | void test() 19 | { 20 | typedef boost::chrono::system_clock Clock; 21 | typedef boost::chrono::milliseconds Duration1; 22 | typedef boost::chrono::microseconds Duration2; 23 | { 24 | boost::chrono::time_point t2(Duration2(3)); 25 | boost::chrono::time_point t1 = t2; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /test/clock.h: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // The LLVM Compiler Infrastructure 4 | // 5 | // This file is dual licensed under the MIT and the University of Illinois Open 6 | // Source Licenses. See LICENSE.TXT for details. 7 | // 8 | //===----------------------------------------------------------------------===// 9 | // Adaptation to Boost of the libcxx 10 | // Copyright 2010 Vicente J. Botet Escriba 11 | // Distributed under the Boost Software License, Version 1.0. 12 | // See http://www.boost.org/LICENSE_1_0.txt 13 | 14 | #ifndef CLOCK_H 15 | #define CLOCK_H 16 | 17 | #include 18 | 19 | class Clock 20 | { 21 | typedef boost::chrono::nanoseconds duration; 22 | typedef duration::rep rep; 23 | typedef duration::period period; 24 | typedef boost::chrono::time_point time_point; 25 | static const bool is_steady = false; 26 | 27 | static time_point now(); 28 | }; 29 | 30 | #endif // CLOCK_H 31 | -------------------------------------------------------------------------------- /test/duration/types_pass.cpp: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // The LLVM Compiler Infrastructure 4 | // 5 | // This file is dual licensed under the MIT and the University of Illinois Open 6 | // Source Licenses. See LICENSE.TXT for details. 7 | // 8 | //===----------------------------------------------------------------------===// 9 | // Adaptation to Boost of the libcxx 10 | // Copyright 2010 Vicente J. Botet Escriba 11 | // Distributed under the Boost Software License, Version 1.0. 12 | // See http://www.boost.org/LICENSE_1_0.txt 13 | 14 | // duration 15 | // Test nested types 16 | 17 | // typedef Rep rep; 18 | // typedef Period period; 19 | 20 | #include 21 | #include 22 | #if !defined(BOOST_NO_CXX11_STATIC_ASSERT) 23 | #define NOTHING "" 24 | #endif 25 | 26 | typedef boost::chrono::duration > D; 27 | BOOST_CHRONO_STATIC_ASSERT((boost::is_same::value), NOTHING, ()); 28 | BOOST_CHRONO_STATIC_ASSERT((boost::is_same >::value), NOTHING, ()); 29 | -------------------------------------------------------------------------------- /include/boost/chrono/chrono_io.hpp: -------------------------------------------------------------------------------- 1 | 2 | // chrono_io 3 | // 4 | // (C) Copyright Howard Hinnant 5 | // (C) Copyright 2010-2011 Vicente J. Botet Escriba 6 | // Use, modification and distribution are subject to the Boost Software License, 7 | // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at 8 | // http://www.boost.org/LICENSE_1_0.txt). 9 | // 10 | // This code was adapted by Vicente from Howard Hinnant's experimental work 11 | // on chrono i/o under lvm/libc++ to Boost 12 | 13 | #ifndef BOOST_CHRONO_CHRONO_IO_HPP 14 | #define BOOST_CHRONO_CHRONO_IO_HPP 15 | 16 | #include 17 | 18 | //#if BOOST_CHRONO_VERSION == 2 19 | //#include 20 | //#include 21 | //#elif BOOST_CHRONO_VERSION == 1 22 | //#include 23 | //#endif 24 | 25 | #if defined BOOST_CHRONO_DONT_PROVIDES_DEPRECATED_IO_SINCE_V2_0_0 26 | #include 27 | #include 28 | #else 29 | #include 30 | #endif 31 | 32 | #include 33 | 34 | #endif // BOOST_CHRONO_CHRONO_IO_HPP 35 | -------------------------------------------------------------------------------- /test/time_point/comparisons/equal_fail.cpp: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // The LLVM Compiler Infrastructure 4 | // 5 | // This file is dual licensed under the MIT and the University of Illinois Open 6 | // Source Licenses. See LICENSE.TXT for details. 7 | // 8 | //===----------------------------------------------------------------------===// 9 | // Adaptation to Boost of the libcxx 10 | // Copyright 2010 Vicente J. Botet Escriba 11 | // Distributed under the Boost Software License, Version 1.0. 12 | // See http://www.boost.org/LICENSE_1_0.txt 13 | 14 | // time_points with different clocks should not compare 15 | 16 | #include 17 | 18 | #include "../../clock.h" 19 | 20 | void test() 21 | { 22 | typedef boost::chrono::system_clock Clock1; 23 | typedef Clock Clock2; 24 | typedef boost::chrono::milliseconds Duration1; 25 | typedef boost::chrono::microseconds Duration2; 26 | typedef boost::chrono::time_point T1; 27 | typedef boost::chrono::time_point T2; 28 | 29 | T1 t1(Duration1(3)); 30 | T2 t2(Duration2(3000)); 31 | t1 == t2; 32 | } 33 | -------------------------------------------------------------------------------- /test/time_point/comparisons/less_fail.cpp: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // The LLVM Compiler Infrastructure 4 | // 5 | // This file is dual licensed under the MIT and the University of Illinois Open 6 | // Source Licenses. See LICENSE.TXT for details. 7 | // 8 | //===----------------------------------------------------------------------===// 9 | // Adaptation to Boost of the libcxx 10 | // Copyright 2010 Vicente J. Botet Escriba 11 | // Distributed under the Boost Software License, Version 1.0. 12 | // See http://www.boost.org/LICENSE_1_0.txt 13 | 14 | // time_points with different clocks should not compare 15 | 16 | #include 17 | 18 | #include "../../clock.h" 19 | 20 | void test() 21 | { 22 | typedef boost::chrono::system_clock Clock1; 23 | typedef Clock Clock2; 24 | typedef boost::chrono::milliseconds Duration1; 25 | typedef boost::chrono::microseconds Duration2; 26 | typedef boost::chrono::time_point T1; 27 | typedef boost::chrono::time_point T2; 28 | 29 | T1 t1(Duration1(3)); 30 | T2 t2(Duration2(3000)); 31 | t1 < t2; 32 | } 33 | -------------------------------------------------------------------------------- /include/boost/chrono/io/duration_style.hpp: -------------------------------------------------------------------------------- 1 | // (C) Copyright Howard Hinnant 2 | // (C) Copyright 2011 Vicente J. Botet Escriba 3 | // Use, modification and distribution are subject to the Boost Software License, 4 | // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at 5 | // http://www.boost.org/LICENSE_1_0.txt). 6 | // 7 | // This code was adapted by Vicente from Howard Hinnant's experimental work 8 | // on chrono i/o to Boost 9 | 10 | #ifndef BOOST_CHRONO_IO_DURATION_STYLE_HPP 11 | #define BOOST_CHRONO_IO_DURATION_STYLE_HPP 12 | 13 | #include 14 | 15 | namespace boost 16 | { 17 | namespace chrono 18 | { 19 | /** 20 | * Scoped enumeration emulation stating whether the duration I/O style is long or short. 21 | * prefix means duration::rep with whatever stream/locale settings are set for it followed by a long name representing the unit 22 | * symbol means duration::rep with whatever stream/locale settings are set for it followed by a SI unit abbreviation 23 | */ 24 | BOOST_SCOPED_ENUM_DECLARE_BEGIN(duration_style) 25 | { 26 | prefix, symbol 27 | } 28 | BOOST_SCOPED_ENUM_DECLARE_END(duration_style) 29 | 30 | 31 | } // chrono 32 | 33 | } 34 | 35 | #endif // header 36 | -------------------------------------------------------------------------------- /include/boost/chrono/typeof/boost/chrono/chrono.hpp: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////// 2 | // 3 | // (C) Copyright Vicente J. Botet Escriba 20010. 4 | // Distributed under the Boost Software License, Version 1.0. 5 | // (See accompanying file LICENSE_1_0.txt or 6 | // copy at http://www.boost.org/LICENSE_1_0.txt) 7 | // 8 | // Based on the unique_threader/unique_joiner design from of Kevlin Henney (n1883) 9 | // 10 | // See http://www.boost.org/libs/chrono for documentation. 11 | // 12 | ////////////////////////////////////////////////////////////////////////////// 13 | 14 | #ifndef BOOST_CHRONO_TYPEOF_CHRONO_HPP 15 | #define BOOST_CHRONO_TYPEOF_CHRONO_HPP 16 | 17 | #include 18 | #include 19 | 20 | #include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP() 21 | 22 | BOOST_TYPEOF_REGISTER_TEMPLATE(boost::chrono::duration, (typename)(typename)) 23 | BOOST_TYPEOF_REGISTER_TEMPLATE(boost::chrono::time_point, (typename)(typename)) 24 | #if 0 25 | BOOST_TYPEOF_REGISTER_TYPE(boost::chrono::system_clock) 26 | #ifdef BOOST_CHRONO_HAS_CLOCK_STEADY 27 | BOOST_TYPEOF_REGISTER_TYPE(boost::chrono::steady_clock) 28 | #endif 29 | BOOST_TYPEOF_REGISTER_TYPE(boost::chrono::high_resolution_clock) 30 | 31 | #endif 32 | #endif 33 | -------------------------------------------------------------------------------- /test/traits/treat_as_floating_point_pass.cpp: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // The LLVM Compiler Infrastructure 4 | // 5 | // This file is dual licensed under the MIT and the University of Illinois Open 6 | // Source Licenses. See LICENSE.TXT for details. 7 | // 8 | //===----------------------------------------------------------------------===// 9 | // Adaptation to Boost of the libcxx 10 | // Copyright 2010 Vicente J. Botet Escriba 11 | // Distributed under the Boost Software License, Version 1.0. 12 | // See http://www.boost.org/LICENSE_1_0.txt 13 | 14 | #include 15 | #include 16 | 17 | #if !defined(BOOST_NO_CXX11_STATIC_ASSERT) 18 | #define NOTHING "" 19 | #endif 20 | 21 | template 22 | void 23 | test() 24 | { 25 | BOOST_CHRONO_STATIC_ASSERT((boost::is_base_of, 26 | boost::chrono::treat_as_floating_point >::value), NOTHING, ()); 27 | } 28 | 29 | struct A {}; 30 | 31 | void testall() 32 | { 33 | test(); 34 | test(); 35 | test(); 36 | test(); 37 | test(); 38 | test(); 39 | test(); 40 | test(); 41 | } 42 | -------------------------------------------------------------------------------- /example/test_thread_clock.cpp: -------------------------------------------------------------------------------- 1 | // test_thread_clock.cpp ----------------------------------------------------------// 2 | 3 | // Copyright 2009 Vicente J. Botet Escriba 4 | 5 | // Distributed under the Boost Software License, Version 1.0. 6 | // See http://www.boost.org/LICENSE_1_0.txt 7 | 8 | 9 | #include 10 | #include 11 | 12 | #include 13 | 14 | 15 | void test_thread_clock() 16 | { 17 | #if defined(BOOST_CHRONO_HAS_THREAD_CLOCK) 18 | using namespace boost::chrono; 19 | 20 | std::cout << "thread_clock test" << std::endl; 21 | thread_clock::duration delay = milliseconds(5); 22 | thread_clock::time_point start = thread_clock::now(); 23 | while (thread_clock::now() - start <= delay) 24 | ; 25 | thread_clock::time_point stop = thread_clock::now(); 26 | thread_clock::duration elapsed = stop - start; 27 | std::cout << "paused " << nanoseconds(elapsed).count() << " nanoseconds\n"; 28 | start = thread_clock::now(); 29 | stop = thread_clock::now(); 30 | std::cout << "thread_clock resolution estimate: " << nanoseconds(stop-start).count() << " nanoseconds\n"; 31 | #else 32 | std::cout << "thread_clock not available\n"; 33 | #endif 34 | } 35 | 36 | 37 | int main() 38 | { 39 | test_thread_clock(); 40 | return 0; 41 | } 42 | 43 | -------------------------------------------------------------------------------- /include/boost/chrono/detail/static_assert.hpp: -------------------------------------------------------------------------------- 1 | // static_assert.hpp --------------------------------------------------------------// 2 | 3 | // Copyright 2009-2010 Vicente J. Botet Escriba 4 | 5 | // Distributed under the Boost Software License, Version 1.0. 6 | // See http://www.boost.org/LICENSE_1_0.txt 7 | 8 | 9 | #ifndef BOOST_CHRONO_DETAIL_STATIC_ASSERT_HPP 10 | #define BOOST_CHRONO_DETAIL_STATIC_ASSERT_HPP 11 | 12 | #include 13 | 14 | #ifndef BOOST_NO_CXX11_STATIC_ASSERT 15 | #define BOOST_CHRONO_STATIC_ASSERT(CND, MSG, TYPES) static_assert(CND,MSG) 16 | #elif defined(BOOST_CHRONO_USES_STATIC_ASSERT) 17 | #include 18 | #define BOOST_CHRONO_STATIC_ASSERT(CND, MSG, TYPES) BOOST_STATIC_ASSERT(CND) 19 | #elif defined(BOOST_CHRONO_USES_MPL_ASSERT) 20 | #include 21 | #include 22 | #define BOOST_CHRONO_STATIC_ASSERT(CND, MSG, TYPES) \ 23 | BOOST_MPL_ASSERT_MSG(boost::mpl::bool_< (CND) >::type::value, MSG, TYPES) 24 | #else 25 | //~ #elif defined(BOOST_CHRONO_USES_ARRAY_ASSERT) 26 | #define BOOST_CHRONO_STATIC_ASSERT(CND, MSG, TYPES) static char BOOST_JOIN(boost_chrono_test_,__LINE__)[(CND)?1:-1] 27 | //~ #define BOOST_CHRONO_STATIC_ASSERT(CND, MSG, TYPES) 28 | #endif 29 | 30 | #endif // BOOST_CHRONO_DETAIL_STATIC_ASSERT_HPP 31 | -------------------------------------------------------------------------------- /test/time_point/min_max_pass.cpp: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // The LLVM Compiler Infrastructure 4 | // 5 | // This file is dual licensed under the MIT and the University of Illinois Open 6 | // Source Licenses. See LICENSE.TXT for details. 7 | // 8 | //===----------------------------------------------------------------------===// 9 | // Adaptation to Boost of the libcxx 10 | // Copyright 2010 Vicente J. Botet Escriba 11 | // Distributed under the Boost Software License, Version 1.0. 12 | // See http://www.boost.org/LICENSE_1_0.txt 13 | 14 | #include 15 | #include 16 | 17 | #if defined BOOST_NO_CXX11_NUMERIC_LIMITS || defined BOOST_NO_CXX11_CONSTEXPR 18 | #define BOOST_CONSTEXPR_ASSERT(C) BOOST_TEST(C) 19 | #else 20 | #include 21 | #define BOOST_CONSTEXPR_ASSERT(C) BOOST_STATIC_ASSERT(C) 22 | #endif 23 | 24 | int main() 25 | { 26 | typedef boost::chrono::system_clock Clock; 27 | typedef boost::chrono::milliseconds Duration; 28 | typedef boost::chrono::time_point TP; 29 | 30 | BOOST_CONSTEXPR_ASSERT((TP::min)() == TP((Duration::min)())); 31 | BOOST_CONSTEXPR_ASSERT((TP::max)() == TP((Duration::max)())); 32 | 33 | return boost::report_errors(); 34 | } 35 | -------------------------------------------------------------------------------- /example/io_ex3.cpp: -------------------------------------------------------------------------------- 1 | // io_ex1.cpp ----------------------------------------------------------// 2 | 3 | // Copyright 2010 Howard Hinnant 4 | // Copyright 2010 Vicente J. Botet Escriba 5 | 6 | // Distributed under the Boost Software License, Version 1.0. 7 | // See http://www.boost.org/LICENSE_1_0.txt 8 | 9 | /* 10 | This code was adapted by Vicente J. Botet Escriba from Hinnant's html documentation. 11 | Many thanks to Howard for making his code available under the Boost license. 12 | 13 | */ 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | int main() 21 | { 22 | using namespace boost::chrono; 23 | using std::cout; 24 | 25 | high_resolution_clock::time_point t0 = high_resolution_clock::now(); 26 | std::stringstream io; 27 | io << t0; 28 | BOOST_ASSERT(!io.fail()); 29 | cout << io.str() << '\n'; 30 | BOOST_ASSERT(!io.fail()); 31 | high_resolution_clock::time_point t1; 32 | io >> t1; 33 | BOOST_ASSERT(!io.fail()); 34 | cout << io.str() << '\n'; 35 | cout << t0 << '\n'; 36 | cout << t1 << '\n'; 37 | high_resolution_clock::time_point t = high_resolution_clock::now(); 38 | cout << t << '\n'; 39 | 40 | cout << "That took " << t - t0 << '\n'; 41 | cout << "That took " << t - t1 << '\n'; 42 | 43 | return 0; 44 | } 45 | 46 | //~ 50908679121461 nanoseconds since boot 47 | //~ That took 649630 nanoseconds 48 | 49 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | # Copyright 2016, 2017, 2022 Peter Dimov 2 | # Distributed under the Boost Software License, Version 1.0. 3 | # (See accompanying file LICENSE_1_0.txt or copy at http://boost.org/LICENSE_1_0.txt) 4 | 5 | version: 1.0.{build}-{branch} 6 | 7 | shallow_clone: true 8 | 9 | branches: 10 | only: 11 | - master 12 | - develop 13 | - /feature\/.*/ 14 | 15 | environment: 16 | matrix: 17 | - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 18 | TOOLSET: msvc-9.0,msvc-10.0,msvc-11.0 19 | ADDRMD: 32 20 | - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 21 | TOOLSET: msvc-12.0,msvc-14.0 22 | ADDRMD: 32,64 23 | - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017 24 | TOOLSET: msvc-14.1 25 | CXXSTD: 14,17,latest 26 | ADDRMD: 32,64 27 | 28 | install: 29 | - set BOOST_BRANCH=develop 30 | - if "%APPVEYOR_REPO_BRANCH%" == "master" set BOOST_BRANCH=master 31 | - cd .. 32 | - git clone -b %BOOST_BRANCH% --depth 1 https://github.com/boostorg/boost.git boost-root 33 | - cd boost-root 34 | - git submodule update --init tools/boostdep 35 | - xcopy /s /e /q %APPVEYOR_BUILD_FOLDER% libs\chrono\ 36 | - python tools/boostdep/depinst/depinst.py chrono 37 | - cmd /c bootstrap 38 | - b2 -d0 headers 39 | 40 | build: off 41 | 42 | test_script: 43 | - if not "%CXXSTD%" == "" set CXXSTD=cxxstd=%CXXSTD% 44 | - if not "%ADDRMD%" == "" set ADDRMD=address-model=%ADDRMD% 45 | - b2 -j3 libs/chrono/test toolset=%TOOLSET% %CXXSTD% %ADDRMD% variant=debug,release 46 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Generated by `boostdep --cmake chrono` 2 | # Copyright 2020, 2021 Peter Dimov 3 | # Distributed under the Boost Software License, Version 1.0. 4 | # https://www.boost.org/LICENSE_1_0.txt 5 | 6 | cmake_minimum_required(VERSION 3.8...3.20) 7 | 8 | project(boost_chrono VERSION "${BOOST_SUPERPROJECT_VERSION}" LANGUAGES CXX) 9 | 10 | add_library(boost_chrono 11 | src/chrono.cpp 12 | src/process_cpu_clocks.cpp 13 | src/thread_clock.cpp 14 | ) 15 | 16 | add_library(Boost::chrono ALIAS boost_chrono) 17 | 18 | target_include_directories(boost_chrono PUBLIC include) 19 | 20 | target_link_libraries(boost_chrono 21 | PUBLIC 22 | Boost::assert 23 | Boost::config 24 | Boost::core 25 | Boost::integer 26 | Boost::move 27 | Boost::mpl 28 | Boost::predef 29 | Boost::ratio 30 | Boost::static_assert 31 | Boost::system 32 | Boost::throw_exception 33 | Boost::type_traits 34 | Boost::typeof 35 | Boost::utility 36 | Boost::winapi 37 | ) 38 | 39 | target_compile_features(boost_chrono PUBLIC cxx_std_11) 40 | 41 | target_compile_definitions(boost_chrono 42 | PUBLIC BOOST_CHRONO_NO_LIB 43 | PRIVATE BOOST_CHRONO_SOURCE 44 | ) 45 | 46 | if(BUILD_SHARED_LIBS) 47 | target_compile_definitions(boost_chrono PUBLIC BOOST_CHRONO_DYN_LINK) 48 | else() 49 | target_compile_definitions(boost_chrono PUBLIC BOOST_CHRONO_STATIC_LINK) 50 | endif() 51 | 52 | if(BUILD_TESTING AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/test/CMakeLists.txt") 53 | 54 | add_subdirectory(test) 55 | 56 | endif() 57 | -------------------------------------------------------------------------------- /include/boost/chrono/io/utility/to_string.hpp: -------------------------------------------------------------------------------- 1 | // boost/chrono/utility/to_string.hpp 2 | // 3 | // Copyright 2011 Vicente J. Botet Escriba 4 | // Use, modification and distribution are subject to the Boost Software License, 5 | // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at 6 | // http://www.boost.org/LICENSE_1_0.txt). 7 | 8 | #ifndef BOOST_CHRONO_UTILITY_TO_STRING_HPP 9 | #define BOOST_CHRONO_UTILITY_TO_STRING_HPP 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | namespace boost 16 | { 17 | namespace chrono 18 | { 19 | template 20 | std::basic_string to_basic_string(T const&v) { 21 | std::basic_stringstream sstr; 22 | sstr << v; 23 | return sstr.str(); 24 | } 25 | 26 | template 27 | std::string to_string(T const&v) { 28 | return to_basic_string(v); 29 | } 30 | #ifndef BOOST_NO_STD_WSTRING 31 | template 32 | std::wstring to_wstring(T const&v) { 33 | return to_basic_string(v); 34 | } 35 | #endif 36 | #if BOOST_CHRONO_HAS_UNICODE_SUPPORT 37 | template 38 | std::basic_string to_u16string(T const&v) { 39 | return to_basic_string(v); 40 | } 41 | template 42 | std::basic_string to_u32string(T const&v) { 43 | return to_basic_string(v); 44 | } 45 | #endif 46 | } // chrono 47 | 48 | } // boost 49 | 50 | #endif // header 51 | -------------------------------------------------------------------------------- /include/boost/chrono/detail/no_warning/signed_unsigned_cmp.hpp: -------------------------------------------------------------------------------- 1 | // is_evenly_divisible_by.hpp --------------------------------------------------------------// 2 | 3 | // Copyright 2009-2010 Vicente J. Botet Escriba 4 | 5 | // Distributed under the Boost Software License, Version 1.0. 6 | // See http://www.boost.org/LICENSE_1_0.txt 7 | 8 | #ifndef BOOST_CHRONO_DETAIL_NO_WARNING_SIGNED_UNSIGNED_CMP_HPP 9 | #define BOOST_CHRONO_DETAIL_NO_WARNING_SIGNED_UNSIGNED_CMP_HPP 10 | 11 | // 12 | // We simply cannot include this header on gcc without getting copious warnings of the kind: 13 | // 14 | //../../../boost/chrono/detail/no_warning/signed_unsigned_cmp.hpp:37: warning: comparison between signed and unsigned integer expressions 15 | // 16 | // And yet there is no other reasonable implementation, so we declare this a system header 17 | // to suppress these warnings. 18 | // 19 | 20 | #if defined(__GNUC__) && (__GNUC__ >= 4) 21 | #pragma GCC system_header 22 | #elif defined __SUNPRO_CC 23 | #pragma disable_warn 24 | #elif defined _MSC_VER 25 | #pragma warning(push, 1) 26 | #endif 27 | 28 | namespace boost { 29 | namespace chrono { 30 | namespace detail { 31 | 32 | template 33 | bool lt(T t, U u) 34 | { 35 | return t < u; 36 | } 37 | 38 | template 39 | bool gt(T t, U u) 40 | { 41 | return t > u; 42 | } 43 | 44 | } // namespace detail 45 | } // namespace detail 46 | } // namespace chrono 47 | 48 | #if defined __SUNPRO_CC 49 | #pragma enable_warn 50 | #elif defined _MSC_VER 51 | #pragma warning(pop) 52 | #endif 53 | 54 | #endif // BOOST_CHRONO_DETAIL_NO_WARNING_SIGNED_UNSIGNED_CMP_HPP 55 | -------------------------------------------------------------------------------- /include/boost/chrono/round.hpp: -------------------------------------------------------------------------------- 1 | // boost/chrono/round.hpp ------------------------------------------------------------// 2 | 3 | // (C) Copyright Howard Hinnant 4 | // Copyright 2011 Vicente J. Botet Escriba 5 | 6 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 7 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 8 | 9 | // See http://www.boost.org/libs/chrono for documentation. 10 | 11 | #ifndef BOOST_CHRONO_ROUND_HPP 12 | #define BOOST_CHRONO_ROUND_HPP 13 | 14 | #include 15 | #include 16 | //#include 17 | 18 | namespace boost 19 | { 20 | namespace chrono 21 | { 22 | 23 | /** 24 | * rounds to nearest, to even on tie 25 | */ 26 | template 27 | To round(const duration& d) 28 | { 29 | typedef typename common_type >::type result_type; 30 | result_type diff0; 31 | result_type diff1; 32 | 33 | To t0 = duration_cast(d); 34 | To t1 = t0; 35 | if (t0>d) { 36 | --t1; 37 | diff0 = t0 - d; 38 | diff1 = d - t1; 39 | } else { 40 | ++t1; 41 | diff0 = d - t0; 42 | diff1 = t1 - d; 43 | } 44 | 45 | if (diff0 == diff1) 46 | { 47 | if (t0.count() & 1) 48 | return t1; 49 | return t0; 50 | } 51 | else if (diff0 < diff1) 52 | return t0; 53 | return t1; 54 | } 55 | 56 | } // namespace chrono 57 | } // namespace boost 58 | 59 | #endif 60 | -------------------------------------------------------------------------------- /test/traits/duration_values_pass.cpp: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // The LLVM Compiler Infrastructure 4 | // 5 | // This file is dual licensed under the MIT and the University of Illinois Open 6 | // Source Licenses. See LICENSE.TXT for details. 7 | // 8 | //===----------------------------------------------------------------------===// 9 | // Adaptation to Boost of the libcxx 10 | // Copyright 2010 Vicente J. Botet Escriba 11 | // Distributed under the Boost Software License, Version 1.0. 12 | // See http://www.boost.org/LICENSE_1_0.txt 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | #include "../rep.h" 20 | 21 | int main() 22 | { 23 | BOOST_TEST((boost::chrono::duration_values::min)() == 24 | (std::numeric_limits::min)()); 25 | BOOST_TEST((boost::chrono::duration_values::min)() == 26 | -(std::numeric_limits::max)()); 27 | BOOST_TEST((boost::chrono::duration_values::min)() == 28 | (std::numeric_limits::min)()); 29 | 30 | BOOST_TEST((boost::chrono::duration_values::max)() == 31 | (std::numeric_limits::max)()); 32 | BOOST_TEST((boost::chrono::duration_values::max)() == 33 | (std::numeric_limits::max)()); 34 | BOOST_TEST((boost::chrono::duration_values::max)() == 35 | (std::numeric_limits::max)()); 36 | 37 | BOOST_TEST(boost::chrono::duration_values::zero() == 0); 38 | BOOST_TEST(boost::chrono::duration_values::zero() == 0); 39 | 40 | return boost::report_errors(); 41 | } 42 | -------------------------------------------------------------------------------- /test/duration/typedefs_pass.cpp: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // The LLVM Compiler Infrastructure 4 | // 5 | // This file is dual licensed under the MIT and the University of Illinois Open 6 | // Source Licenses. See LICENSE.TXT for details. 7 | // 8 | //===----------------------------------------------------------------------===// 9 | // Adaptation to Boost of the libcxx 10 | // Copyright 2010 Vicente J. Botet Escriba 11 | // Distributed under the Boost Software License, Version 1.0. 12 | // See http://www.boost.org/LICENSE_1_0.txt 13 | 14 | 15 | #include 16 | #include 17 | #include 18 | 19 | #if !defined(BOOST_NO_CXX11_STATIC_ASSERT) 20 | #define NOTHING "" 21 | #endif 22 | 23 | template 24 | void check_duration() 25 | { 26 | typedef typename D::rep Rep; 27 | typedef typename D::period Period; 28 | BOOST_CHRONO_STATIC_ASSERT(boost::is_signed::value, NOTHING, ()); 29 | BOOST_CHRONO_STATIC_ASSERT(boost::is_integral::value, NOTHING, ()); 30 | BOOST_CHRONO_STATIC_ASSERT(std::numeric_limits::digits >= ExpectedDigits, NOTHING, ()); 31 | BOOST_CHRONO_STATIC_ASSERT((boost::is_same::value), NOTHING, ()); 32 | } 33 | 34 | void test() 35 | { 36 | check_duration >(); 37 | check_duration >(); 38 | check_duration >(); 39 | check_duration(); 40 | check_duration(); 41 | check_duration(); 42 | } 43 | -------------------------------------------------------------------------------- /test/traits/common_type_duration_pass.cpp: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // The LLVM Compiler Infrastructure 4 | // 5 | // This file is dual licensed under the MIT and the University of Illinois Open 6 | // Source Licenses. See LICENSE.TXT for details. 7 | // 8 | //===----------------------------------------------------------------------===// 9 | // Adaptation to Boost of the libcxx 10 | // Copyright 2010 Vicente J. Botet Escriba 11 | // Distributed under the Boost Software License, Version 1.0. 12 | // See http://www.boost.org/LICENSE_1_0.txt 13 | 14 | #include 15 | #include 16 | 17 | #if !defined(BOOST_NO_CXX11_STATIC_ASSERT) 18 | #define NOTHING "" 19 | #endif 20 | 21 | template 22 | void 23 | test() 24 | { 25 | typedef typename boost::common_type::type Dc; 26 | BOOST_CHRONO_STATIC_ASSERT((boost::is_same::value), NOTHING, (D1, D2, Dc, De)); 27 | } 28 | 29 | void testall() 30 | { 31 | test >, 32 | boost::chrono::duration >, 33 | boost::chrono::duration > >(); 34 | test >, 35 | boost::chrono::duration >, 36 | boost::chrono::duration > >(); 37 | test >, 38 | boost::chrono::duration >, 39 | boost::chrono::duration > >(); 40 | test >, 41 | boost::chrono::duration >, 42 | boost::chrono::duration > >(); 43 | } 44 | -------------------------------------------------------------------------------- /example/rounding.cpp: -------------------------------------------------------------------------------- 1 | // french.cpp ----------------------------------------------------------// 2 | 3 | // Copyright 2010 Howard Hinnant 4 | // Copyright 2011 Vicente J. Botet Escriba 5 | // Distributed under the Boost Software License, Version 1.0. 6 | // See http://www.boost.org/LICENSE_1_0.txt 7 | 8 | // Adapted to Boost from the original Hawards's code 9 | 10 | #include 11 | //#include 12 | #include 13 | #include 14 | #include 15 | 16 | int main() 17 | { 18 | boost::chrono::milliseconds ms(2500); 19 | std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl; 20 | std::cout << boost::chrono::floor(ms).count() 21 | << " seconds\n"; 22 | std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl; 23 | std::cout << boost::chrono::round(ms).count() 24 | << " seconds\n"; 25 | std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl; 26 | std::cout << boost::chrono::ceil(ms).count() 27 | << " seconds\n"; 28 | std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl; 29 | ms = boost::chrono::milliseconds(2516); 30 | std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl; 31 | typedef boost::chrono::duration > frame_rate; 32 | std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl; 33 | std::cout << boost::chrono::floor(ms).count() 34 | << " [1/30] seconds\n"; 35 | std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl; 36 | std::cout << boost::chrono::round(ms).count() 37 | << " [1/30] seconds\n"; 38 | std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl; 39 | std::cout << boost::chrono::ceil(ms).count() 40 | << " [1/30] seconds\n"; 41 | std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl; 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /test/rep.h: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // The LLVM Compiler Infrastructure 4 | // 5 | // This file is dual licensed under the MIT and the University of Illinois Open 6 | // Source Licenses. See LICENSE.TXT for details. 7 | // 8 | //===----------------------------------------------------------------------===// 9 | // Adaptation to Boost of the libcxx 10 | // Copyright 2010 Vicente J. Botet Escriba 11 | // Distributed under the Boost Software License, Version 1.0. 12 | // See http://www.boost.org/LICENSE_1_0.txt 13 | 14 | #ifndef REP_H 15 | #define REP_H 16 | 17 | #include 18 | 19 | class Rep 20 | { 21 | public: 22 | int data_; 23 | BOOST_CONSTEXPR Rep() : data_() {} 24 | explicit BOOST_CONSTEXPR Rep(int i) : data_(i) {} 25 | 26 | BOOST_CONSTEXPR bool operator==(int i) const {return data_ == i;} 27 | BOOST_CONSTEXPR bool operator==(const Rep& r) const {return data_ == r.data_;} 28 | 29 | Rep& operator*=(Rep x) {data_ *= x.data_; return *this;} 30 | Rep& operator/=(Rep x) {data_ /= x.data_; return *this;} 31 | }; 32 | 33 | #if 0 34 | namespace std { 35 | 36 | template <> 37 | struct numeric_limits 38 | { 39 | static BOOST_CONSTEXPR Rep max BOOST_PREVENT_MACRO_SUBSTITUTION () 40 | { 41 | return Rep((std::numeric_limits::max)()); 42 | } 43 | 44 | }; 45 | } // namespace std 46 | 47 | namespace boost { 48 | namespace chrono { 49 | template <> 50 | struct duration_values 51 | { 52 | static BOOST_CONSTEXPR Rep zero() {return Rep(0);} 53 | static BOOST_CONSTEXPR Rep max BOOST_PREVENT_MACRO_SUBSTITUTION () 54 | { 55 | return Rep((std::numeric_limits::max)()); 56 | } 57 | 58 | static BOOST_CONSTEXPR Rep min BOOST_PREVENT_MACRO_SUBSTITUTION () 59 | { 60 | return Rep(detail::numeric_limits::lowest()); 61 | } 62 | }; 63 | 64 | } // namespace chrono 65 | } // namespace boost 66 | #endif 67 | #endif // REP_H 68 | -------------------------------------------------------------------------------- /example/manipulate_clock_object.cpp: -------------------------------------------------------------------------------- 1 | // manipulate_clock_object.cpp ----------------------------------------------------------// 2 | 3 | // Copyright 2008 Howard Hinnant 4 | // Copyright 2008 Beman Dawes 5 | // Copyright 2009 Vicente J. Botet Escriba 6 | 7 | // Distributed under the Boost Software License, Version 1.0. 8 | // See http://www.boost.org/LICENSE_1_0.txt 9 | 10 | /* 11 | This code was extracted by Vicente J. Botet Escriba from Beman Dawes time2_demo.cpp which 12 | was derived by Beman Dawes from Howard Hinnant's time2_demo prototype. 13 | Many thanks to Howard for making his code available under the Boost license. 14 | The original code was modified to conform to Boost conventions and to section 15 | 20.9 Time utilities [time] of the C++ committee's working paper N2798. 16 | See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf. 17 | 18 | time2_demo contained this comment: 19 | 20 | Much thanks to Andrei Alexandrescu, 21 | Walter Brown, 22 | Peter Dimov, 23 | Jeff Garland, 24 | Terry Golubiewski, 25 | Daniel Krugler, 26 | Anthony Williams. 27 | */ 28 | 29 | #include 30 | #include 31 | 32 | #include 33 | 34 | using namespace boost::chrono; 35 | 36 | #if defined _MSC_VER 37 | #pragma warning(push) 38 | #pragma warning(disable:4100) 39 | #endif 40 | void manipulate_clock_object(system_clock clock) 41 | #if defined _MSC_VER 42 | #pragma warning(pop) 43 | #endif 44 | { 45 | system_clock::duration delay = milliseconds(5); 46 | system_clock::time_point start = clock.now(); 47 | 48 | while ((clock.now() - start) <= delay) {} 49 | 50 | system_clock::time_point stop = clock.now(); 51 | system_clock::duration elapsed = stop - start; 52 | std::cout << "paused " << nanoseconds(elapsed).count() << " nanoseconds\n"; 53 | } 54 | 55 | 56 | int main() 57 | { 58 | manipulate_clock_object(system_clock()); 59 | return 0; 60 | } 61 | 62 | -------------------------------------------------------------------------------- /test/traits/common_type_time_point_pass.cpp: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // The LLVM Compiler Infrastructure 4 | // 5 | // This file is dual licensed under the MIT and the University of Illinois Open 6 | // Source Licenses. See LICENSE.TXT for details. 7 | // 8 | //===----------------------------------------------------------------------===// 9 | // Adaptation to Boost of the libcxx 10 | // Copyright 2010 Vicente J. Botet Escriba 11 | // Distributed under the Boost Software License, Version 1.0. 12 | // See http://www.boost.org/LICENSE_1_0.txt 13 | 14 | #include 15 | #include 16 | 17 | #if !defined(BOOST_NO_CXX11_STATIC_ASSERT) 18 | #define NOTHING "" 19 | #endif 20 | 21 | template 22 | void 23 | test() 24 | { 25 | typedef boost::chrono::system_clock C; 26 | typedef boost::chrono::time_point T1; 27 | typedef boost::chrono::time_point T2; 28 | typedef boost::chrono::time_point Te; 29 | typedef typename boost::common_type::type Tc; 30 | BOOST_CHRONO_STATIC_ASSERT((boost::is_same::value), NOTHING, (T1, T2, Tc, Te)); 31 | } 32 | 33 | void testall() 34 | { 35 | test >, 36 | boost::chrono::duration >, 37 | boost::chrono::duration > >(); 38 | test >, 39 | boost::chrono::duration >, 40 | boost::chrono::duration > >(); 41 | test >, 42 | boost::chrono::duration >, 43 | boost::chrono::duration > >(); 44 | test >, 45 | boost::chrono::duration >, 46 | boost::chrono::duration > >(); 47 | } 48 | -------------------------------------------------------------------------------- /example/test_special_values.cpp: -------------------------------------------------------------------------------- 1 | // test_special_values.cpp ----------------------------------------------------------// 2 | 3 | // Copyright 2008 Howard Hinnant 4 | // Copyright 2008 Beman Dawes 5 | // Copyright 2009 Vicente J. Botet Escriba 6 | 7 | // Distributed under the Boost Software License, Version 1.0. 8 | // See http://www.boost.org/LICENSE_1_0.txt 9 | 10 | /* 11 | This code was extracted by Vicente J. Botet Escriba from Beman Dawes time2_demo.cpp which 12 | was derived by Beman Dawes from Howard Hinnant's time2_demo prototype. 13 | Many thanks to Howard for making his code available under the Boost license. 14 | The original code was modified to conform to Boost conventions and to section 15 | 20.9 Time utilities [time] of the C++ committee's working paper N2798. 16 | See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf. 17 | 18 | time2_demo contained this comment: 19 | 20 | Much thanks to Andrei Alexandrescu, 21 | Walter Brown, 22 | Peter Dimov, 23 | Jeff Garland, 24 | Terry Golubiewski, 25 | Daniel Krugler, 26 | Anthony Williams. 27 | */ 28 | 29 | #include 30 | #include 31 | 32 | #include 33 | 34 | using namespace boost::chrono; 35 | 36 | void test_special_values() 37 | { 38 | std::cout << "duration::min().count() = " << ((duration::min)()).count() << '\n'; 39 | std::cout << "duration::zero().count() = " << duration::zero().count() << '\n'; 40 | std::cout << "duration::max().count() = " << ((duration::max)()).count() << '\n'; 41 | std::cout << "duration::min().count() = " << ((duration::min)()).count() << '\n'; 42 | std::cout << "duration::zero().count() = " << duration::zero().count() << '\n'; 43 | std::cout << "duration::max().count() = " << ((duration::max)()).count() << '\n'; 44 | } 45 | 46 | int main() 47 | { 48 | test_special_values(); 49 | return 0; 50 | } 51 | 52 | -------------------------------------------------------------------------------- /include/boost/chrono/detail/inlined/process_cpu_clocks.hpp: -------------------------------------------------------------------------------- 1 | // boost process_cpu_clocks.cpp -----------------------------------------------------------// 2 | 3 | // Copyright 2009-2010 Vicente J. Botet Escriba 4 | 5 | // Distributed under the Boost Software License, Version 1.0. 6 | // See http://www.boost.org/LICENSE_1_0.txt 7 | 8 | // See http://www.boost.org/libs/chrono for documentation. 9 | 10 | //--------------------------------------------------------------------------------------// 11 | #ifndef BOOST_CHRONO_DETAIL_INLINED_PROCESS_CPU_CLOCKS_HPP 12 | #define BOOST_CHRONO_DETAIL_INLINED_PROCESS_CPU_CLOCKS_HPP 13 | 14 | 15 | #include 16 | #if defined(BOOST_CHRONO_HAS_PROCESS_CLOCKS) 17 | 18 | #include 19 | #include 20 | #include 21 | #if defined BOOST_CHRONO_PROVIDE_HYBRID_ERROR_HANDLING 22 | #include 23 | #endif 24 | //----------------------------------------------------------------------------// 25 | // Windows // 26 | //----------------------------------------------------------------------------// 27 | #if defined(BOOST_CHRONO_WINDOWS_API) 28 | #include 29 | 30 | //----------------------------------------------------------------------------// 31 | // Mac // 32 | //----------------------------------------------------------------------------// 33 | #elif defined(BOOST_CHRONO_MAC_API) 34 | #include 35 | 36 | //----------------------------------------------------------------------------// 37 | // POSIX // 38 | //----------------------------------------------------------------------------// 39 | #elif defined(BOOST_CHRONO_POSIX_API) 40 | #include 41 | 42 | #endif // POSIX 43 | 44 | #endif 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /example/explore_limits.cpp: -------------------------------------------------------------------------------- 1 | // explore_limits.cpp ----------------------------------------------------------// 2 | 3 | // Copyright 2008 Howard Hinnant 4 | // Copyright 2008 Beman Dawes 5 | // Copyright 2009 Vicente J. Botet Escriba 6 | 7 | // Distributed under the Boost Software License, Version 1.0. 8 | // See http://www.boost.org/LICENSE_1_0.txt 9 | 10 | /* 11 | This code was extracted by Vicente J. Botet Escriba from Beman Dawes time2_demo.cpp which 12 | was derived by Beman Dawes from Howard Hinnant's time2_demo prototype. 13 | Many thanks to Howard for making his code available under the Boost license. 14 | The original code was modified to conform to Boost conventions and to section 15 | 20.9 Time utilities [time] of the C++ committee's working paper N2798. 16 | See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf. 17 | 18 | time2_demo contained this comment: 19 | 20 | Much thanks to Andrei Alexandrescu, 21 | Walter Brown, 22 | Peter Dimov, 23 | Jeff Garland, 24 | Terry Golubiewski, 25 | Daniel Krugler, 26 | Anthony Williams. 27 | */ 28 | 29 | #include 30 | #include 31 | 32 | #include 33 | 34 | using namespace boost::chrono; 35 | 36 | 37 | void explore_limits() 38 | { 39 | typedef duration, 40 | hours::period>::type> Years; 41 | #ifdef BOOST_CHRONO_HAS_CLOCK_STEADY 42 | steady_clock::time_point t1( Years(250)); 43 | steady_clock::time_point t2(-Years(250)); 44 | #else 45 | system_clock::time_point t1( Years(250)); 46 | system_clock::time_point t2(-Years(250)); 47 | #endif 48 | // nanosecond resolution is likely to overflow. "up cast" to microseconds. 49 | // The "up cast" trades precision for range. 50 | microseconds d = time_point_cast(t1) - time_point_cast(t2); 51 | std::cout << d.count() << " microseconds\n"; 52 | } 53 | 54 | 55 | int main() 56 | { 57 | explore_limits(); 58 | return 0; 59 | } 60 | 61 | -------------------------------------------------------------------------------- /include/boost/chrono/detail/inlined/thread_clock.hpp: -------------------------------------------------------------------------------- 1 | // boost thread_clock.cpp -----------------------------------------------------------// 2 | 3 | // Copyright 2010 Vicente J. Botet Escriba 4 | 5 | // Distributed under the Boost Software License, Version 1.0. 6 | // See http://www.boost.org/LICENSE_1_0.txt 7 | 8 | // See http://www.boost.org/libs/chrono for documentation. 9 | 10 | //--------------------------------------------------------------------------------------// 11 | #ifndef BOOST_CHRONO_DETAIL_INLINED_THREAD_CLOCK_HPP 12 | #define BOOST_CHRONO_DETAIL_INLINED_THREAD_CLOCK_HPP 13 | 14 | #include 15 | #include 16 | #if defined(BOOST_CHRONO_HAS_THREAD_CLOCK) 17 | #include 18 | #include 19 | #if defined BOOST_CHRONO_PROVIDE_HYBRID_ERROR_HANDLING 20 | #include 21 | #endif 22 | #include 23 | #include 24 | 25 | //----------------------------------------------------------------------------// 26 | // Windows // 27 | //----------------------------------------------------------------------------// 28 | #if defined(BOOST_CHRONO_WINDOWS_API) 29 | #include 30 | 31 | //----------------------------------------------------------------------------// 32 | // Mac // 33 | //----------------------------------------------------------------------------// 34 | #elif defined(BOOST_CHRONO_MAC_API) 35 | #include 36 | 37 | //----------------------------------------------------------------------------// 38 | // POSIX // 39 | //----------------------------------------------------------------------------// 40 | #elif defined(BOOST_CHRONO_POSIX_API) 41 | #include 42 | 43 | #endif // POSIX 44 | 45 | #endif 46 | #endif 47 | -------------------------------------------------------------------------------- /example/timer.hpp: -------------------------------------------------------------------------------- 1 | // boost/chrono/timer.hpp ------------------------------------------------------------// 2 | 3 | // Copyright Beman Dawes 2008 4 | // Copyright 2009 Vicente J. Botet Escriba 5 | 6 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 7 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 8 | 9 | // See http://www.boost.org/libs/system for documentation. 10 | 11 | #ifndef BOOSTEX_CHRONO_TIMER_HPP 12 | #define BOOSTEX_CHRONO_TIMER_HPP 13 | 14 | #include 15 | #include 16 | 17 | namespace boost_ex 18 | { 19 | namespace chrono 20 | { 21 | 22 | //--------------------------------------------------------------------------------------// 23 | // timer // 24 | //--------------------------------------------------------------------------------------// 25 | 26 | template 27 | class timer 28 | { 29 | public: 30 | typedef Clock clock; 31 | typedef typename Clock::duration duration; 32 | typedef typename Clock::time_point time_point; 33 | 34 | explicit timer( boost::system::error_code & ec = ::boost::throws() ) 35 | { 36 | start(ec); 37 | } 38 | 39 | ~timer() {} // never throws 40 | 41 | void start( boost::system::error_code & ec = ::boost::throws() ) 42 | { 43 | m_start = clock::now( ec ); 44 | } 45 | 46 | duration elapsed( boost::system::error_code & ec = ::boost::throws() ) 47 | { return clock::now( ec ) - m_start; } 48 | 49 | private: 50 | time_point m_start; 51 | }; 52 | 53 | typedef chrono::timer< boost::chrono::system_clock > system_timer; 54 | #ifdef BOOST_CHRONO_HAS_CLOCK_STEADY 55 | typedef chrono::timer< boost::chrono::steady_clock > steady_timer; 56 | #endif 57 | typedef chrono::timer< boost::chrono::high_resolution_clock > high_resolution_timer; 58 | 59 | } // namespace chrono 60 | } // namespace boost_ex 61 | 62 | #endif 63 | -------------------------------------------------------------------------------- /test/test_10631.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2015 Vicente J. Botet Escriba 2 | // Distributed under the Boost Software License, Version 1.0. 3 | // See http://www.boost.org/LICENSE_1_0.txt 4 | // See http://www.boost.org/libs/chrono for documentation. 5 | 6 | #define BOOST_CHRONO_VERION 2 7 | #include 8 | #include 9 | #include 10 | 11 | // a custom clock 12 | // here based on boost's own system_clock 13 | class MyMillenniumClock 14 | { 15 | public: 16 | typedef boost::chrono::system_clock::rep rep; 17 | typedef boost::chrono::system_clock::period period; 18 | typedef boost::chrono::duration duration; 19 | typedef boost::chrono::time_point time_point; 20 | 21 | BOOST_STATIC_CONSTEXPR bool is_steady = boost::chrono::system_clock::is_steady; 22 | 23 | public: 24 | /// Returns a time_point representing the current value of the clock. 25 | static time_point now() { 26 | return time_point(boost::chrono::system_clock::now().time_since_epoch() - boost::chrono::hours(30*365)); 27 | } 28 | }; 29 | 30 | 31 | namespace boost 32 | { 33 | namespace chrono 34 | { 35 | template 36 | struct clock_string 37 | { 38 | static std::basic_string name() { 39 | static const CharT a[] = {'M', 'y', 'M', 'i', 'l', 'l', 'e', 'n', 'n', 'i', 'u', 'm', 'C', 'l', 'o', 'c', 'k'}; 40 | return std::basic_string(a, a + sizeof(a)/sizeof(a[0])); 41 | } 42 | static std::basic_string since() { 43 | static const CharT a[] = {' ', 's', 'i', 'n', 'c', 'e', ' ', 'y', 'e', 'a', 'r', ' ', '2', 'k'}; 44 | return std::basic_string(a, a + sizeof(a)/sizeof(a[0])); 45 | } 46 | }; 47 | } 48 | } 49 | 50 | template 51 | std::basic_string get_epoch_custom(MyMillenniumClock, TPUFacet&) 52 | { 53 | return boost::chrono::clock_string::since(); 54 | } 55 | 56 | int main() 57 | { 58 | std::cout << boost::chrono::steady_clock::now() << std::endl; 59 | std::cout << MyMillenniumClock::now() << std::endl; 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /include/boost/chrono/detail/inlined/chrono.hpp: -------------------------------------------------------------------------------- 1 | // chrono.cpp --------------------------------------------------------------// 2 | 3 | // Copyright Beman Dawes 2008 4 | // Copyright Vicente J. Botet Escriba 2009 5 | 6 | // Distributed under the Boost Software License, Version 1.0. 7 | // See http://www.boost.org/LICENSE_1_0.txt 8 | 9 | #ifndef BOOST_CHRONO_DETAIL_INLINED_CHRONO_HPP 10 | #define BOOST_CHRONO_DETAIL_INLINED_CHRONO_HPP 11 | 12 | #include 13 | #include 14 | #if defined BOOST_CHRONO_PROVIDE_HYBRID_ERROR_HANDLING 15 | #include 16 | #endif 17 | #include 18 | #include 19 | 20 | //----------------------------------------------------------------------------// 21 | // // 22 | // Platform-specific Implementations // 23 | // // 24 | //----------------------------------------------------------------------------// 25 | 26 | //----------------------------------------------------------------------------// 27 | // Windows // 28 | //----------------------------------------------------------------------------// 29 | #if defined(BOOST_CHRONO_WINDOWS_API) 30 | #include 31 | 32 | //----------------------------------------------------------------------------// 33 | // Mac // 34 | //----------------------------------------------------------------------------// 35 | #elif defined(BOOST_CHRONO_MAC_API) 36 | #include 37 | 38 | //----------------------------------------------------------------------------// 39 | // POSIX // 40 | //----------------------------------------------------------------------------// 41 | #elif defined(BOOST_CHRONO_POSIX_API) 42 | #include 43 | 44 | #endif // POSIX 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /test/clock/errored_clock.hpp: -------------------------------------------------------------------------------- 1 | // errored_clock.hpp --------------------------------------------------------------// 2 | 3 | // Copyright 2010 Vicente J. Botet Escriba 4 | 5 | // Distributed under the Boost Software License, Version 1.0. 6 | // See http://www.boost.org/LICENSE_1_0.txt 7 | 8 | #ifndef BOOST_CHRONO_ERRORED_CLOCKS_HPP 9 | #define BOOST_CHRONO_ERRORED_CLOCKS_HPP 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | class errored_clock 20 | { 21 | public: 22 | typedef boost::chrono::nanoseconds duration; 23 | typedef duration::rep rep; 24 | typedef duration::period period; 25 | typedef boost::chrono::time_point time_point; 26 | static const bool is_steady = true; 27 | static int errno_; 28 | 29 | static void set_errno(int err) { 30 | errno_=err; 31 | } 32 | 33 | // throws on error 34 | static time_point now() { 35 | boost::throw_exception( 36 | boost::system::system_error( 37 | errno_, 38 | ::boost::system::system_category(), 39 | "errored_clock" 40 | ) 41 | ); 42 | return time_point(); 43 | } 44 | // never throws and set ec 45 | static time_point now(boost::system::error_code & ec) { 46 | if (::boost::chrono::is_throws(ec)) 47 | { 48 | boost::throw_exception( 49 | boost::system::system_error( 50 | errno_, 51 | ::boost::system::system_category(), 52 | "errored_clock" 53 | ) 54 | ); 55 | } 56 | ec.assign( errno_, ::boost::system::system_category() ); 57 | return time_point(); 58 | }; 59 | }; 60 | int errored_clock::errno_; 61 | 62 | #endif 63 | -------------------------------------------------------------------------------- /example/clock_name.hpp: -------------------------------------------------------------------------------- 1 | // stopclock_perf.cpp ---------------------------------------------------// 2 | 3 | // Copyright 2009 Vicente J. Botet Escriba 4 | // Copyright 2009 Howard Hinnant 5 | 6 | // Distributed under the Boost Software License, Version 1.0. 7 | // See http://www.boost.org/LICENSE_1_0.txt 8 | 9 | // See http://www.boost.org/libs/chrono for documentation. 10 | 11 | #ifndef BOOST_CHRONO_CLOCK_NAME_HPP 12 | #define BOOST_CHRONO_CLOCK_NAME_HPP 13 | 14 | #include 15 | #include 16 | 17 | template ::value, 19 | #ifdef BOOST_CHRONO_HAS_CLOCK_STEADY 20 | bool = boost::is_same::value, 21 | #else 22 | bool = false, 23 | #endif 24 | bool = boost::is_same::value 25 | > 26 | struct name; 27 | 28 | template 29 | struct name { 30 | static const char* apply() { return "unknown clock";} 31 | }; 32 | 33 | template 34 | struct name { 35 | static const char* apply() { return "system_clock";} 36 | }; 37 | 38 | template 39 | struct name { 40 | static const char* apply() { return "steady_clock";} 41 | }; 42 | 43 | template 44 | struct name { 45 | static const char* apply() { return "high_resolution_clock";} 46 | }; 47 | 48 | template 49 | struct name { 50 | static const char* apply() { return "steady_clock and high_resolution_clock";} 51 | }; 52 | 53 | template 54 | struct name { 55 | static const char* apply() { return "system_clock and high_resolution_clock";} 56 | }; 57 | 58 | template 59 | struct name { 60 | static const char* apply() { return "system_clock and steady_clock";} 61 | }; 62 | 63 | template 64 | struct name { 65 | static const char* apply() { return "system_clock, steady_clock and high_resolution_clock";} 66 | }; 67 | 68 | #endif 69 | -------------------------------------------------------------------------------- /example/await_keystroke.cpp: -------------------------------------------------------------------------------- 1 | // await_keystroke.cpp -----------------------------------------------------// 2 | 3 | // Copyright Beman Dawes 2008 4 | 5 | // Distributed under the Boost Software License, Version 1.0. 6 | // See http://www.boost.org/LICENSE_1_0.txt 7 | 8 | #define _CRT_SECURE_NO_WARNINGS 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | using namespace boost::chrono; 15 | 16 | template< class Clock > 17 | class timer 18 | { 19 | typename Clock::time_point start; 20 | public: 21 | 22 | timer() : start( Clock::now() ) {} 23 | 24 | typename Clock::duration elapsed() const 25 | { 26 | return Clock::now() - start; 27 | } 28 | 29 | double seconds() const 30 | { 31 | return elapsed().count() * ((double)Clock::period::num/Clock::period::den); 32 | } 33 | }; 34 | 35 | int main() 36 | { 37 | timer t1; 38 | timer t2; 39 | timer t3; 40 | 41 | std::cout << "Strike any key: "; 42 | std::cin.get(); 43 | 44 | std::cout << std::fixed << std::setprecision(9); 45 | std::cout << "system_clock-----------: " 46 | << t1.seconds() << " seconds\n"; 47 | std::cout << "steady_clock--------: " 48 | << t2.seconds() << " seconds\n"; 49 | std::cout << "high_resolution_clock--: " 50 | << t3.seconds() << " seconds\n"; 51 | 52 | system_clock::time_point d4 = system_clock::now(); 53 | system_clock::time_point d5 = system_clock::now(); 54 | 55 | std::cout << "\nsystem_clock latency-----------: " << (d5 - d4).count() << std::endl; 56 | 57 | steady_clock::time_point d6 = steady_clock::now(); 58 | steady_clock::time_point d7 = steady_clock::now(); 59 | 60 | std::cout << "steady_clock latency--------: " << (d7 - d6).count() << std::endl; 61 | 62 | high_resolution_clock::time_point d8 = high_resolution_clock::now(); 63 | high_resolution_clock::time_point d9 = high_resolution_clock::now(); 64 | 65 | std::cout << "high_resolution_clock latency--: " << (d9 - d8).count() << std::endl; 66 | 67 | std::time_t now = system_clock::to_time_t(system_clock::now()); 68 | 69 | std::cout << "\nsystem_clock::now() reports UTC is " 70 | << std::asctime(std::gmtime(&now)) << "\n"; 71 | 72 | return 0; 73 | } 74 | -------------------------------------------------------------------------------- /include/boost/chrono/thread_clock.hpp: -------------------------------------------------------------------------------- 1 | // boost/chrono/thread_clock.hpp -----------------------------------------------------------// 2 | 3 | // Copyright 2009-2011 Vicente J. Botet Escriba 4 | 5 | // Distributed under the Boost Software License, Version 1.0. 6 | // See http://www.boost.org/LICENSE_1_0.txt 7 | 8 | // See http://www.boost.org/libs/system for documentation. 9 | 10 | #include 11 | 12 | #ifndef BOOST_CHRONO_THREAD_CLOCK_HPP 13 | #define BOOST_CHRONO_THREAD_CLOCK_HPP 14 | 15 | #if defined(BOOST_CHRONO_HAS_THREAD_CLOCK) 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #ifndef BOOST_CHRONO_HEADER_ONLY 24 | #include // must be the last #include 25 | #endif 26 | 27 | namespace boost { namespace chrono { 28 | 29 | class BOOST_CHRONO_DECL thread_clock { 30 | public: 31 | typedef nanoseconds duration; 32 | typedef duration::rep rep; 33 | typedef duration::period period; 34 | typedef chrono::time_point time_point; 35 | BOOST_STATIC_CONSTEXPR bool is_steady = BOOST_CHRONO_THREAD_CLOCK_IS_STEADY; 36 | 37 | static BOOST_CHRONO_INLINE time_point now( ) BOOST_NOEXCEPT; 38 | #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING 39 | static BOOST_CHRONO_INLINE time_point now( system::error_code & ec ); 40 | #endif 41 | }; 42 | 43 | template 44 | struct clock_string 45 | { 46 | static std::basic_string name() 47 | { 48 | static const CharT u[] = 49 | { 't', 'h', 'r', 'e', 'a', 'd', '_', 50 | 'c', 'l','o', 'c', 'k'}; 51 | static const std::basic_string str(u, u + sizeof(u)/sizeof(u[0])); 52 | return str; 53 | } 54 | static std::basic_string since() 55 | { 56 | const CharT u[] = 57 | { ' ', 's', 'i', 'n', 'c', 'e', ' ', 't', 'h', 'r', 'e', 'a', 'd', ' ', 's', 't', 'a', 'r', 't', '-', 'u', 'p'}; 58 | const std::basic_string str(u, u + sizeof(u)/sizeof(u[0])); 59 | return str; 60 | } 61 | }; 62 | 63 | } // namespace chrono 64 | } // namespace boost 65 | 66 | 67 | #ifndef BOOST_CHRONO_HEADER_ONLY 68 | #include // pops abi_prefix.hpp pragmas 69 | #else 70 | #include 71 | #endif 72 | 73 | #endif 74 | 75 | #endif // BOOST_CHRONO_THREAD_CLOCK_HPP 76 | -------------------------------------------------------------------------------- /test/time_point/arithmetic_ext_pass.cpp: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // The LLVM Compiler Infrastructure 4 | // 5 | // This file is dual licensed under the MIT and the University of Illinois Open 6 | // Source Licenses. See LICENSE.TXT for details. 7 | // 8 | //===----------------------------------------------------------------------===// 9 | // Adaptation to Boost of the libcxx 10 | // Copyright 2010 Vicente J. Botet Escriba 11 | // Distributed under the Boost Software License, Version 1.0. 12 | // See http://www.boost.org/LICENSE_1_0.txt 13 | 14 | #define BOOST_CHRONO_EXTENSIONS 15 | #include 16 | #include 17 | #ifdef BOOST_NO_CXX11_CONSTEXPR 18 | #define BOOST_CONSTEXPR_ASSERT(C) BOOST_TEST(C) 19 | #else 20 | #include 21 | #define BOOST_CONSTEXPR_ASSERT(C) BOOST_STATIC_ASSERT(C) 22 | #endif 23 | 24 | int main() 25 | { 26 | { 27 | typedef boost::chrono::system_clock Clock; 28 | typedef boost::chrono::milliseconds Duration; 29 | boost::chrono::time_point t(Duration(3)); 30 | t += 2; 31 | BOOST_TEST(t.time_since_epoch() == Duration(5)); 32 | } 33 | { 34 | typedef boost::chrono::system_clock Clock; 35 | typedef boost::chrono::milliseconds Duration; 36 | boost::chrono::time_point t(Duration(3)); 37 | t++; 38 | BOOST_TEST(t.time_since_epoch() == Duration(4)); 39 | } 40 | { 41 | typedef boost::chrono::system_clock Clock; 42 | typedef boost::chrono::milliseconds Duration; 43 | boost::chrono::time_point t(Duration(3)); 44 | ++t; 45 | BOOST_TEST(t.time_since_epoch() == Duration(4)); 46 | } 47 | { 48 | typedef boost::chrono::system_clock Clock; 49 | typedef boost::chrono::milliseconds Duration; 50 | boost::chrono::time_point t(Duration(3)); 51 | t -= 2; 52 | BOOST_TEST(t.time_since_epoch() == Duration(1)); 53 | } 54 | { 55 | typedef boost::chrono::system_clock Clock; 56 | typedef boost::chrono::milliseconds Duration; 57 | boost::chrono::time_point t(Duration(3)); 58 | t--; 59 | BOOST_TEST(t.time_since_epoch() == Duration(2)); 60 | } 61 | { 62 | typedef boost::chrono::system_clock Clock; 63 | typedef boost::chrono::milliseconds Duration; 64 | boost::chrono::time_point t(Duration(3)); 65 | --t; 66 | BOOST_TEST(t.time_since_epoch() == Duration(2)); 67 | } 68 | 69 | return boost::report_errors(); 70 | } 71 | -------------------------------------------------------------------------------- /test/duration/duration_cast_pass.cpp: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // The LLVM Compiler Infrastructure 4 | // 5 | // This file is dual licensed under the MIT and the University of Illinois Open 6 | // Source Licenses. See LICENSE.TXT for details. 7 | // 8 | //===----------------------------------------------------------------------===// 9 | // Adaptation to Boost of the libcxx 10 | // Copyright 2010 Vicente J. Botet Escriba 11 | // Distributed under the Boost Software License, Version 1.0. 12 | // See http://www.boost.org/LICENSE_1_0.txt 13 | 14 | #include 15 | #include 16 | #include 17 | #if !defined(BOOST_NO_CXX11_STATIC_ASSERT) 18 | #define NOTHING "" 19 | #endif 20 | 21 | #ifdef BOOST_NO_CXX11_CONSTEXPR 22 | #define BOOST_CONSTEXPR_ASSERT(C) BOOST_TEST(C) 23 | #else 24 | #include 25 | #define BOOST_CONSTEXPR_ASSERT(C) BOOST_STATIC_ASSERT(C) 26 | #endif 27 | 28 | 29 | template 30 | void 31 | test(const FromDuration& f, const ToDuration& d) 32 | { 33 | //~ #if defined(BOOST_NO_CXX11_DECLTYPE) 34 | //~ typedef BOOST_TYPEOF_TPL(boost::chrono::duration_cast(f)) R; 35 | //~ #else 36 | //~ typedef decltype(boost::chrono::duration_cast(f)) R; 37 | //~ #endif 38 | //~ BOOST_CHRONO_STATIC_ASSERT((boost::is_same::value), NOTHING, (R, ToDuration)); 39 | BOOST_TEST(boost::chrono::duration_cast(f) == d); 40 | } 41 | 42 | int main() 43 | { 44 | test(boost::chrono::milliseconds(7265000), boost::chrono::hours(2)); 45 | test(boost::chrono::milliseconds(7265000), boost::chrono::minutes(121)); 46 | test(boost::chrono::milliseconds(7265000), boost::chrono::seconds(7265)); 47 | test(boost::chrono::milliseconds(7265000), boost::chrono::milliseconds(7265000)); 48 | test(boost::chrono::milliseconds(7265000), boost::chrono::microseconds(7265000000LL)); 49 | test(boost::chrono::milliseconds(7265000), boost::chrono::nanoseconds(7265000000000LL)); 50 | test(boost::chrono::milliseconds(7265000), 51 | boost::chrono::duration >(7265./3600)); 52 | test(boost::chrono::duration >(9), 53 | boost::chrono::duration >(10)); 54 | { 55 | BOOST_CONSTEXPR boost::chrono::hours h = boost::chrono::duration_cast(boost::chrono::milliseconds(7265000)); 56 | BOOST_CONSTEXPR_ASSERT(h.count() == 2); 57 | } 58 | return boost::report_errors(); 59 | } 60 | -------------------------------------------------------------------------------- /test/duration/duration_values_pass.cpp: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // The LLVM Compiler Infrastructure 4 | // 5 | // This file is dual licensed under the MIT and the University of Illinois Open 6 | // Source Licenses. See LICENSE.TXT for details. 7 | // 8 | //===----------------------------------------------------------------------===// 9 | // Adaptation to Boost of the libcxx 10 | // Copyright 2010 Vicente J. Botet Escriba 11 | // Distributed under the Boost Software License, Version 1.0. 12 | // See http://www.boost.org/LICENSE_1_0.txt 13 | 14 | #include 15 | #include 16 | 17 | #include "../rep.h" 18 | #if defined BOOST_NO_CXX11_NUMERIC_LIMITS || defined BOOST_NO_CXX11_CONSTEXPR 19 | #define BOOST_CONSTEXPR_ASSERT(C) BOOST_TEST(C) 20 | #else 21 | #include 22 | #define BOOST_CONSTEXPR_ASSERT(C) BOOST_STATIC_ASSERT(C) 23 | #endif 24 | 25 | template 26 | void check_max() 27 | { 28 | typedef typename D::rep Rep; 29 | Rep max_rep = (boost::chrono::duration_values::max)(); 30 | BOOST_TEST((D::max)().count() == max_rep); 31 | { 32 | typedef typename D::rep Rep; 33 | BOOST_CHRONO_LIB_CONSTEXPR Rep max_rep = (boost::chrono::duration_values::max)(); 34 | BOOST_CONSTEXPR_ASSERT((D::max)().count() == max_rep); 35 | } 36 | } 37 | 38 | template 39 | void check_min() 40 | { 41 | typedef typename D::rep Rep; 42 | Rep min_rep = (boost::chrono::duration_values::min)(); 43 | BOOST_TEST((D::min)().count() == min_rep); 44 | { 45 | typedef typename D::rep Rep; 46 | BOOST_CHRONO_LIB_CONSTEXPR Rep min_rep = (boost::chrono::duration_values::min)(); 47 | BOOST_CONSTEXPR_ASSERT((D::min)().count() == min_rep); 48 | 49 | } 50 | } 51 | 52 | template 53 | void check_zero() 54 | { 55 | typedef typename D::rep Rep; 56 | Rep zero_rep = boost::chrono::duration_values::zero(); 57 | BOOST_TEST(D::zero().count() == zero_rep); 58 | { 59 | typedef typename D::rep Rep; 60 | BOOST_CONSTEXPR Rep zero_rep = boost::chrono::duration_values::zero(); 61 | BOOST_CONSTEXPR_ASSERT(D::zero().count() == zero_rep); 62 | 63 | } 64 | } 65 | 66 | 67 | int main() 68 | { 69 | check_max >(); 70 | check_max >(); 71 | check_min >(); 72 | check_min >(); 73 | check_zero >(); 74 | check_zero >(); 75 | return boost::report_errors(); 76 | } 77 | -------------------------------------------------------------------------------- /example/io_ex5.cpp: -------------------------------------------------------------------------------- 1 | // io_ex1.cpp ----------------------------------------------------------// 2 | 3 | // Copyright 2010 Howard Hinnant 4 | // Copyright 2010 Vicente J. Botet Escriba 5 | 6 | // Distributed under the Boost Software License, Version 1.0. 7 | // See http://www.boost.org/LICENSE_1_0.txt 8 | 9 | /* 10 | This code was adapted by Vicente J. Botet Escriba from Hinnant's html documentation. 11 | Many thanks to Howard for making his code available under the Boost license. 12 | 13 | */ 14 | 15 | #include 16 | #include 17 | #include 18 | 19 | // format duration as [-]d/hh::mm::ss.cc 20 | template 21 | std::basic_ostream& 22 | display(std::basic_ostream& os, 23 | boost::chrono::duration d) 24 | { 25 | using std::cout; 26 | using namespace boost; 27 | using namespace boost::chrono; 28 | 29 | typedef duration > days; 30 | typedef duration centiseconds; 31 | 32 | // if negative, print negative sign and negate 33 | if (d < duration(0)) 34 | { 35 | d = -d; 36 | os << '-'; 37 | } 38 | // round d to nearest centiseconds, to even on tie 39 | centiseconds cs = duration_cast(d); 40 | if (d - cs > milliseconds(5) 41 | || (d - cs == milliseconds(5) && cs.count() & 1)) 42 | ++cs; 43 | // separate seconds from centiseconds 44 | seconds s = duration_cast(cs); 45 | cs -= s; 46 | // separate minutes from seconds 47 | minutes m = duration_cast(s); 48 | s -= m; 49 | // separate hours from minutes 50 | hours h = duration_cast(m); 51 | m -= h; 52 | // separate days from hours 53 | days dy = duration_cast(h); 54 | h -= dy; 55 | // print d/hh:mm:ss.cc 56 | os << dy.count() << '/'; 57 | if (h < hours(10)) 58 | os << '0'; 59 | os << h.count() << ':'; 60 | if (m < minutes(10)) 61 | os << '0'; 62 | os << m.count() << ':'; 63 | if (s < seconds(10)) 64 | os << '0'; 65 | os << s.count() << '.'; 66 | if (cs < centiseconds(10)) 67 | os << '0'; 68 | os << cs.count(); 69 | return os; 70 | } 71 | 72 | int main() 73 | { 74 | using std::cout; 75 | using namespace boost; 76 | using namespace boost::chrono; 77 | 78 | #ifdef BOOST_CHRONO_HAS_CLOCK_STEADY 79 | display(cout, steady_clock::now().time_since_epoch() 80 | + duration(1)) << '\n'; 81 | #endif 82 | display(cout, -milliseconds(6)) << '\n'; 83 | display(cout, duration(1)) << '\n'; 84 | display(cout, -duration(1)) << '\n'; 85 | } 86 | 87 | //~ 12/06:03:22.95 88 | //~ -0/00:00:00.01 89 | //~ 11/13:46:40.00 90 | //~ -11/13:46:40.00 91 | -------------------------------------------------------------------------------- /example/min_time_point.cpp: -------------------------------------------------------------------------------- 1 | // min_time_point.cpp ----------------------------------------------------------// 2 | 3 | // Copyright 2008 Howard Hinnant 4 | // Copyright 2008 Beman Dawes 5 | // Copyright 2009 Vicente J. Botet Escriba 6 | 7 | // Distributed under the Boost Software License, Version 1.0. 8 | // See http://www.boost.org/LICENSE_1_0.txt 9 | 10 | /* 11 | This code was extracted by Vicente J. Botet Escriba from Beman Dawes time2_demo.cpp which 12 | was derived by Beman Dawes from Howard Hinnant's time2_demo prototype. 13 | Many thanks to Howard for making his code available under the Boost license. 14 | The original code was modified to conform to Boost conventions and to section 15 | 20.9 Time utilities [time] of the C++ committee's working paper N2798. 16 | See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf. 17 | 18 | time2_demo contained this comment: 19 | 20 | Much thanks to Andrei Alexandrescu, 21 | Walter Brown, 22 | Peter Dimov, 23 | Jeff Garland, 24 | Terry Golubiewski, 25 | Daniel Krugler, 26 | Anthony Williams. 27 | */ 28 | 29 | #include 30 | #include 31 | 32 | #include 33 | 34 | using namespace boost::chrono; 35 | 36 | template 37 | void print_duration(std::ostream& os, duration d) 38 | { 39 | os << d.count() << " * " << Period::num << '/' << Period::den << " seconds\n"; 40 | } 41 | 42 | namespace my_ns { 43 | // Example min utility: returns the earliest time_point 44 | // Being able to *easily* write this function is a major feature! 45 | template 46 | inline 47 | typename boost::common_type, 48 | time_point >::type 49 | min BOOST_PREVENT_MACRO_SUBSTITUTION (time_point t1, time_point t2) 50 | { 51 | return t2 < t1 ? t2 : t1; 52 | } 53 | } 54 | void test_min() 55 | { 56 | #if 1 57 | typedef time_point::type> T1; 59 | typedef time_point::type> T2; 61 | typedef boost::common_type::type T3; 62 | /*auto*/ T1 t1 = system_clock::now() + seconds(3); 63 | /*auto*/ T2 t2 = system_clock::now() + nanoseconds(3); 64 | /*auto*/ T3 t3 = (my_ns::min)(t1, t2); 65 | #else 66 | BOOST_AUTO(t1, system_clock::now() + seconds(3)); 67 | BOOST_AUTO(t2, system_clock::now() + nanoseconds(3)); 68 | BOOST_AUTO(t3, (min)(t1, t2)); 69 | #endif 70 | print_duration(std::cout, t1 - t3); 71 | print_duration(std::cout, t2 - t3); 72 | } 73 | 74 | int main() 75 | { 76 | test_min(); 77 | return 0; 78 | } 79 | 80 | -------------------------------------------------------------------------------- /include/boost/chrono/io/utility/manip_base.hpp: -------------------------------------------------------------------------------- 1 | // boost/chrono/utility/manip_base.hpp ------------------------------------------------------------// 2 | 3 | // Copyright 2011 Vicente J. Botet Escriba 4 | 5 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 6 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 7 | 8 | // See http://www.boost.org/libs/chrono for documentation. 9 | 10 | #ifndef BOOST_CHRONO_UTILITY_MANIP_BASE_PTR_HPP 11 | #define BOOST_CHRONO_UTILITY_MANIP_BASE_PTR_HPP 12 | 13 | #include 14 | 15 | /** 16 | * 17 | 18 | */ 19 | 20 | namespace boost 21 | { 22 | namespace chrono 23 | { 24 | 25 | /** 26 | * manip is a manipulator mixin class following the CRTP. 27 | * @tparam Final the derived from manip and final type 28 | * 29 | * @Example 30 | * @code 31 | 32 | class mendl: public manip 33 | { 34 | public: 35 | explicit mendl(size_t how_many) : 36 | count(how_many) {} 37 | template 38 | void operator()(out_stream &out) const 39 | { 40 | for (size_t line = 0; line < count; ++line) 41 | { 42 | out.put(out.widen('\n')); 43 | } 44 | out.flush(); 45 | } 46 | private: 47 | size_t count; 48 | }; 49 | 50 | * @codeend 51 | */ 52 | template 53 | class manip 54 | { 55 | public: 56 | /** 57 | * 58 | * @param ios the io stream or ios_base. 59 | * @Effects calls to the manipulator final functor. 60 | */ 61 | //template 62 | void operator()(std::ios_base &ios) const 63 | { 64 | (*static_cast (this))(ios); 65 | } 66 | }; 67 | 68 | /** 69 | * @c manip stream inserter 70 | * @param out the io stream or ios_base. 71 | * @param op the manipulator instance. 72 | * @Effects if @c out is good calls to the manipulator functor @op. 73 | * @return @c out 74 | */ 75 | template 76 | out_stream &operator<<(out_stream &out, const manip &op) 77 | { 78 | if (out.good()) 79 | op(out); 80 | return out; 81 | } 82 | 83 | /** 84 | * @c manip stream extractor 85 | * @param in the io stream or ios_base. 86 | * @param op the manipulator instance. 87 | * @Effects if @c in is good calls to the manipulator functor @op. 88 | * @return @c in 89 | */ 90 | template 91 | in_stream &operator>>(in_stream &in, const manip &op) 92 | { 93 | if (in.good()) 94 | op(in); 95 | return in; 96 | } 97 | 98 | } // namespace chrono 99 | } // namespace boost 100 | 101 | #endif // header 102 | -------------------------------------------------------------------------------- /test/time_point/time_point_cast_pass.cpp: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // The LLVM Compiler Infrastructure 4 | // 5 | // This file is dual licensed under the MIT and the University of Illinois Open 6 | // Source Licenses. See LICENSE.TXT for details. 7 | // 8 | //===----------------------------------------------------------------------===// 9 | // Adaptation to Boost of the libcxx 10 | // Copyright 2010 Vicente J. Botet Escriba 11 | // Distributed under the Boost Software License, Version 1.0. 12 | // See http://www.boost.org/LICENSE_1_0.txt 13 | 14 | #include 15 | #include 16 | #include 17 | 18 | #if !defined(BOOST_NO_CXX11_STATIC_ASSERT) 19 | #define NOTHING "" 20 | #endif 21 | #ifdef BOOST_NO_CXX11_CONSTEXPR 22 | #define BOOST_CONSTEXPR_ASSERT(C) BOOST_TEST(C) 23 | #else 24 | #include 25 | #define BOOST_CONSTEXPR_ASSERT(C) BOOST_STATIC_ASSERT(C) 26 | #endif 27 | 28 | template 29 | void 30 | test(const FromDuration& df, const ToDuration& d) 31 | { 32 | typedef boost::chrono::system_clock Clock; 33 | typedef boost::chrono::time_point FromTimePoint; 34 | typedef boost::chrono::time_point ToTimePoint; 35 | FromTimePoint f(df); 36 | ToTimePoint t(d); 37 | //~ #if defined(BOOST_NO_CXX11_DECLTYPE) 38 | //~ typedef BOOST_TYPEOF_TPL(boost::chrono::time_point_cast(f)) R; 39 | //~ #else 40 | //~ typedef decltype(boost::chrono::time_point_cast(f)) R; 41 | //~ #endif 42 | //~ BOOST_CHRONO_STATIC_ASSERT((boost::is_same::value), NOTHING, ()); 43 | BOOST_TEST(boost::chrono::time_point_cast(f) == t); 44 | } 45 | 46 | int main() 47 | { 48 | test(boost::chrono::milliseconds(7265000), boost::chrono::hours(2)); 49 | test(boost::chrono::milliseconds(7265000), boost::chrono::minutes(121)); 50 | test(boost::chrono::milliseconds(7265000), boost::chrono::seconds(7265)); 51 | test(boost::chrono::milliseconds(7265000), boost::chrono::milliseconds(7265000)); 52 | test(boost::chrono::milliseconds(7265000), boost::chrono::microseconds(7265000000LL)); 53 | test(boost::chrono::milliseconds(7265000), boost::chrono::nanoseconds(7265000000000LL)); 54 | test(boost::chrono::milliseconds(7265000), 55 | boost::chrono::duration >(7265./3600)); 56 | test(boost::chrono::duration >(9), 57 | boost::chrono::duration >(10)); 58 | { 59 | typedef boost::chrono::system_clock Clock; 60 | typedef boost::chrono::time_point FromTimePoint; 61 | typedef boost::chrono::time_point ToTimePoint; 62 | BOOST_CONSTEXPR FromTimePoint f(boost::chrono::milliseconds(7265000)); 63 | BOOST_CONSTEXPR ToTimePoint tph = boost::chrono::time_point_cast(f); 64 | BOOST_CONSTEXPR_ASSERT(tph.time_since_epoch().count() == 2); 65 | } 66 | return boost::report_errors(); 67 | } 68 | -------------------------------------------------------------------------------- /include/boost/chrono/detail/inlined/posix/thread_clock.hpp: -------------------------------------------------------------------------------- 1 | // boost thread_clock.cpp -----------------------------------------------------------// 2 | 3 | // Copyright Beman Dawes 1994, 2006, 2008 4 | // Copyright Vicente J. Botet Escriba 2009-2011 5 | 6 | // Distributed under the Boost Software License, Version 1.0. 7 | // See http://www.boost.org/LICENSE_1_0.txt 8 | 9 | // See http://www.boost.org/libs/chrono for documentation. 10 | 11 | //--------------------------------------------------------------------------------------// 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | #if !defined(__VXWORKS__) 19 | # include 20 | #endif 21 | # include 22 | # include 23 | 24 | namespace boost { namespace chrono { 25 | 26 | thread_clock::time_point thread_clock::now( ) BOOST_NOEXCEPT 27 | { 28 | struct timespec ts; 29 | #if defined CLOCK_THREAD_CPUTIME_ID 30 | // get the timespec associated to the thread clock 31 | if ( ::clock_gettime( CLOCK_THREAD_CPUTIME_ID, &ts ) ) 32 | #else 33 | // get the current thread 34 | pthread_t pth=pthread_self(); 35 | // get the clock_id associated to the current thread 36 | clockid_t clock_id; 37 | pthread_getcpuclockid(pth, &clock_id); 38 | // get the timespec associated to the thread clock 39 | if ( ::clock_gettime( clock_id, &ts ) ) 40 | #endif 41 | { 42 | BOOST_ASSERT(0 && "Boost::Chrono - Internal Error"); 43 | } 44 | 45 | // transform to nanoseconds 46 | return time_point(duration( 47 | static_cast( ts.tv_sec ) * 1000000000 + ts.tv_nsec)); 48 | 49 | } 50 | 51 | #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING 52 | thread_clock::time_point thread_clock::now( system::error_code & ec ) 53 | { 54 | struct timespec ts; 55 | #if defined CLOCK_THREAD_CPUTIME_ID 56 | // get the timespec associated to the thread clock 57 | if ( ::clock_gettime( CLOCK_THREAD_CPUTIME_ID, &ts ) ) 58 | #else 59 | // get the current thread 60 | pthread_t pth=pthread_self(); 61 | // get the clock_id associated to the current thread 62 | clockid_t clock_id; 63 | pthread_getcpuclockid(pth, &clock_id); 64 | // get the timespec associated to the thread clock 65 | if ( ::clock_gettime( clock_id, &ts ) ) 66 | #endif 67 | { 68 | if (::boost::chrono::is_throws(ec)) 69 | { 70 | boost::throw_exception( 71 | system::system_error( 72 | errno, 73 | ::boost::system::system_category(), 74 | "chrono::thread_clock" )); 75 | } 76 | else 77 | { 78 | ec.assign( errno, ::boost::system::system_category() ); 79 | return time_point(); 80 | } 81 | } 82 | if (!::boost::chrono::is_throws(ec)) 83 | { 84 | ec.clear(); 85 | } 86 | // transform to nanoseconds 87 | return time_point(duration( 88 | static_cast( ts.tv_sec ) * 1000000000 + ts.tv_nsec)); 89 | 90 | } 91 | #endif 92 | } } 93 | -------------------------------------------------------------------------------- /test/time_point/constructor_pass.cpp: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // The LLVM Compiler Infrastructure 4 | // 5 | // This file is dual licensed under the MIT and the University of Illinois Open 6 | // Source Licenses. See LICENSE.TXT for details. 7 | // 8 | //===----------------------------------------------------------------------===// 9 | // Adaptation to Boost of the libcxx 10 | // Copyright 2010 Vicente J. Botet Escriba 11 | // Distributed under the Boost Software License, Version 1.0. 12 | // See http://www.boost.org/LICENSE_1_0.txt 13 | 14 | #include 15 | #include 16 | #include "../rep.h" 17 | #ifdef BOOST_NO_CXX11_CONSTEXPR 18 | #define BOOST_CONSTEXPR_ASSERT(C) BOOST_TEST(C) 19 | #else 20 | #include 21 | #define BOOST_CONSTEXPR_ASSERT(C) BOOST_STATIC_ASSERT(C) 22 | #endif 23 | 24 | int main() 25 | { 26 | { 27 | typedef boost::chrono::system_clock Clock; 28 | typedef boost::chrono::microseconds Duration1; 29 | typedef boost::chrono::milliseconds Duration2; 30 | boost::chrono::time_point t2(Duration2(3)); 31 | boost::chrono::time_point t1 = t2; 32 | BOOST_TEST(t1.time_since_epoch() == Duration1(3000)); 33 | } 34 | { 35 | typedef boost::chrono::system_clock Clock; 36 | typedef boost::chrono::microseconds Duration1; 37 | typedef boost::chrono::milliseconds Duration2; 38 | BOOST_CONSTEXPR boost::chrono::time_point t2(Duration2(3)); 39 | BOOST_CONSTEXPR boost::chrono::time_point t1 = t2; 40 | BOOST_CONSTEXPR_ASSERT(t1.time_since_epoch() == Duration1(3000)); 41 | } 42 | { 43 | typedef boost::chrono::system_clock Clock; 44 | typedef boost::chrono::duration Duration; 45 | boost::chrono::time_point t; 46 | BOOST_TEST(t.time_since_epoch() == Duration::zero()); 47 | } 48 | { 49 | typedef boost::chrono::system_clock Clock; 50 | typedef boost::chrono::duration Duration; 51 | BOOST_CONSTEXPR boost::chrono::time_point t; 52 | BOOST_CONSTEXPR_ASSERT(t.time_since_epoch() == Duration::zero()); 53 | } 54 | { 55 | typedef boost::chrono::system_clock Clock; 56 | typedef boost::chrono::milliseconds Duration; 57 | boost::chrono::time_point t(Duration(3)); 58 | BOOST_TEST(t.time_since_epoch() == Duration(3)); 59 | } 60 | { 61 | typedef boost::chrono::system_clock Clock; 62 | typedef boost::chrono::milliseconds Duration; 63 | BOOST_CONSTEXPR boost::chrono::time_point t(Duration(3)); 64 | BOOST_CONSTEXPR_ASSERT(t.time_since_epoch() == Duration(3)); 65 | } 66 | { 67 | typedef boost::chrono::system_clock Clock; 68 | typedef boost::chrono::milliseconds Duration; 69 | boost::chrono::time_point t(boost::chrono::seconds(3)); 70 | BOOST_TEST(t.time_since_epoch() == Duration(3000)); 71 | } 72 | { 73 | typedef boost::chrono::system_clock Clock; 74 | typedef boost::chrono::milliseconds Duration; 75 | BOOST_CONSTEXPR boost::chrono::time_point t(boost::chrono::seconds(3)); 76 | BOOST_CONSTEXPR_ASSERT(t.time_since_epoch() == Duration(3000)); 77 | } 78 | 79 | return boost::report_errors(); 80 | } 81 | -------------------------------------------------------------------------------- /example/xtime.cpp: -------------------------------------------------------------------------------- 1 | // xtime.cpp ----------------------------------------------------------// 2 | 3 | // Copyright 2008 Howard Hinnant 4 | // Copyright 2008 Beman Dawes 5 | // Copyright 2009 Vicente J. Botet Escriba 6 | 7 | // Distributed under the Boost Software License, Version 1.0. 8 | // See http://www.boost.org/LICENSE_1_0.txt 9 | 10 | /* 11 | This code was extracted by Vicente J. Botet Escriba from Beman Dawes time2_demo.cpp which 12 | was derived by Beman Dawes from Howard Hinnant's time2_demo prototype. 13 | Many thanks to Howard for making his code available under the Boost license. 14 | The original code was modified to conform to Boost conventions and to section 15 | 20.9 Time utilities [time] of the C++ committee's working paper N2798. 16 | See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf. 17 | 18 | time2_demo contained this comment: 19 | 20 | Much thanks to Andrei Alexandrescu, 21 | Walter Brown, 22 | Peter Dimov, 23 | Jeff Garland, 24 | Terry Golubiewski, 25 | Daniel Krugler, 26 | Anthony Williams. 27 | */ 28 | 29 | #include 30 | #include 31 | 32 | #include 33 | 34 | using namespace boost::chrono; 35 | 36 | // Example round_up utility: converts d to To, rounding up for inexact conversions 37 | // Being able to *easily* write this function is a major feature! 38 | template 39 | To 40 | round_up(duration d) 41 | { 42 | To result = duration_cast(d); 43 | if (result < d) 44 | ++result; 45 | return result; 46 | } 47 | 48 | // demonstrate interaction with xtime-like facility: 49 | 50 | // msvc defines ::xtime in , so we use xtime_ 51 | struct xtime_ 52 | { 53 | long sec; 54 | unsigned long usec; 55 | }; 56 | 57 | template 58 | xtime_ 59 | to_xtime_truncate(duration d) 60 | { 61 | xtime_ xt; 62 | xt.sec = static_cast(duration_cast(d).count()); 63 | xt.usec = static_cast(duration_cast(d - seconds(xt.sec)).count()); 64 | return xt; 65 | } 66 | 67 | template 68 | xtime_ 69 | to_xtime_round_up(duration d) 70 | { 71 | xtime_ xt; 72 | xt.sec = static_cast(duration_cast(d).count()); 73 | xt.usec = static_cast(round_up(d - seconds(xt.sec)).count()); 74 | return xt; 75 | } 76 | 77 | microseconds 78 | from_xtime(xtime_ xt) 79 | { 80 | return seconds(xt.sec) + microseconds(xt.usec); 81 | } 82 | 83 | void print(xtime_ xt) 84 | { 85 | std::cout << '{' << xt.sec << ',' << xt.usec << "}\n"; 86 | } 87 | 88 | void test_with_xtime() 89 | { 90 | std::cout << "test_with_xtime\n"; 91 | xtime_ xt = to_xtime_truncate(seconds(3) + milliseconds(251)); 92 | print(xt); 93 | milliseconds ms = duration_cast(from_xtime(xt)); 94 | std::cout << ms.count() << " milliseconds\n"; 95 | xt = to_xtime_round_up(ms); 96 | print(xt); 97 | xt = to_xtime_truncate(seconds(3) + nanoseconds(999)); 98 | print(xt); 99 | xt = to_xtime_round_up(seconds(3) + nanoseconds(999)); 100 | print(xt); 101 | } 102 | 103 | 104 | int main() 105 | { 106 | test_with_xtime(); 107 | return 0; 108 | } 109 | 110 | -------------------------------------------------------------------------------- /doc/Jamfile.v2: -------------------------------------------------------------------------------- 1 | # Boost.Chrono library documentation Jamfile --------------------------------- 2 | # 3 | # Copyright Vicente J. Botet Escriba 2009. Use, modification and 4 | # distribution is subject to the Boost Software License, Version 5 | # 1.0. (See accompanying file LICENSE_1_0.txt or copy at 6 | # http://www.boost.org/LICENSE_1_0.txt) 7 | # 8 | # See http://www.boost.org for updates, documentation, and revision history. 9 | 10 | #import doxygen ; 11 | using quickbook ; 12 | 13 | #doxygen autodoc 14 | # : 15 | # [ glob ../include/boost/chrono/*.hpp ] 16 | # [ glob ../include/boost/chrono/allocators/*.hpp ] 17 | # : 18 | # EXTRACT_ALL=NO 19 | # HIDE_UNDOC_MEMBERS=YES 20 | # EXTRACT_PRIVATE=NO 21 | # EXPAND_ONLY_PREDEF=YES 22 | # PREDEFINED=BOOST_INTERPROCESS_DOXYGEN_INVOKED 23 | # "boost.doxygen.reftitle=Boost.Chrono Reference" 24 | # ; 25 | 26 | xml chrono : chrono.qbk ; 27 | 28 | boostbook standalone 29 | : 30 | chrono 31 | : 32 | # HTML options first: 33 | # Use graphics not text for navigation: 34 | navig.graphics=1 35 | # How far down we chunk nested sections, basically all of them: 36 | chunk.section.depth=2 37 | # Don't put the first section on the same page as the TOC: 38 | chunk.first.sections=1 39 | # How far down sections get TOC's 40 | toc.section.depth=4 41 | # Max depth in each TOC: 42 | toc.max.depth=2 43 | # How far down we go with TOC's 44 | generate.section.toc.level=10 45 | # Path for links to Boost: 46 | boost.root=../../../.. 47 | # Path for libraries index: 48 | boost.libraries=../../../../libs/libraries.htm 49 | # Use the main Boost stylesheet: 50 | html.stylesheet=../../../../doc/src/boostbook.css 51 | 52 | # PDF Options: 53 | # TOC Generation: this is needed for FOP-0.9 and later: 54 | pdf:fop1.extensions=0 55 | # Or enable this if you're using XEP: 56 | pdf:xep.extensions=1 57 | # TOC generation: this is needed for FOP 0.2, but must not be set to zero for FOP-0.9! 58 | pdf:fop.extensions=0 59 | # No indent on body text: 60 | body.start.indent=0pt 61 | # Margin size: 62 | page.margin.inner=0.5in 63 | # Margin size: 64 | page.margin.outer=0.5in 65 | # Yes, we want graphics for admonishments: 66 | admon.graphics=1 67 | # Set this one for PDF generation *only*: 68 | # default pnd graphics are awful in PDF form, 69 | # better use SVG's instead: 70 | pdf:admon.graphics.extension=".svg" 71 | pdf:admon.graphics.path=$(boost-images) 72 | pdf:boost.url.prefix=http://www.boost.org/doc/libs/release/libs/chrono/doc/html 73 | ; 74 | 75 | install pdfinstall : standalone/pdf : . PDF ; 76 | explicit pdfinstall ; 77 | 78 | ############################################################################### 79 | alias boostdoc 80 | : chrono 81 | : 82 | : 83 | : ; 84 | explicit boostdoc ; 85 | alias boostrelease ; 86 | explicit boostrelease ; 87 | -------------------------------------------------------------------------------- /example/io_ex1.cpp: -------------------------------------------------------------------------------- 1 | // io_ex1.cpp ----------------------------------------------------------// 2 | 3 | // Copyright 2010 Howard Hinnant 4 | // Copyright 2010 Vicente J. Botet Escriba 5 | // Copyright (c) Microsoft Corporation 2014 6 | 7 | // Distributed under the Boost Software License, Version 1.0. 8 | // See http://www.boost.org/LICENSE_1_0.txt 9 | 10 | /* 11 | This code was adapted by Vicente J. Botet Escriba from Hinnant's html documentation. 12 | Many thanks to Howard for making his code available under the Boost license. 13 | 14 | */ 15 | 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | int main() 24 | { 25 | using std::cout; 26 | using namespace boost; 27 | using namespace boost::chrono; 28 | 29 | cout << "milliseconds(1) = " 30 | << milliseconds(1) << '\n'; 31 | cout << "milliseconds(3) + microseconds(10) = " 32 | << milliseconds(3) + microseconds(10) << '\n'; 33 | 34 | cout << "hours(3) + minutes(10) = " 35 | << hours(3) + minutes(10) << '\n'; 36 | 37 | typedef duration > ClockTick; 38 | cout << "ClockTick(3) + nanoseconds(10) = " 39 | << ClockTick(3) + nanoseconds(10) << '\n'; 40 | 41 | cout << "\nSet cout to use short names:\n"; 42 | #if BOOST_CHRONO_VERSION==2 43 | cout << duration_fmt(duration_style::symbol); 44 | #else 45 | cout << duration_short; 46 | #endif 47 | cout << "milliseconds(3) + microseconds(10) = " 48 | << milliseconds(3) + microseconds(10) << '\n'; 49 | 50 | cout << "hours(3) + minutes(10) = " 51 | << hours(3) + minutes(10) << '\n'; 52 | 53 | cout << "ClockTick(3) + nanoseconds(10) = " 54 | << ClockTick(3) + nanoseconds(10) << '\n'; 55 | 56 | cout << "\nsystem_clock::now() = " << system_clock::now() << '\n'; 57 | #if defined _MSC_VER && _MSC_VER == 1700 58 | #else 59 | #if BOOST_CHRONO_VERSION==2 60 | cout << "\nsystem_clock::now() = " << time_fmt(chrono::timezone::local) << system_clock::now() << '\n'; 61 | cout << "\nsystem_clock::now() = " << time_fmt(chrono::timezone::local,"%Y/%m/%d") << system_clock::now() << '\n'; 62 | #endif 63 | #endif 64 | 65 | #ifdef BOOST_CHRONO_HAS_CLOCK_STEADY 66 | cout << "steady_clock::now() = " << steady_clock::now() << '\n'; 67 | #endif 68 | #if BOOST_CHRONO_VERSION==2 69 | cout << "\nSet cout to use long names:\n" << duration_fmt(duration_style::prefix) 70 | << "high_resolution_clock::now() = " << high_resolution_clock::now() << '\n'; 71 | #else 72 | cout << "\nSet cout to use long names:\n" << duration_long 73 | << "high_resolution_clock::now() = " << high_resolution_clock::now() << '\n'; 74 | #endif 75 | #if defined(BOOST_CHRONO_HAS_THREAD_CLOCK) 76 | cout << "\nthread_clock::now() = " << thread_clock::now() << '\n'; 77 | #endif 78 | #if defined(BOOST_CHRONO_HAS_PROCESS_CLOCKS) 79 | cout << "\nprocess_real_cpu_clock::now() = " << process_real_cpu_clock::now() << '\n'; 80 | #if BOOST_PLAT_WINDOWS_DESKTOP 81 | cout << "\nprocess_user_cpu_clock::now() = " << process_user_cpu_clock::now() << '\n'; 82 | cout << "\nprocess_system_cpu_clock::now() = " << process_system_cpu_clock::now() << '\n'; 83 | cout << "\nprocess_cpu_clock::now() = " << process_cpu_clock::now() << '\n'; 84 | #endif 85 | #endif 86 | return 0; 87 | } 88 | -------------------------------------------------------------------------------- /test/test_7868.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2013 Vicente J. Botet Escriba 2 | // Distributed under the Boost Software License, Version 1.0. 3 | // See http://www.boost.org/LICENSE_1_0.txt 4 | // See http://www.boost.org/libs/chrono for documentation. 5 | 6 | #define BOOST_CHRONO_VERSION 2 7 | //#define BOOST_CHRONO_PROVIDES_DATE_IO_FOR_SYSTEM_CLOCK_TIME_POINT 1 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | int main() 19 | { 20 | using namespace boost; 21 | using namespace boost::chrono; 22 | boost::chrono::system_clock::time_point atnow= boost::chrono::system_clock::now(); 23 | { 24 | std::stringstream strm; 25 | std::stringstream strm2; 26 | // does not change anything: strm<>atnow2; 37 | strm2<(atnow2 - atnow).count() <(atnow2 - atnow).count(), 0); 45 | std::stringstream formatted; 46 | formatted << time_fmt(boost::chrono::timezone::utc, "%Y-%m-%d %H:%M:%S"); 47 | formatted << "actual:"<< atnow <>atnow2; 72 | strm2<(atnow2 - atnow).count(), 0); 78 | 79 | } 80 | return boost::report_errors(); 81 | } 82 | -------------------------------------------------------------------------------- /include/boost/chrono/detail/inlined/win/thread_clock.hpp: -------------------------------------------------------------------------------- 1 | // boost thread_clock.cpp -----------------------------------------------------------// 2 | 3 | // Copyright 2010 Vicente J. Botet Escriba 4 | 5 | // Distributed under the Boost Software License, Version 1.0. 6 | // See http://www.boost.org/LICENSE_1_0.txt 7 | 8 | // See http://www.boost.org/libs/chrono for documentation. 9 | 10 | //--------------------------------------------------------------------------------------// 11 | #ifndef BOOST_CHRONO_DETAIL_INLINED_WIN_THREAD_CLOCK_HPP 12 | #define BOOST_CHRONO_DETAIL_INLINED_WIN_THREAD_CLOCK_HPP 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | #include 20 | #include 21 | #include 22 | 23 | namespace boost 24 | { 25 | namespace chrono 26 | { 27 | 28 | #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING 29 | thread_clock::time_point thread_clock::now( system::error_code & ec ) 30 | { 31 | // note that Windows uses 100 nanosecond ticks for FILETIME 32 | boost::winapi::FILETIME_ creation, exit, user_time, system_time; 33 | 34 | if ( boost::winapi::GetThreadTimes( 35 | boost::winapi::GetCurrentThread (), &creation, &exit, 36 | &system_time, &user_time ) ) 37 | { 38 | duration user = duration( 39 | ((static_cast(user_time.dwHighDateTime) << 32) 40 | | user_time.dwLowDateTime) * 100 ); 41 | 42 | duration system = duration( 43 | ((static_cast(system_time.dwHighDateTime) << 32) 44 | | system_time.dwLowDateTime) * 100 ); 45 | 46 | if (!::boost::chrono::is_throws(ec)) 47 | { 48 | ec.clear(); 49 | } 50 | return time_point(system+user); 51 | 52 | } 53 | else 54 | { 55 | if (::boost::chrono::is_throws(ec)) 56 | { 57 | boost::throw_exception( 58 | system::system_error( 59 | boost::winapi::GetLastError(), 60 | ::boost::system::system_category(), 61 | "chrono::thread_clock" )); 62 | } 63 | else 64 | { 65 | ec.assign( boost::winapi::GetLastError(), ::boost::system::system_category() ); 66 | return thread_clock::time_point(duration(0)); 67 | } 68 | } 69 | } 70 | #endif 71 | 72 | thread_clock::time_point thread_clock::now() BOOST_NOEXCEPT 73 | { 74 | 75 | // note that Windows uses 100 nanosecond ticks for FILETIME 76 | boost::winapi::FILETIME_ creation, exit, user_time, system_time; 77 | 78 | if ( boost::winapi::GetThreadTimes( 79 | boost::winapi::GetCurrentThread (), &creation, &exit, 80 | &system_time, &user_time ) ) 81 | { 82 | duration user = duration( 83 | ((static_cast(user_time.dwHighDateTime) << 32) 84 | | user_time.dwLowDateTime) * 100 ); 85 | 86 | duration system = duration( 87 | ((static_cast(system_time.dwHighDateTime) << 32) 88 | | system_time.dwLowDateTime) * 100 ); 89 | 90 | return time_point(system+user); 91 | } 92 | else 93 | { 94 | BOOST_ASSERT(0 && "Boost::Chrono - Internal Error"); 95 | return time_point(); 96 | } 97 | 98 | } 99 | 100 | } // namespace chrono 101 | } // namespace boost 102 | 103 | #endif 104 | -------------------------------------------------------------------------------- /include/boost/chrono/detail/inlined/mac/thread_clock.hpp: -------------------------------------------------------------------------------- 1 | // boost thread_clock.cpp -----------------------------------------------------------// 2 | 3 | // Copyright Beman Dawes 1994, 2006, 2008 4 | // Copyright Vicente J. Botet Escriba 2009-2011 5 | // Copyright Christopher Brown 2013 6 | 7 | // Distributed under the Boost Software License, Version 1.0. 8 | // See http://www.boost.org/LICENSE_1_0.txt 9 | 10 | // See http://www.boost.org/libs/chrono for documentation. 11 | 12 | //--------------------------------------------------------------------------------------// 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | # include 20 | # include 21 | 22 | namespace boost { namespace chrono { 23 | 24 | thread_clock::time_point thread_clock::now( ) BOOST_NOEXCEPT 25 | { 26 | // get the thread port (borrowing pthread's reference) 27 | mach_port_t port = pthread_mach_thread_np(pthread_self()); 28 | 29 | // get the thread info 30 | thread_basic_info_data_t info; 31 | mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT; 32 | if ( thread_info(port, THREAD_BASIC_INFO, (thread_info_t)&info, &count) != KERN_SUCCESS ) 33 | { 34 | BOOST_ASSERT(0 && "Boost::Chrono - Internal Error"); 35 | return time_point(); 36 | } 37 | 38 | // convert to nanoseconds 39 | duration user = duration( 40 | static_cast( info.user_time.seconds ) * 1000000000 41 | + static_cast(info.user_time.microseconds ) * 1000); 42 | 43 | duration system = duration( 44 | static_cast( info.system_time.seconds ) * 1000000000 45 | + static_cast( info.system_time.microseconds ) * 1000); 46 | 47 | return time_point( user + system ); 48 | } 49 | 50 | #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING 51 | thread_clock::time_point thread_clock::now( system::error_code & ec ) 52 | { 53 | // get the thread port (borrowing pthread's reference) 54 | mach_port_t port = pthread_mach_thread_np(pthread_self()); 55 | 56 | // get the thread info 57 | thread_basic_info_data_t info; 58 | mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT; 59 | if ( thread_info(port, THREAD_BASIC_INFO, (thread_info_t)&info, &count) != KERN_SUCCESS ) 60 | { 61 | if (::boost::chrono::is_throws(ec)) 62 | { 63 | boost::throw_exception( 64 | system::system_error( 65 | EINVAL, 66 | ::boost::system::system_category(), 67 | "chrono::thread_clock" )); 68 | } 69 | else 70 | { 71 | ec.assign( errno, ::boost::system::system_category() ); 72 | return time_point(); 73 | } 74 | } 75 | if (!::boost::chrono::is_throws(ec)) 76 | { 77 | ec.clear(); 78 | } 79 | 80 | // convert to nanoseconds 81 | duration user = duration( 82 | static_cast( info.user_time.seconds ) * 1000000000 83 | + static_cast(info.user_time.microseconds ) * 1000); 84 | 85 | duration system = duration( 86 | static_cast( info.system_time.seconds ) * 1000000000 87 | + static_cast( info.system_time.microseconds ) * 1000); 88 | 89 | return time_point( user + system ); 90 | } 91 | #endif 92 | } } 93 | -------------------------------------------------------------------------------- /test/io/duration_output.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2011 Vicente J. Botet Escriba 2 | // Distributed under the Boost Software License, Version 1.0. 3 | // See http://www.boost.org/LICENSE_1_0.txt 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | template 10 | void test_good_prefix(const char* str, D d) 11 | { 12 | std::ostringstream out; 13 | out << d; 14 | BOOST_TEST(out.good()); 15 | 16 | BOOST_TEST(out.str() == str); 17 | } 18 | 19 | template 20 | void test_good_symbol(const char* str, D d) 21 | { 22 | std::ostringstream out; 23 | #if BOOST_CHRONO_VERSION==2 24 | out << boost::chrono::duration_fmt(boost::chrono::duration_style::symbol) << d; 25 | #else 26 | out << boost::chrono::duration_short << d; 27 | #endif 28 | BOOST_TEST(out.good()); 29 | BOOST_TEST_EQ(out.str(), str); 30 | } 31 | #if BOOST_CHRONO_VERSION==2 32 | 33 | template 34 | void test_good(const char* str, D d, boost::chrono::duration_style style) 35 | { 36 | std::ostringstream out; 37 | 38 | out << boost::chrono::duration_fmt(style) << d; 39 | BOOST_TEST(out.good()); 40 | BOOST_TEST(out.str() == str); 41 | } 42 | 43 | template 44 | void test_state_saver(const char* str, const char* str2, D d, boost::chrono::duration_style style) 45 | { 46 | std::ostringstream out; 47 | { 48 | boost::chrono::duration_style_io_saver ios(out); 49 | out << boost::chrono::duration_fmt(style) << d; 50 | BOOST_TEST(out.good()); 51 | BOOST_TEST(out.str() == str); 52 | } 53 | out << " " << d; 54 | BOOST_TEST(out.good()); 55 | BOOST_TEST(out.str() == str2); 56 | } 57 | 58 | template 59 | void test_state_saver2(const char* str, const char* str2, D d, boost::chrono::duration_style style) 60 | { 61 | std::ostringstream out; 62 | { 63 | boost::chrono::duration_style_io_saver ios(out, style); 64 | out << d; 65 | BOOST_TEST(out.good()); 66 | BOOST_TEST(out.str() == str); 67 | } 68 | out << " " << d; 69 | BOOST_TEST(out.good()); 70 | BOOST_TEST(out.str() == str2); 71 | } 72 | 73 | #endif 74 | 75 | int main() 76 | { 77 | using namespace boost::chrono; 78 | using namespace boost; 79 | 80 | test_good_prefix("5000 hours", hours(5000)); 81 | test_good_prefix("5000 minutes", minutes(5000)); 82 | test_good_prefix("5000 seconds", seconds(5000)); 83 | test_good_prefix("0 seconds", seconds(0)); 84 | test_good_prefix("1 second", seconds(1)); 85 | test_good_prefix("-1 second", seconds(-1)); 86 | test_good_prefix("5000 milliseconds", milliseconds(5000)); 87 | test_good_prefix("5000 microseconds", microseconds(5000)); 88 | test_good_prefix("5000 nanoseconds", nanoseconds(5000)); 89 | test_good_prefix("5000 deciseconds", duration (5000)); 90 | test_good_prefix("5000 [1/30]seconds", duration > (5000)); 91 | 92 | test_good_symbol("5000 h", hours(5000)); 93 | #if BOOST_CHRONO_VERSION==2 94 | test_good_symbol("5000 min", minutes(5000)); 95 | #else 96 | test_good_symbol("5000 m", minutes(5000)); 97 | #endif 98 | test_good_symbol("5000 s", seconds(5000)); 99 | test_good_symbol("5000 ms", milliseconds(5000)); 100 | test_good_symbol("5000 ns", nanoseconds(5000)); 101 | test_good_symbol("5000 ds", duration (5000)); 102 | test_good_symbol("5000 [1/30]s", duration > (5000)); 103 | 104 | #if BOOST_CHRONO_VERSION==2 105 | test_good("5000 hours", hours(5000), duration_style::prefix); 106 | test_good("5000 h", hours(5000), duration_style::symbol); 107 | test_state_saver("5000 h", "5000 h 5000 hours", hours(5000), duration_style::symbol); 108 | test_state_saver2("5000 h", "5000 h 5000 hours", hours(5000), duration_style::symbol); 109 | #endif 110 | 111 | return boost::report_errors(); 112 | 113 | } 114 | 115 | -------------------------------------------------------------------------------- /example/i_dont_like_the_default_duration_behavior.cpp: -------------------------------------------------------------------------------- 1 | // i_dont_like_the_default_duration_behavior.cpp ----------------------------------------------------------// 2 | 3 | // Copyright 2008 Howard Hinnant 4 | // Copyright 2008 Beman Dawes 5 | // Copyright 2009 Vicente J. Botet Escriba 6 | 7 | // Distributed under the Boost Software License, Version 1.0. 8 | // See http://www.boost.org/LICENSE_1_0.txt 9 | 10 | /* 11 | This code was extracted by Vicente J. Botet Escriba from Beman Dawes time2_demo.cpp which 12 | was derived by Beman Dawes from Howard Hinnant's time2_demo prototype. 13 | Many thanks to Howard for making his code available under the Boost license. 14 | The original code was modified to conform to Boost conventions and to section 15 | 20.9 Time utilities [time] of the C++ committee's working paper N2798. 16 | See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf. 17 | 18 | time2_demo contained this comment: 19 | 20 | Much thanks to Andrei Alexandrescu, 21 | Walter Brown, 22 | Peter Dimov, 23 | Jeff Garland, 24 | Terry Golubiewski, 25 | Daniel Krugler, 26 | Anthony Williams. 27 | */ 28 | 29 | #include 30 | #include 31 | 32 | #include 33 | 34 | namespace I_dont_like_the_default_duration_behavior 35 | { 36 | 37 | // Here's how you override the duration's default constructor to do anything you want (in this case zero) 38 | 39 | template 40 | class zero_default 41 | { 42 | public: 43 | typedef R rep; 44 | 45 | private: 46 | rep rep_; 47 | public: 48 | zero_default(rep i = 0) : rep_(i) {} 49 | operator rep() const {return rep_;} 50 | 51 | zero_default& operator+=(zero_default x) {rep_ += x.rep_; return *this;} 52 | zero_default& operator-=(zero_default x) {rep_ -= x.rep_; return *this;} 53 | zero_default& operator*=(zero_default x) {rep_ *= x.rep_; return *this;} 54 | zero_default& operator/=(zero_default x) {rep_ /= x.rep_; return *this;} 55 | 56 | zero_default operator+ () const {return *this;} 57 | zero_default operator- () const {return zero_default(-rep_);} 58 | zero_default& operator++() {++rep_; return *this;} 59 | zero_default operator++(int) {return zero_default(rep_++);} 60 | zero_default& operator--() {--rep_; return *this;} 61 | zero_default operator--(int) {return zero_default(rep_--);} 62 | 63 | friend zero_default operator+(zero_default x, zero_default y) {return x += y;} 64 | friend zero_default operator-(zero_default x, zero_default y) {return x -= y;} 65 | friend zero_default operator*(zero_default x, zero_default y) {return x *= y;} 66 | friend zero_default operator/(zero_default x, zero_default y) {return x /= y;} 67 | 68 | friend bool operator==(zero_default x, zero_default y) {return x.rep_ == y.rep_;} 69 | friend bool operator!=(zero_default x, zero_default y) {return !(x == y);} 70 | friend bool operator< (zero_default x, zero_default y) {return x.rep_ < y.rep_;} 71 | friend bool operator<=(zero_default x, zero_default y) {return !(y < x);} 72 | friend bool operator> (zero_default x, zero_default y) {return y < x;} 73 | friend bool operator>=(zero_default x, zero_default y) {return !(x < y);} 74 | }; 75 | 76 | typedef boost::chrono::duration, boost::nano > nanoseconds; 77 | typedef boost::chrono::duration, boost::micro > microseconds; 78 | typedef boost::chrono::duration, boost::milli > milliseconds; 79 | typedef boost::chrono::duration > seconds; 80 | typedef boost::chrono::duration, boost::ratio<60> > minutes; 81 | typedef boost::chrono::duration, boost::ratio<3600> > hours; 82 | 83 | void test() 84 | { 85 | milliseconds ms; 86 | std::cout << ms.count() << '\n'; 87 | } 88 | 89 | } // I_dont_like_the_default_duration_behavior 90 | 91 | 92 | int main() 93 | { 94 | I_dont_like_the_default_duration_behavior::test(); 95 | return 0; 96 | } 97 | 98 | -------------------------------------------------------------------------------- /perf/store_now_in_vector.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2011 Vicente J. Botet Escriba 2 | // Copyright (c) Microsoft Corporation 2014 3 | 4 | // Distributed under the Boost Software License, Version 1.0. 5 | // See http://www.boost.org/LICENSE_1_0.txt 6 | 7 | #include "../example/timer.hpp" 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | //#define BOOST_CHRONO_HAS_TIMES_AND_CLOCK 15 | 16 | #ifdef BOOST_CHRONO_HAS_TIMES_AND_CLOCK 17 | #include //for gettimeofday and timeval 18 | #include //for times 19 | #include 20 | #endif 21 | 22 | 23 | static const std::size_t size = 1000000; 24 | 25 | typedef boost_ex::chrono::timer Stopwatch; 26 | 27 | template 28 | void perf_constant(std::vector& vec) 29 | { 30 | for (int i=size-1; i>=0; --i) 31 | { 32 | vec[i]=typename Clock::time_point(); 33 | } 34 | } 35 | 36 | template 37 | void perf(std::vector& vec) 38 | { 39 | for (int i=size-1; i>=0; --i) 40 | { 41 | vec[i]=Clock::now(); 42 | } 43 | } 44 | 45 | template 46 | void test() 47 | { 48 | std::vector vec(size); 49 | Stopwatch sw1; 50 | perf_constant(vec); 51 | Stopwatch::duration t1 = sw1.elapsed(); 52 | Stopwatch sw2; 53 | perf(vec); 54 | Stopwatch::duration t2 = sw2.elapsed(); 55 | std::cout <<" "<< (t2-t1) << std::endl; 56 | //std::cout <<" "<< ((t2-t1)/size) << std::endl; 57 | std::size_t cnt=0; 58 | for (int i=size-1; i>0; --i) 59 | { 60 | if (vec[i]!=vec[i-1]) ++cnt; 61 | } 62 | std::cout <<"changes: "<< cnt << std::endl; 63 | 64 | } 65 | 66 | 67 | 68 | 69 | 70 | #ifdef BOOST_CHRONO_HAS_TIMES_AND_CLOCK 71 | void perf2(std::vector& vec) 72 | { 73 | Stopwatch sw; 74 | for (int i=size-1; i>=0; --i) 75 | { 76 | tms tm; 77 | vec[i]=::times(&tm); 78 | } 79 | std::cout << sw.elapsed() << std::endl; 80 | } 81 | 82 | void perf3(std::vector& vec) 83 | { 84 | Stopwatch sw; 85 | for (int i=size-1; i>=0; --i) 86 | { 87 | vec[i]=::clock(); 88 | } 89 | std::cout << sw.elapsed() << std::endl; 90 | } 91 | 92 | void test2() 93 | { 94 | std::vector vec(size); 95 | perf2(vec); 96 | std::size_t cnt=0; 97 | for (int i=10; i>0; --i) 98 | { 99 | if (vec[i]!=vec[i-1]) ++cnt; 100 | std::cout << vec[i] << " " ; 101 | } 102 | std::cout<< std::endl; 103 | std::cout <<"changes: "<< cnt << std::endl; 104 | } 105 | 106 | void test3() 107 | { 108 | std::vector vec(size); 109 | perf3(vec); 110 | std::size_t cnt=0; 111 | for (int i=10; i>0; --i) 112 | { 113 | if (vec[i]!=vec[i-1]) ++cnt; 114 | std::cout << vec[i] << " " ; 115 | } 116 | std::cout<< std::endl; 117 | std::cout <<"changes: "<< cnt << std::endl; 118 | } 119 | 120 | #endif 121 | 122 | int main() { 123 | 124 | std::cout << "system_clock "; 125 | test(); 126 | #ifdef BOOST_CHRONO_HAS_CLOCK_STEADY 127 | std::cout << "steady_clock " ; 128 | test(); 129 | #endif 130 | std::cout << "high_resolution_clock " ; 131 | test(); 132 | 133 | #if defined(BOOST_CHRONO_HAS_PROCESS_CLOCKS) 134 | std::cout << "process_real_cpu_clock "; 135 | test(); 136 | #if ! BOOST_OS_WINDOWS || BOOST_PLAT_WINDOWS_DESKTOP 137 | std::cout << "process_user_cpu_clock "; 138 | test(); 139 | std::cout << "process_system_cpu_clock " ; 140 | test(); 141 | std::cout << "process_cpu_clock " ; 142 | test(); 143 | #endif 144 | #endif 145 | std::cout << "system_clock "; 146 | test(); 147 | #if 0 148 | std::cout << "times "; 149 | test2(); 150 | std::cout << "clock "; 151 | test3(); 152 | #endif 153 | return 0; 154 | } 155 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto !eol svneol=native#text/plain 2 | *.gitattributes text svneol=native#text/plain 3 | 4 | # Scriptish formats 5 | *.bat text svneol=native#text/plain 6 | *.bsh text svneol=native#text/x-beanshell 7 | *.cgi text svneol=native#text/plain 8 | *.cmd text svneol=native#text/plain 9 | *.js text svneol=native#text/javascript 10 | *.php text svneol=native#text/x-php 11 | *.pl text svneol=native#text/x-perl 12 | *.pm text svneol=native#text/x-perl 13 | *.py text svneol=native#text/x-python 14 | *.sh eol=lf svneol=LF#text/x-sh 15 | configure eol=lf svneol=LF#text/x-sh 16 | 17 | # Image formats 18 | *.bmp binary svneol=unset#image/bmp 19 | *.gif binary svneol=unset#image/gif 20 | *.ico binary svneol=unset#image/ico 21 | *.jpeg binary svneol=unset#image/jpeg 22 | *.jpg binary svneol=unset#image/jpeg 23 | *.png binary svneol=unset#image/png 24 | *.tif binary svneol=unset#image/tiff 25 | *.tiff binary svneol=unset#image/tiff 26 | *.svg text svneol=native#image/svg%2Bxml 27 | 28 | # Data formats 29 | *.pdf binary svneol=unset#application/pdf 30 | *.avi binary svneol=unset#video/avi 31 | *.doc binary svneol=unset#application/msword 32 | *.dsp text svneol=crlf#text/plain 33 | *.dsw text svneol=crlf#text/plain 34 | *.eps binary svneol=unset#application/postscript 35 | *.gz binary svneol=unset#application/gzip 36 | *.mov binary svneol=unset#video/quicktime 37 | *.mp3 binary svneol=unset#audio/mpeg 38 | *.ppt binary svneol=unset#application/vnd.ms-powerpoint 39 | *.ps binary svneol=unset#application/postscript 40 | *.psd binary svneol=unset#application/photoshop 41 | *.rdf binary svneol=unset#text/rdf 42 | *.rss text svneol=unset#text/xml 43 | *.rtf binary svneol=unset#text/rtf 44 | *.sln text svneol=native#text/plain 45 | *.swf binary svneol=unset#application/x-shockwave-flash 46 | *.tgz binary svneol=unset#application/gzip 47 | *.vcproj text svneol=native#text/xml 48 | *.vcxproj text svneol=native#text/xml 49 | *.vsprops text svneol=native#text/xml 50 | *.wav binary svneol=unset#audio/wav 51 | *.xls binary svneol=unset#application/vnd.ms-excel 52 | *.zip binary svneol=unset#application/zip 53 | 54 | # Text formats 55 | .htaccess text svneol=native#text/plain 56 | *.bbk text svneol=native#text/xml 57 | *.cmake text svneol=native#text/plain 58 | *.css text svneol=native#text/css 59 | *.dtd text svneol=native#text/xml 60 | *.htm text svneol=native#text/html 61 | *.html text svneol=native#text/html 62 | *.ini text svneol=native#text/plain 63 | *.log text svneol=native#text/plain 64 | *.mak text svneol=native#text/plain 65 | *.qbk text svneol=native#text/plain 66 | *.rst text svneol=native#text/plain 67 | *.sql text svneol=native#text/x-sql 68 | *.txt text svneol=native#text/plain 69 | *.xhtml text svneol=native#text/xhtml%2Bxml 70 | *.xml text svneol=native#text/xml 71 | *.xsd text svneol=native#text/xml 72 | *.xsl text svneol=native#text/xml 73 | *.xslt text svneol=native#text/xml 74 | *.xul text svneol=native#text/xul 75 | *.yml text svneol=native#text/plain 76 | boost-no-inspect text svneol=native#text/plain 77 | CHANGES text svneol=native#text/plain 78 | COPYING text svneol=native#text/plain 79 | INSTALL text svneol=native#text/plain 80 | Jamfile text svneol=native#text/plain 81 | Jamroot text svneol=native#text/plain 82 | Jamfile.v2 text svneol=native#text/plain 83 | Jamrules text svneol=native#text/plain 84 | Makefile* text svneol=native#text/plain 85 | README text svneol=native#text/plain 86 | TODO text svneol=native#text/plain 87 | 88 | # Code formats 89 | *.c text svneol=native#text/plain 90 | *.cpp text svneol=native#text/plain 91 | *.h text svneol=native#text/plain 92 | *.hpp text svneol=native#text/plain 93 | *.ipp text svneol=native#text/plain 94 | *.tpp text svneol=native#text/plain 95 | *.jam text svneol=native#text/plain 96 | *.java text svneol=native#text/plain 97 | -------------------------------------------------------------------------------- /test/duration/constructor_pass.cpp: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // The LLVM Compiler Infrastructure 4 | // 5 | // This file is dual licensed under the MIT and the University of Illinois Open 6 | // Source Licenses. See LICENSE.TXT for details. 7 | // 8 | //===----------------------------------------------------------------------===// 9 | // Adaptation to Boost of the libcxx 10 | // Copyright 2010 Vicente J. Botet Escriba 11 | // Distributed under the Boost Software License, Version 1.0. 12 | // See http://www.boost.org/LICENSE_1_0.txt 13 | 14 | 15 | #include 16 | #include 17 | 18 | 19 | #include "../rep.h" 20 | #include 21 | 22 | #ifdef BOOST_NO_CXX11_CONSTEXPR 23 | #define BOOST_CONSTEXPR_ASSERT(C) BOOST_TEST(C) 24 | #else 25 | #include 26 | #define BOOST_CONSTEXPR_ASSERT(C) BOOST_STATIC_ASSERT(C) 27 | #endif 28 | 29 | template 30 | void 31 | check_default() 32 | { 33 | { 34 | D d; 35 | BOOST_TEST(d.count() == typename D::rep()); 36 | } 37 | } 38 | template 39 | void 40 | check_constexpr() 41 | { 42 | BOOST_CONSTEXPR D d(0); 43 | BOOST_CONSTEXPR_ASSERT(d.count() == typename D::rep()); 44 | } 45 | 46 | template 47 | void 48 | check_from_rep(R r) 49 | { 50 | { 51 | D d(r); 52 | BOOST_TEST(d.count() == r); 53 | } 54 | } 55 | 56 | int main() 57 | { 58 | // exact conversions allowed for integral reps 59 | { 60 | boost::chrono::milliseconds ms(1); 61 | boost::chrono::microseconds us = ms; 62 | BOOST_TEST(us.count() == 1000); 63 | { 64 | BOOST_CONSTEXPR boost::chrono::milliseconds ms(1); 65 | BOOST_CONSTEXPR boost::chrono::microseconds us = ms; 66 | BOOST_CONSTEXPR_ASSERT(us.count() == 1000); 67 | } 68 | } 69 | // inexact conversions allowed for floating point reps 70 | { 71 | boost::chrono::duration us(1); 72 | boost::chrono::duration ms = us; 73 | BOOST_TEST(ms.count() == 1./1000); 74 | { 75 | BOOST_CONSTEXPR boost::chrono::duration us(1); 76 | BOOST_CONSTEXPR boost::chrono::duration ms = us; 77 | BOOST_CONSTEXPR_ASSERT(ms.count() == 1./1000); 78 | } 79 | } 80 | // Convert int to float 81 | { 82 | boost::chrono::duration i(3); 83 | boost::chrono::duration d = i; 84 | BOOST_TEST(d.count() == 3); 85 | { 86 | BOOST_CONSTEXPR boost::chrono::duration i(3); 87 | BOOST_CONSTEXPR boost::chrono::duration d = i; 88 | BOOST_CONSTEXPR_ASSERT(d.count() == 3); 89 | } 90 | } 91 | // default constructor 92 | { 93 | check_default >(); 94 | } 95 | { 96 | check_constexpr >(); 97 | } 98 | // constructor from rep 99 | { 100 | check_from_rep >(5); 101 | { 102 | BOOST_CONSTEXPR boost::chrono::duration d(5); 103 | BOOST_CONSTEXPR_ASSERT(d.count() == 5); 104 | } 105 | check_from_rep > >(5); 106 | { 107 | BOOST_CONSTEXPR boost::chrono::duration > d(5); 108 | BOOST_CONSTEXPR_ASSERT(d.count() == 5); 109 | } 110 | check_from_rep > >(Rep(3)); 111 | { 112 | BOOST_CONSTEXPR boost::chrono::duration > d(Rep(3)); 113 | BOOST_CONSTEXPR_ASSERT(d.count() == Rep(3)); 114 | } 115 | check_from_rep > >(5.5); 116 | { 117 | BOOST_CONSTEXPR boost::chrono::duration > d(5.5); 118 | BOOST_CONSTEXPR_ASSERT(d.count() == 5.5); 119 | } 120 | 121 | 122 | } 123 | // constructor from other rep 124 | { 125 | boost::chrono::duration d(5); 126 | BOOST_TEST(d.count() == 5); 127 | { 128 | BOOST_CONSTEXPR boost::chrono::duration d(5); 129 | BOOST_CONSTEXPR_ASSERT(d.count() == 5); 130 | } 131 | } 132 | 133 | return boost::report_errors(); 134 | } 135 | -------------------------------------------------------------------------------- /perf/Jamfile.v2: -------------------------------------------------------------------------------- 1 | # Boost Chrono Library test Jamfile 2 | 3 | # Copyright Beman Dawes 2008 4 | # Copyright Vicente J. Botet Escriba 2009-2010 5 | 6 | # Distributed under the Boost Software License, Version 1.0. 7 | # See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt 8 | 9 | # See library home page at http://www.boost.org/libs/chrono 10 | 11 | import os ; 12 | import feature ; 13 | 14 | project 15 | : requirements 16 | freebsd:"-lrt" 17 | linux:"-lrt" 18 | pgi:"-lrt" 19 | #single:BOOST_CHRONO_THREAD_DISABLED 20 | msvc:on 21 | BOOST_CHRONO_USES_MPL_ASSERT 22 | sun:BOOST_TYPEOF_EMULATION 23 | all 24 | gcc:-Wextra 25 | gcc:-pedantic 26 | gcc:-Wno-long-long 27 | darwin:-Wextra 28 | darwin:-pedantic 29 | darwin:-Wno-long-long 30 | #pathscale:-Wextra 31 | pathscale:-Wno-long-long 32 | pathscale:-pedantic 33 | clang:-Wextra 34 | clang:-pedantic 35 | clang:-Wno-long-long 36 | gcc-4.5.0,windows:-Wno-missing-field-initializers 37 | gcc-4.5.0,windows:-fdiagnostics-show-option 38 | msvc:/wd4127 39 | # Note: Some of the remarks from the Intel compiler are disabled 40 | # remark #304: access control not specified ("public" by default) 41 | # remark #383: value copied to temporary, reference to temporary used 42 | # remark #1418: external function definition with no prior declaration 43 | intel:-wd304,383,1418 44 | BOOST_CHRONO_VERSION=2 45 | . 46 | ; 47 | 48 | rule chrono-run ( sources ) 49 | { 50 | return 51 | [ run $(sources) ../build//boost_chrono 52 | : : 53 | : 54 | : $(sources[1]:B)_shared ] 55 | [ run $(sources) ../build//boost_chrono/static 56 | : : 57 | : 58 | : $(sources[1]:B)_static ] 59 | [ run $(sources) 60 | : : 61 | : BOOST_CHRONO_HEADER_ONLY 62 | : $(sources[1]:B)_header ] 63 | ; 64 | } 65 | rule chrono-run2 ( sources : name ) 66 | { 67 | return 68 | [ run $(sources) ../build//boost_chrono 69 | : : 70 | : 71 | : $(name)_shared ] 72 | [ run $(sources) ../build//boost_chrono/static 73 | : : 74 | : 75 | : $(name)_static ] 76 | [ run $(sources) 77 | : : 78 | : BOOST_CHRONO_HEADER_ONLY 79 | : $(name)_header ] 80 | ; 81 | } 82 | 83 | 84 | rule chrono-run-mt ( sources ) 85 | { 86 | return 87 | [ run $(sources) ../build//boost_chrono 88 | : : 89 | : 90 | : $(sources[1]:B)_shared ] 91 | [ run $(sources) ../build//boost_chrono/static 92 | : : 93 | : 94 | : $(sources[1]:B)_static ] 95 | [ run $(sources) 96 | : : 97 | : BOOST_CHRONO_HEADER_ONLY 98 | : $(sources[1]:B)_header ] 99 | ; 100 | } 101 | rule chrono-run2-mt ( sources : name ) 102 | { 103 | return 104 | [ run $(sources) ../build//boost_chrono 105 | : : 106 | : 107 | /boost/system//boost_system 108 | : $(name)_shared ] 109 | [ run $(sources) ../build//boost_chrono/static 110 | : : 111 | : 112 | /boost/system//boost_system 113 | : $(name)_static ] 114 | [ run $(sources) 115 | : : 116 | : BOOST_CHRONO_HEADER_ONLY 117 | : $(name)_header ] 118 | ; 119 | } 120 | rule chrono-compile ( sources ) 121 | { 122 | return 123 | [ compile $(sources) 124 | : 125 | : $(sources[1]:B)_lib ] 126 | [ compile $(sources) 127 | : BOOST_CHRONO_HEADER_ONLY 128 | : $(sources[1]:B)_header ] 129 | ; 130 | } 131 | 132 | rule chrono-compile2 ( sources : name ) 133 | { 134 | return 135 | [ compile $(sources) 136 | : 137 | : $(name)_lib ] 138 | [ compile $(sources) 139 | : BOOST_CHRONO_HEADER_ONLY 140 | : $(name)_header ] 141 | ; 142 | } 143 | 144 | test-suite "perf" 145 | : 146 | [ chrono-run store_now_in_vector.cpp ] 147 | ; 148 | 149 | -------------------------------------------------------------------------------- /test/time_point/comparisons_pass.cpp: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // The LLVM Compiler Infrastructure 4 | // 5 | // This file is dual licensed under the MIT and the University of Illinois Open 6 | // Source Licenses. See LICENSE.TXT for details. 7 | // 8 | //===----------------------------------------------------------------------===// 9 | // Adaptation to Boost of the libcxx 10 | // Copyright 2010 Vicente J. Botet Escriba 11 | // Distributed under the Boost Software License, Version 1.0. 12 | // See http://www.boost.org/LICENSE_1_0.txt 13 | 14 | #include 15 | #include 16 | #ifdef BOOST_NO_CXX11_CONSTEXPR 17 | #define BOOST_CONSTEXPR_ASSERT(C) BOOST_TEST(C) 18 | #else 19 | #include 20 | #define BOOST_CONSTEXPR_ASSERT(C) BOOST_STATIC_ASSERT(C) 21 | #endif 22 | 23 | 24 | int main() 25 | { 26 | typedef boost::chrono::system_clock Clock; 27 | typedef boost::chrono::milliseconds Duration1; 28 | typedef boost::chrono::microseconds Duration2; 29 | typedef boost::chrono::time_point T1; 30 | typedef boost::chrono::time_point T2; 31 | 32 | { 33 | T1 t1(Duration1(3)); 34 | T1 t2(Duration1(3)); 35 | BOOST_TEST( (t1 == t2)); 36 | BOOST_TEST(! (t1 != t2)); 37 | } 38 | { 39 | BOOST_CONSTEXPR T1 t1(Duration1(3)); 40 | BOOST_CONSTEXPR T1 t2(Duration1(3)); 41 | BOOST_CONSTEXPR_ASSERT( (t1 == t2)); 42 | BOOST_CONSTEXPR_ASSERT(! (t1 != t2)); 43 | } 44 | { 45 | T1 t1(Duration1(3)); 46 | T1 t2(Duration1(4)); 47 | BOOST_TEST(! (t1 == t2)); 48 | BOOST_TEST( (t1 != t2)); 49 | } 50 | { 51 | BOOST_CONSTEXPR T1 t1(Duration1(3)); 52 | BOOST_CONSTEXPR T1 t2(Duration1(4)); 53 | BOOST_CONSTEXPR_ASSERT(! (t1 == t2)); 54 | BOOST_CONSTEXPR_ASSERT( (t1 != t2)); 55 | } 56 | { 57 | T1 t1(Duration1(3)); 58 | T2 t2(Duration2(3000)); 59 | BOOST_TEST( (t1 == t2)); 60 | BOOST_TEST(! (t1 != t2)); 61 | } 62 | { 63 | BOOST_CONSTEXPR T1 t1(Duration1(3)); 64 | BOOST_CONSTEXPR T2 t2(Duration2(3000)); 65 | BOOST_CONSTEXPR_ASSERT( (t1 == t2)); 66 | BOOST_CONSTEXPR_ASSERT(! (t1 != t2)); 67 | } 68 | { 69 | T1 t1(Duration1(3)); 70 | T2 t2(Duration2(3001)); 71 | BOOST_TEST(! (t1 == t2)); 72 | BOOST_TEST( (t1 != t2)); 73 | } 74 | { 75 | BOOST_CONSTEXPR T1 t1(Duration1(3)); 76 | BOOST_CONSTEXPR T2 t2(Duration2(3001)); 77 | BOOST_CONSTEXPR_ASSERT(! (t1 == t2)); 78 | BOOST_CONSTEXPR_ASSERT( (t1 != t2)); 79 | } 80 | { 81 | T1 t1(Duration1(3)); 82 | T1 t2(Duration1(3)); 83 | BOOST_TEST(! (t1 < t2)); 84 | BOOST_TEST(! (t1 > t2)); 85 | BOOST_TEST( (t1 <= t2)); 86 | BOOST_TEST( (t1 >= t2)); 87 | } 88 | { 89 | BOOST_CONSTEXPR T1 t1(Duration1(3)); 90 | BOOST_CONSTEXPR T1 t2(Duration1(3)); 91 | BOOST_CONSTEXPR_ASSERT(! (t1 < t2)); 92 | BOOST_CONSTEXPR_ASSERT(! (t1 > t2)); 93 | BOOST_CONSTEXPR_ASSERT( (t1 <= t2)); 94 | BOOST_CONSTEXPR_ASSERT( (t1 >= t2)); 95 | } 96 | { 97 | T1 t1(Duration1(3)); 98 | T1 t2(Duration1(4)); 99 | BOOST_TEST( (t1 < t2)); 100 | BOOST_TEST(! (t1 > t2)); 101 | BOOST_TEST( (t1 <= t2)); 102 | BOOST_TEST(! (t1 >= t2)); 103 | } 104 | { 105 | BOOST_CONSTEXPR T1 t1(Duration1(3)); 106 | BOOST_CONSTEXPR T1 t2(Duration1(4)); 107 | BOOST_CONSTEXPR_ASSERT( (t1 < t2)); 108 | BOOST_CONSTEXPR_ASSERT(! (t1 > t2)); 109 | BOOST_CONSTEXPR_ASSERT( (t1 <= t2)); 110 | BOOST_CONSTEXPR_ASSERT(! (t1 >= t2)); 111 | } 112 | { 113 | T1 t1(Duration1(3)); 114 | T2 t2(Duration2(3000)); 115 | BOOST_TEST(! (t1 < t2)); 116 | BOOST_TEST(! (t1 > t2)); 117 | BOOST_TEST( (t1 <= t2)); 118 | BOOST_TEST( (t1 >= t2)); 119 | } 120 | { 121 | BOOST_CONSTEXPR T1 t1(Duration1(3)); 122 | BOOST_CONSTEXPR T2 t2(Duration2(3000)); 123 | BOOST_CONSTEXPR_ASSERT(! (t1 < t2)); 124 | BOOST_CONSTEXPR_ASSERT(! (t1 > t2)); 125 | BOOST_CONSTEXPR_ASSERT( (t1 <= t2)); 126 | BOOST_CONSTEXPR_ASSERT( (t1 >= t2)); 127 | } 128 | { 129 | T1 t1(Duration1(3)); 130 | T2 t2(Duration2(3001)); 131 | BOOST_TEST( (t1 < t2)); 132 | BOOST_TEST(! (t1 > t2)); 133 | BOOST_TEST( (t1 <= t2)); 134 | BOOST_TEST(! (t1 >= t2)); 135 | } 136 | { 137 | BOOST_CONSTEXPR T1 t1(Duration1(3)); 138 | BOOST_CONSTEXPR T2 t2(Duration2(3001)); 139 | BOOST_CONSTEXPR_ASSERT( (t1 < t2)); 140 | BOOST_CONSTEXPR_ASSERT(! (t1 > t2)); 141 | BOOST_CONSTEXPR_ASSERT( (t1 <= t2)); 142 | BOOST_CONSTEXPR_ASSERT(! (t1 >= t2)); 143 | } 144 | 145 | return boost::report_errors(); 146 | } 147 | -------------------------------------------------------------------------------- /include/boost/chrono/detail/inlined/posix/chrono.hpp: -------------------------------------------------------------------------------- 1 | // posix/chrono.cpp --------------------------------------------------------------// 2 | 3 | // Copyright Beman Dawes 2008 4 | // Copyright Vicente J. Botet Escriba 2009 5 | 6 | // Distributed under the Boost Software License, Version 1.0. 7 | // See http://www.boost.org/LICENSE_1_0.txt 8 | 9 | //----------------------------------------------------------------------------// 10 | // POSIX // 11 | //----------------------------------------------------------------------------// 12 | 13 | #include // for clock_gettime 14 | #include 15 | #include 16 | 17 | namespace boost 18 | { 19 | namespace chrono 20 | { 21 | 22 | system_clock::time_point system_clock::now() BOOST_NOEXCEPT 23 | { 24 | timespec ts; 25 | if ( ::clock_gettime( CLOCK_REALTIME, &ts ) ) 26 | { 27 | BOOST_ASSERT(0 && "Boost::Chrono - Internal Error"); 28 | } 29 | 30 | return time_point(duration( 31 | static_cast( ts.tv_sec ) * 1000000000 + ts.tv_nsec)); 32 | } 33 | 34 | #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING 35 | system_clock::time_point system_clock::now(system::error_code & ec) 36 | { 37 | timespec ts; 38 | if ( ::clock_gettime( CLOCK_REALTIME, &ts ) ) 39 | { 40 | if (::boost::chrono::is_throws(ec)) 41 | { 42 | boost::throw_exception( 43 | system::system_error( 44 | errno, 45 | ::boost::system::system_category(), 46 | "chrono::system_clock" )); 47 | } 48 | else 49 | { 50 | ec.assign( errno, ::boost::system::system_category() ); 51 | return time_point(); 52 | } 53 | } 54 | 55 | if (!::boost::chrono::is_throws(ec)) 56 | { 57 | ec.clear(); 58 | } 59 | return time_point(duration( 60 | static_cast( ts.tv_sec ) * 1000000000 + ts.tv_nsec)); 61 | } 62 | #endif 63 | 64 | std::time_t system_clock::to_time_t(const system_clock::time_point& t) BOOST_NOEXCEPT 65 | { 66 | return static_cast( t.time_since_epoch().count() / 1000000000 ); 67 | } 68 | 69 | system_clock::time_point system_clock::from_time_t(std::time_t t) BOOST_NOEXCEPT 70 | { 71 | return time_point(duration(static_cast(t) * 1000000000)); 72 | } 73 | 74 | #ifdef BOOST_CHRONO_HAS_CLOCK_STEADY 75 | 76 | steady_clock::time_point steady_clock::now() BOOST_NOEXCEPT 77 | { 78 | timespec ts; 79 | #if BOOST_OS_CYGWIN 80 | // lack of thread safety in high resolution timer initialization 81 | // can lead to a timespec of zero without an error; was reported 82 | // to the cygwin mailing list and can be removed once fixed 83 | do 84 | { 85 | #endif 86 | if ( ::clock_gettime( CLOCK_MONOTONIC, &ts ) ) 87 | { 88 | BOOST_ASSERT(0 && "Boost::Chrono - Internal Error"); 89 | } 90 | #if BOOST_OS_CYGWIN 91 | } while (ts.tv_sec == 0 && ts.tv_nsec == 0); 92 | #endif 93 | return time_point(duration( 94 | static_cast( ts.tv_sec ) * 1000000000 + ts.tv_nsec)); 95 | } 96 | 97 | #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING 98 | steady_clock::time_point steady_clock::now(system::error_code & ec) 99 | { 100 | timespec ts; 101 | #if BOOST_OS_CYGWIN 102 | // lack of thread safety in high resolution timer initialization 103 | // can lead to a timespec of zero without an error; was reported 104 | // to the cygwin mailing list and can be removed once fixed 105 | do 106 | { 107 | #endif 108 | if ( ::clock_gettime( CLOCK_MONOTONIC, &ts ) ) 109 | { 110 | if (::boost::chrono::is_throws(ec)) 111 | { 112 | boost::throw_exception( 113 | system::system_error( 114 | errno, 115 | ::boost::system::system_category(), 116 | "chrono::steady_clock" )); 117 | } 118 | else 119 | { 120 | ec.assign( errno, ::boost::system::system_category() ); 121 | return time_point(); 122 | } 123 | } 124 | #if BOOST_OS_CYGWIN 125 | } while (ts.tv_sec == 0 && ts.tv_nsec == 0); 126 | #endif 127 | 128 | if (!::boost::chrono::is_throws(ec)) 129 | { 130 | ec.clear(); 131 | } 132 | return time_point(duration( 133 | static_cast( ts.tv_sec ) * 1000000000 + ts.tv_nsec)); 134 | } 135 | #endif 136 | #endif 137 | 138 | } // namespace chrono 139 | } // namespace boost 140 | 141 | 142 | -------------------------------------------------------------------------------- /example/french.cpp: -------------------------------------------------------------------------------- 1 | // french.cpp ----------------------------------------------------------// 2 | 3 | // Copyright 2010 Howard Hinnant 4 | // Copyright 2011 Vicente J. Botet Escriba 5 | // Distributed under the Boost Software License, Version 1.0. 6 | // See http://www.boost.org/LICENSE_1_0.txt 7 | 8 | // Adapted to Boost from the original Hawards's code 9 | 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | 19 | #if BOOST_CHRONO_VERSION==2 20 | #include 21 | 22 | using namespace boost; 23 | using namespace boost::chrono; 24 | 25 | template 26 | class duration_units_fr: public duration_units_default 27 | { 28 | public: 29 | typedef CharT char_type; 30 | 31 | explicit duration_units_fr(size_t refs = 0) : 32 | duration_units_default(refs) 33 | { 34 | } 35 | protected: 36 | 37 | using duration_units_default::do_get_unit; 38 | std::size_t do_get_plural_form(boost::int_least64_t value) const 39 | { 40 | return (value == -1 || value == 0 || value == 1) ? 0 : 1; 41 | } 42 | 43 | std::basic_string do_get_unit(duration_style style, ratio<1> , std::size_t pf) const 44 | { 45 | static const CharT t[] = 46 | { 's' }; 47 | static const std::basic_string symbol(t, t + sizeof (t) / sizeof (t[0])); 48 | static const CharT u[] = 49 | { 's', 'e', 'c', 'o', 'n', 'd', 'e' }; 50 | static const std::basic_string singular(u, u + sizeof (u) / sizeof (u[0])); 51 | static const CharT v[] = 52 | { 's', 'e', 'c', 'o', 'n', 'd', 'e', 's' }; 53 | static const std::basic_string plural(v, v + sizeof (v) / sizeof (v[0])); 54 | 55 | if (style == duration_style::symbol) return symbol; 56 | if (pf == 0) return singular; 57 | if (pf == 1) return plural; 58 | // assert 59 | //throw "exception"; 60 | return ""; 61 | } 62 | 63 | std::basic_string do_get_unit(duration_style style, ratio<60> , std::size_t pf) const 64 | { 65 | static const CharT t[] = 66 | { 'm', 'i', 'n' }; 67 | static const std::basic_string symbol(t, t + sizeof (t) / sizeof (t[0])); 68 | 69 | static const CharT u[] = 70 | { 'm', 'i', 'n', 'u', 't', 'e' }; 71 | static const std::basic_string singular(u, u + sizeof (u) / sizeof (u[0])); 72 | static const CharT v[] = 73 | { 'm', 'i', 'n', 'u', 't', 'e', 's' }; 74 | static const std::basic_string plural(v, v + sizeof (v) / sizeof (v[0])); 75 | 76 | if (style == duration_style::symbol) return symbol; 77 | if (pf == 0) return singular; 78 | if (pf == 1) return plural; 79 | // assert 80 | //throw "exception"; 81 | return ""; 82 | } 83 | 84 | std::basic_string do_get_unit(duration_style style, ratio<3600> , std::size_t pf) const 85 | { 86 | static const CharT t[] = 87 | { 'h' }; 88 | static const std::basic_string symbol(t, t + sizeof (t) / sizeof (t[0])); 89 | static const CharT u[] = 90 | { 'h', 'e', 'u', 'r', 'e' }; 91 | static const std::basic_string singular(u, u + sizeof (u) / sizeof (u[0])); 92 | static const CharT v[] = 93 | { 'h', 'e', 'u', 'r', 'e', 's' }; 94 | static const std::basic_string plural(v, v + sizeof (v) / sizeof (v[0])); 95 | 96 | if (style == duration_style::symbol) return symbol; 97 | if (pf == 0) return singular; 98 | if (pf == 1) return plural; 99 | // assert 100 | //throw "exception"; 101 | return ""; 102 | } 103 | }; 104 | #endif 105 | 106 | int main() 107 | { 108 | using std::cout; 109 | using std::locale; 110 | using namespace boost; 111 | using namespace boost::chrono; 112 | 113 | #if BOOST_CHRONO_VERSION==2 114 | cout.imbue(locale(locale(), new duration_units_fr<>())); 115 | #else 116 | cout.imbue(locale(locale(), new duration_punct 117 | ( 118 | duration_punct::use_long, 119 | "secondes", "minutes", "heures", 120 | "s", "m", "h" 121 | ))); 122 | #endif 123 | hours h(5); 124 | minutes m(45); 125 | seconds s(15); 126 | milliseconds ms(763); 127 | cout << h << ", " << m << ", " << s << " et " << ms << '\n'; 128 | cout << hours(0) << ", " << minutes(0) << ", " << s << " et " << ms << '\n'; 129 | return 0; 130 | } 131 | -------------------------------------------------------------------------------- /include/boost/chrono/io/ios_base_state.hpp: -------------------------------------------------------------------------------- 1 | // (C) Copyright 2011 Vicente J. Botet Escriba 2 | // Use, modification and distribution are subject to the Boost Software License, 3 | // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at 4 | // http://www.boost.org/LICENSE_1_0.txt). 5 | // 6 | // This code was adapted by Vicente from Howard Hinnant's experimental work 7 | // on chrono i/o to Boost 8 | 9 | #ifndef BOOST_CHRONO_IO_IOS_BASE_STATE_HPP 10 | #define BOOST_CHRONO_IO_IOS_BASE_STATE_HPP 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | namespace boost 19 | { 20 | namespace chrono 21 | { 22 | 23 | class fmt_masks : public ios_flags 24 | { 25 | typedef ios_flags base_type; 26 | fmt_masks& operator=(fmt_masks const& rhs) ; 27 | 28 | public: 29 | fmt_masks(std::ios_base& ios): base_type(ios) {} 30 | enum type 31 | { 32 | uses_symbol = 1 << 0, 33 | uses_local = 1 << 1 34 | }; 35 | 36 | inline duration_style get_duration_style() 37 | { 38 | return (flags() & uses_symbol) ? duration_style::symbol : duration_style::prefix; 39 | } 40 | inline void set_duration_style(duration_style style) 41 | { 42 | if (style == duration_style::symbol) 43 | setf(uses_symbol); 44 | else 45 | unsetf(uses_symbol); 46 | } 47 | 48 | inline timezone get_timezone() 49 | { 50 | return (flags() & uses_local) ? timezone::local : timezone::utc; 51 | } 52 | inline void set_timezone(timezone tz) 53 | { 54 | if (tz == timezone::local) 55 | setf(uses_local); 56 | else 57 | unsetf(uses_local); 58 | } 59 | }; 60 | namespace detail 61 | { 62 | namespace /**/ { 63 | xalloc_key_initializer fmt_masks_xalloc_key_initializer; 64 | } // namespace 65 | } // namespace detail 66 | 67 | inline duration_style get_duration_style(std::ios_base & ios) 68 | { 69 | return fmt_masks(ios).get_duration_style(); 70 | } 71 | inline void set_duration_style(std::ios_base& ios, duration_style style) 72 | { 73 | fmt_masks(ios).set_duration_style(style); 74 | } 75 | inline std::ios_base& symbol_format(std::ios_base& ios) 76 | { 77 | fmt_masks(ios).setf(fmt_masks::uses_symbol); 78 | return ios; 79 | } 80 | inline std::ios_base& name_format(std::ios_base& ios) 81 | { 82 | fmt_masks(ios).unsetf(fmt_masks::uses_symbol); 83 | return ios; 84 | } 85 | 86 | inline timezone get_timezone(std::ios_base & ios) 87 | { 88 | return fmt_masks(ios).get_timezone(); 89 | } 90 | inline void set_timezone(std::ios_base& ios, timezone tz) 91 | { 92 | fmt_masks(ios).set_timezone(tz); 93 | } 94 | inline std::ios_base& local_timezone(std::ios_base& ios) 95 | { 96 | fmt_masks(ios).setf(fmt_masks::uses_local); 97 | return ios; 98 | } 99 | 100 | inline std::ios_base& utc_timezone(std::ios_base& ios) 101 | { 102 | fmt_masks(ios).unsetf(fmt_masks::uses_local); 103 | return ios; 104 | } 105 | 106 | namespace detail 107 | { 108 | 109 | template 110 | struct ios_base_data_aux 111 | { 112 | std::basic_string time_fmt; 113 | std::basic_string duration_fmt; 114 | public: 115 | 116 | ios_base_data_aux() 117 | //: 118 | // time_fmt(""), 119 | // duration_fmt("") 120 | { 121 | } 122 | }; 123 | template 124 | struct ios_base_data {}; 125 | namespace /**/ { 126 | xalloc_key_initializer > ios_base_data_aux_xalloc_key_initializer; 127 | xalloc_key_initializer > wios_base_data_aux_xalloc_key_initializer; 128 | #if BOOST_CHRONO_HAS_UNICODE_SUPPORT 129 | xalloc_key_initializer > u16ios_base_data_aux_xalloc_key_initializer; 130 | xalloc_key_initializer > u32ios_base_data_aux_xalloc_key_initializer; 131 | #endif 132 | } // namespace 133 | } // namespace detail 134 | 135 | template 136 | inline std::basic_string get_time_fmt(std::ios_base & ios) 137 | { 138 | ios_state_not_null_ptr, detail::ios_base_data_aux > ptr(ios); 139 | return ptr->time_fmt; 140 | } 141 | template 142 | inline void set_time_fmt(std::ios_base& ios, std::basic_string< 143 | CharT> const& fmt) 144 | { 145 | ios_state_not_null_ptr, detail::ios_base_data_aux > ptr(ios); 146 | ptr->time_fmt = fmt; 147 | } 148 | 149 | } // chrono 150 | } // boost 151 | 152 | #endif // header 153 | -------------------------------------------------------------------------------- /test/duration/rounding_pass.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2013 Krzysztof Czainski 2 | // Copyright 2013 Vicente J. Botet Escriba 3 | // Distributed under the Boost Software License, Version 1.0. 4 | // (See accompanying file LICENSE_1_0.txt or copy at 5 | // http://www.boost.org/LICENSE_1_0.txt) 6 | 7 | /* 8 | * @file chrono_rounding.cpp 9 | * 10 | * @since 2013-11-22 11 | * @author Krzysztof Czainski <1czajnik@gmail.com> 12 | */ 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | #include 20 | #include 21 | 22 | using namespace boost::chrono; 23 | 24 | void test_floor() 25 | { 26 | BOOST_TEST_EQ( seconds(-2), floor( milliseconds(-2000) ) ); 27 | BOOST_TEST_EQ( seconds(-2), floor( milliseconds(-1999) ) ); 28 | BOOST_TEST_EQ( seconds(-2), floor( milliseconds(-1001) ) ); 29 | BOOST_TEST_EQ( seconds(-1), floor( milliseconds(-1000) ) ); 30 | BOOST_TEST_EQ( seconds(-1), floor( milliseconds(-999) ) ); 31 | BOOST_TEST_EQ( seconds(-1), floor( milliseconds(-1) ) ); 32 | BOOST_TEST_EQ( seconds(0), floor( milliseconds(0) ) ); 33 | BOOST_TEST_EQ( seconds(0), floor( milliseconds(1) ) ); 34 | BOOST_TEST_EQ( seconds(0), floor( milliseconds(999) ) ); 35 | BOOST_TEST_EQ( seconds(1), floor( milliseconds(1000) ) ); 36 | BOOST_TEST_EQ( seconds(1), floor( milliseconds(1001) ) ); 37 | BOOST_TEST_EQ( seconds(1), floor( milliseconds(1999) ) ); 38 | BOOST_TEST_EQ( seconds(2), floor( milliseconds(2000) ) ); 39 | 40 | { 41 | // check that int32 isn't overflowed in intermediate calculations: 42 | typedef duration sec32; 43 | typedef duration< boost::int32_t, boost::ratio<999,1000> > sec32_m1ms; 44 | BOOST_TEST_EQ( sec32(-999000000), floor( sec32_m1ms(-1000000000) ) ); 45 | BOOST_TEST_EQ( sec32( 999000000), floor( sec32_m1ms( 1000000000) ) ); 46 | } 47 | } 48 | 49 | void test_ceil() 50 | { 51 | BOOST_TEST_EQ( seconds(-2), ceil( milliseconds(-2000) ) ); 52 | BOOST_TEST_EQ( seconds(-1), ceil( milliseconds(-1999) ) ); 53 | BOOST_TEST_EQ( seconds(-1), ceil( milliseconds(-1001) ) ); 54 | BOOST_TEST_EQ( seconds(-1), ceil( milliseconds(-1000) ) ); 55 | BOOST_TEST_EQ( seconds(0), ceil( milliseconds(-999) ) ); 56 | BOOST_TEST_EQ( seconds(0), ceil( milliseconds(-1) ) ); 57 | BOOST_TEST_EQ( seconds(0), ceil( milliseconds(0) ) ); 58 | BOOST_TEST_EQ( seconds(1), ceil( milliseconds(1) ) ); 59 | BOOST_TEST_EQ( seconds(1), ceil( milliseconds(999) ) ); 60 | BOOST_TEST_EQ( seconds(1), ceil( milliseconds(1000) ) ); 61 | BOOST_TEST_EQ( seconds(2), ceil( milliseconds(1001) ) ); 62 | BOOST_TEST_EQ( seconds(2), ceil( milliseconds(1999) ) ); 63 | BOOST_TEST_EQ( seconds(2), ceil( milliseconds(2000) ) ); 64 | { 65 | // check that int32 isn't overflowed in intermediate calculations: 66 | typedef duration sec32; 67 | typedef duration< boost::int32_t, boost::ratio<999,1000> > sec32_m1ms; 68 | BOOST_TEST_EQ( sec32(-999000000), ceil( sec32_m1ms(-1000000000) ) ); 69 | BOOST_TEST_EQ( sec32( 999000000), ceil( sec32_m1ms( 1000000000) ) ); 70 | } 71 | } 72 | 73 | void test_round() 74 | { 75 | // to even on tie 76 | BOOST_TEST_EQ( seconds(-2), round( milliseconds(-2000) ) ); 77 | BOOST_TEST_EQ( seconds(-2), round( milliseconds(-1500) ) ); 78 | BOOST_TEST_EQ( seconds(-1), round( milliseconds(-1499) ) ); 79 | BOOST_TEST_EQ( seconds(-1), round( milliseconds(-1000) ) ); 80 | BOOST_TEST_EQ( seconds(0), round( milliseconds(-500) ) ); 81 | BOOST_TEST_EQ( seconds(0), round( milliseconds(-499) ) ); 82 | BOOST_TEST_EQ( seconds(0), round( milliseconds(0) ) ); 83 | BOOST_TEST_EQ( seconds(0), round( milliseconds(499) ) ); 84 | BOOST_TEST_EQ( seconds(0), round( milliseconds(500) ) ); 85 | BOOST_TEST_EQ( seconds(1), round( milliseconds(1000) ) ); 86 | BOOST_TEST_EQ( seconds(1), round( milliseconds(1499) ) ); 87 | BOOST_TEST_EQ( seconds(2), round( milliseconds(1500) ) ); 88 | BOOST_TEST_EQ( seconds(2), round( milliseconds(2000) ) ); 89 | { 90 | // check that int32 isn't overflowed in intermediate calculations: 91 | typedef duration sec32; 92 | typedef duration< boost::int32_t, boost::ratio<999,1000> > sec32_m1ms; 93 | BOOST_TEST_EQ( sec32(-999000000), round( sec32_m1ms(-1000000000) ) ); 94 | BOOST_TEST_EQ( sec32( 999000000), round( sec32_m1ms( 1000000000) ) ); 95 | } 96 | } 97 | 98 | int main() 99 | { 100 | test_floor(); 101 | test_ceil(); 102 | test_round(); 103 | return boost::report_errors(); 104 | 105 | } 106 | -------------------------------------------------------------------------------- /include/boost/chrono/detail/inlined/win/chrono.hpp: -------------------------------------------------------------------------------- 1 | // win/chrono.cpp --------------------------------------------------------------// 2 | 3 | // Copyright Beman Dawes 2008 4 | // Copyright 2009-2010 Vicente J. Botet Escriba 5 | 6 | // Distributed under the Boost Software License, Version 1.0. 7 | // See http://www.boost.org/LICENSE_1_0.txt 8 | 9 | //----------------------------------------------------------------------------// 10 | // Windows // 11 | //----------------------------------------------------------------------------// 12 | #ifndef BOOST_CHRONO_DETAIL_INLINED_WIN_CHRONO_HPP 13 | #define BOOST_CHRONO_DETAIL_INLINED_WIN_CHRONO_HPP 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | namespace boost 22 | { 23 | namespace chrono 24 | { 25 | namespace chrono_detail 26 | { 27 | 28 | BOOST_CHRONO_INLINE double get_nanosecs_per_tic() BOOST_NOEXCEPT 29 | { 30 | boost::winapi::LARGE_INTEGER_ freq; 31 | if ( !boost::winapi::QueryPerformanceFrequency( &freq ) ) 32 | return 0.0L; 33 | return double(1000000000.0L / freq.QuadPart); 34 | } 35 | 36 | } 37 | 38 | steady_clock::time_point steady_clock::now() BOOST_NOEXCEPT 39 | { 40 | double nanosecs_per_tic = chrono_detail::get_nanosecs_per_tic(); 41 | 42 | boost::winapi::LARGE_INTEGER_ pcount; 43 | if ( nanosecs_per_tic <= 0.0L ) 44 | { 45 | BOOST_ASSERT(0 && "Boost::Chrono - get_nanosecs_per_tic Internal Error"); 46 | return steady_clock::time_point(); 47 | } 48 | unsigned times=0; 49 | while ( ! boost::winapi::QueryPerformanceCounter( &pcount ) ) 50 | { 51 | if ( ++times > 3 ) 52 | { 53 | BOOST_ASSERT(0 && "Boost::Chrono - QueryPerformanceCounter Internal Error"); 54 | return steady_clock::time_point(); 55 | } 56 | } 57 | 58 | return steady_clock::time_point(steady_clock::duration( 59 | static_cast((nanosecs_per_tic) * pcount.QuadPart))); 60 | } 61 | 62 | 63 | #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING 64 | steady_clock::time_point steady_clock::now( system::error_code & ec ) 65 | { 66 | double nanosecs_per_tic = chrono_detail::get_nanosecs_per_tic(); 67 | 68 | boost::winapi::LARGE_INTEGER_ pcount; 69 | if ( (nanosecs_per_tic <= 0.0L) 70 | || (!boost::winapi::QueryPerformanceCounter( &pcount )) ) 71 | { 72 | boost::winapi::DWORD_ cause = 73 | ((nanosecs_per_tic <= 0.0L) 74 | ? boost::winapi::ERROR_NOT_SUPPORTED_ 75 | : boost::winapi::GetLastError()); 76 | if (::boost::chrono::is_throws(ec)) { 77 | boost::throw_exception( 78 | system::system_error( 79 | cause, 80 | ::boost::system::system_category(), 81 | "chrono::steady_clock" )); 82 | } 83 | else 84 | { 85 | ec.assign( cause, ::boost::system::system_category() ); 86 | return steady_clock::time_point(duration(0)); 87 | } 88 | } 89 | 90 | if (!::boost::chrono::is_throws(ec)) 91 | { 92 | ec.clear(); 93 | } 94 | return time_point(duration( 95 | static_cast(nanosecs_per_tic * pcount.QuadPart))); 96 | } 97 | #endif 98 | 99 | BOOST_CHRONO_INLINE 100 | system_clock::time_point system_clock::now() BOOST_NOEXCEPT 101 | { 102 | boost::winapi::FILETIME_ ft; 103 | boost::winapi::GetSystemTimeAsFileTime( &ft ); // never fails 104 | return system_clock::time_point( 105 | system_clock::duration( 106 | ((static_cast<__int64>( ft.dwHighDateTime ) << 32) | ft.dwLowDateTime) 107 | - 116444736000000000LL 108 | //- (134775LL*864000000000LL) 109 | ) 110 | ); 111 | } 112 | 113 | #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING 114 | BOOST_CHRONO_INLINE 115 | system_clock::time_point system_clock::now( system::error_code & ec ) 116 | { 117 | boost::winapi::FILETIME_ ft; 118 | boost::winapi::GetSystemTimeAsFileTime( &ft ); // never fails 119 | if (!::boost::chrono::is_throws(ec)) 120 | { 121 | ec.clear(); 122 | } 123 | return system_clock::time_point( 124 | system_clock::duration( 125 | ((static_cast<__int64>( ft.dwHighDateTime ) << 32) | ft.dwLowDateTime) 126 | - 116444736000000000LL 127 | //- (134775LL*864000000000LL) 128 | )); 129 | } 130 | #endif 131 | 132 | BOOST_CHRONO_INLINE 133 | std::time_t system_clock::to_time_t(const system_clock::time_point& t) BOOST_NOEXCEPT 134 | { 135 | __int64 temp = t.time_since_epoch().count(); 136 | temp /= 10000000; 137 | return static_cast( temp ); 138 | } 139 | 140 | BOOST_CHRONO_INLINE 141 | system_clock::time_point system_clock::from_time_t(std::time_t t) BOOST_NOEXCEPT 142 | { 143 | __int64 temp = t; 144 | temp *= 10000000; 145 | return time_point(duration(temp)); 146 | } 147 | 148 | } // namespace chrono 149 | } // namespace boost 150 | 151 | #endif 152 | -------------------------------------------------------------------------------- /example/miscellaneous.cpp: -------------------------------------------------------------------------------- 1 | // miscellaneous.cpp ----------------------------------------------------------// 2 | 3 | // Copyright 2008 Howard Hinnant 4 | // Copyright 2008 Beman Dawes 5 | // Copyright 2009 Vicente J. Botet Escriba 6 | 7 | // Distributed under the Boost Software License, Version 1.0. 8 | // See http://www.boost.org/LICENSE_1_0.txt 9 | 10 | /* 11 | This code was extracted by Vicente J. Botet Escriba from Beman Dawes time2_demo.cpp which 12 | was derived by Beman Dawes from Howard Hinnant's time2_demo prototype. 13 | Many thanks to Howard for making his code available under the Boost license. 14 | The original code was modified to conform to Boost conventions and to section 15 | 20.9 Time utilities [time] of the C++ committee's working paper N2798. 16 | See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf. 17 | 18 | time2_demo contained this comment: 19 | 20 | Much thanks to Andrei Alexandrescu, 21 | Walter Brown, 22 | Peter Dimov, 23 | Jeff Garland, 24 | Terry Golubiewski, 25 | Daniel Krugler, 26 | Anthony Williams. 27 | */ 28 | 29 | #include 30 | #include 31 | 32 | #include 33 | 34 | // miscellaneous tests and demos: 35 | 36 | #include 37 | #include 38 | 39 | using namespace boost::chrono; 40 | 41 | void physics_function(duration d) 42 | { 43 | std::cout << "d = " << d.count() << '\n'; 44 | } 45 | 46 | void drive_physics_function() 47 | { 48 | physics_function(nanoseconds(3)); 49 | physics_function(hours(3)); 50 | physics_function(duration(2./3)); 51 | std::cout.precision(16); 52 | physics_function( hours(3) + nanoseconds(-3) ); 53 | } 54 | 55 | void test_range() 56 | { 57 | using namespace boost::chrono; 58 | hours h1 = hours(24 * ( 365 * 292 + 292/4)); 59 | nanoseconds n1 = h1 + nanoseconds(1); 60 | nanoseconds delta = n1 - h1; 61 | std::cout << "292 years of hours = " << h1.count() << "hr\n"; 62 | std::cout << "Add a nanosecond = " << n1.count() << "ns\n"; 63 | std::cout << "Find the difference = " << delta.count() << "ns\n"; 64 | } 65 | 66 | void test_extended_range() 67 | { 68 | using namespace boost::chrono; 69 | hours h1 = hours(24 * ( 365 * 244000 + 244000/4)); 70 | /*auto*/ microseconds u1 = h1 + microseconds(1); 71 | /*auto*/ microseconds delta = u1 - h1; 72 | std::cout << "244,000 years of hours = " << h1.count() << "hr\n"; 73 | std::cout << "Add a microsecond = " << u1.count() << "us\n"; 74 | std::cout << "Find the difference = " << delta.count() << "us\n"; 75 | } 76 | 77 | template 78 | void inspect_duration(boost::chrono::duration d, const std::string& name) 79 | { 80 | typedef boost::chrono::duration Duration; 81 | std::cout << "********* " << name << " *********\n"; 82 | std::cout << "The period of " << name << " is " << (double)Period::num/Period::den << " seconds.\n"; 83 | std::cout << "The frequency of " << name << " is " << (double)Period::den/Period::num << " Hz.\n"; 84 | std::cout << "The representation is "; 85 | if (boost::is_floating_point::value) 86 | { 87 | std::cout << "floating point\n"; 88 | std::cout << "The precision is the most significant "; 89 | std::cout << std::numeric_limits::digits10 << " decimal digits.\n"; 90 | } 91 | else if (boost::is_integral::value) 92 | { 93 | std::cout << "integral\n"; 94 | d = Duration(Rep(1)); 95 | boost::chrono::duration dsec = d; 96 | std::cout << "The precision is " << dsec.count() << " seconds.\n"; 97 | } 98 | else 99 | { 100 | std::cout << "a class type\n"; 101 | d = Duration(Rep(1)); 102 | boost::chrono::duration dsec = d; 103 | std::cout << "The precision is " << dsec.count() << " seconds.\n"; 104 | } 105 | d = Duration((std::numeric_limits::max)()); 106 | using namespace boost::chrono; 107 | typedef duration, hours::period>::type> Years; 108 | Years years = d; 109 | std::cout << "The range is +/- " << years.count() << " years.\n"; 110 | std::cout << "sizeof(" << name << ") = " << sizeof(d) << '\n'; 111 | } 112 | 113 | void inspect_all() 114 | { 115 | using namespace boost::chrono; 116 | std::cout.precision(6); 117 | inspect_duration(nanoseconds(), "nanoseconds"); 118 | inspect_duration(microseconds(), "microseconds"); 119 | inspect_duration(milliseconds(), "milliseconds"); 120 | inspect_duration(seconds(), "seconds"); 121 | inspect_duration(minutes(), "minutes"); 122 | inspect_duration(hours(), "hours"); 123 | inspect_duration(duration(), "duration"); 124 | } 125 | 126 | void test_milliseconds() 127 | { 128 | using namespace boost::chrono; 129 | milliseconds ms(250); 130 | ms += milliseconds(1); 131 | milliseconds ms2(150); 132 | milliseconds msdiff = ms - ms2; 133 | if (msdiff == milliseconds(101)) 134 | std::cout << "success\n"; 135 | else 136 | std::cout << "failure: " << msdiff.count() << '\n'; 137 | } 138 | 139 | int main() 140 | { 141 | using namespace boost; 142 | drive_physics_function(); 143 | test_range(); 144 | test_extended_range(); 145 | inspect_all(); 146 | test_milliseconds(); 147 | inspect_duration(common_type, hours, microseconds>::type(), 148 | "common_type, hours, microseconds>::type"); 149 | duration d = milliseconds(3) * 2.5; 150 | inspect_duration(milliseconds(3) * 2.5, "milliseconds(3) * 2.5"); 151 | std::cout << d.count() << '\n'; 152 | // milliseconds ms(3.5); // doesn't compile 153 | // std::cout << "milliseconds ms(3.5) doesn't compile\n"; 154 | return 0; 155 | } 156 | 157 | --------------------------------------------------------------------------------