1 /* 2 3 Copyright (c) 2007-2018, Arvid Norberg 4 All rights reserved. 5 6 Redistribution and use in source and binary forms, with or without 7 modification, are permitted provided that the following conditions 8 are met: 9 10 * Redistributions of source code must retain the above copyright 11 notice, this list of conditions and the following disclaimer. 12 * Redistributions in binary form must reproduce the above copyright 13 notice, this list of conditions and the following disclaimer in 14 the documentation and/or other materials provided with the distribution. 15 * Neither the name of the author nor the names of its 16 contributors may be used to endorse or promote products derived 17 from this software without specific prior written permission. 18 19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 POSSIBILITY OF SUCH DAMAGE. 30 31 */ 32 33 #ifndef TORRENT_TIME_HPP_INCLUDED 34 #define TORRENT_TIME_HPP_INCLUDED 35 36 #include "libtorrent/config.hpp" 37 38 #include <cstdint> 39 #include <chrono> 40 41 #if defined TORRENT_BUILD_SIMULATOR 42 #include "libtorrent/aux_/disable_warnings_push.hpp" 43 #include "simulator/simulator.hpp" 44 #include "libtorrent/aux_/disable_warnings_pop.hpp" 45 #endif 46 47 namespace libtorrent { 48 49 #if defined TORRENT_BUILD_SIMULATOR 50 using clock_type = sim::chrono::high_resolution_clock; 51 #else 52 using clock_type = std::chrono::high_resolution_clock; 53 #endif 54 55 using time_point = clock_type::time_point; 56 using time_duration = clock_type::duration; 57 58 // 32 bit versions of time_point and duration, with second resolution 59 using seconds32 = std::chrono::duration<std::int32_t>; 60 using minutes32 = std::chrono::duration<std::int32_t, std::ratio<60>>; 61 using time_point32 = std::chrono::time_point<clock_type, seconds32>; 62 63 using seconds = std::chrono::seconds; 64 using milliseconds = std::chrono::milliseconds; 65 using microseconds = std::chrono::microseconds; 66 using minutes = std::chrono::minutes; 67 using hours = std::chrono::hours; 68 using std::chrono::duration_cast; 69 using std::chrono::time_point_cast; 70 71 // internal min_time()72 inline time_point min_time() { return (time_point::min)(); } 73 74 // internal max_time()75 inline time_point max_time() { return (time_point::max)(); } 76 77 template<class T> total_seconds(T td)78 std::int64_t total_seconds(T td) 79 { return duration_cast<seconds>(td).count(); } 80 81 template<class T> total_milliseconds(T td)82 std::int64_t total_milliseconds(T td) 83 { return duration_cast<milliseconds>(td).count(); } 84 85 template<class T> total_microseconds(T td)86 std::int64_t total_microseconds(T td) 87 { return duration_cast<microseconds>(td).count(); } 88 89 } 90 91 #endif // TORRENT_TIME_HPP_INCLUDED 92