Coverage Report

Created: 2025-08-25 06:28

/src/libtorrent/src/time.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
3
Copyright (c) 2009, 2015, 2017-2019, 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
#include "libtorrent/aux_/time.hpp"
34
35
#include <chrono>
36
37
namespace libtorrent { namespace aux {
38
39
5
  time_point time_now() { return clock_type::now(); }
40
0
  time_point32 time_now32() { return time_point_cast<seconds32>(clock_type::now()); }
41
42
  // for simplifying implementation
43
  using std::chrono::system_clock;
44
45
46
  // consider using std::chrono::clock_cast on C++20
47
  time_t to_time_t(const time_point32 tp)
48
0
  {
49
    // special case for unset value
50
0
    if (tp == time_point32(seconds32(0))) return 0;
51
52
0
    const auto lt_now = clock_type::now();
53
0
    const auto sys_now = system_clock::now();
54
55
0
    const auto r = sys_now + std::chrono::duration_cast<system_clock::duration>(tp - lt_now) + lt::milliseconds(500);
56
0
    return system_clock::to_time_t(r);
57
0
  }
58
59
  // consider using std::chrono::clock_cast on C++20
60
  time_point32 from_time_t(const std::time_t t)
61
0
  {
62
    // special case for unset value
63
0
    if (t == 0) return time_point32(seconds32(0));
64
65
0
    const auto tp = system_clock::from_time_t(t);
66
0
    const auto sys_now = system_clock::now();
67
0
    const auto lt_now = clock_type::now();
68
69
0
    auto r = lt_now + std::chrono::duration_cast<clock_type::duration>(tp - sys_now);
70
    // the conversion to seconds will truncate, make sure we round
71
0
    return std::chrono::time_point_cast<seconds32>(r + milliseconds(500));
72
0
  }
73
74
  time_t posix_time()
75
0
  {
76
#ifdef TORRENT_BUILD_SIMULATOR
77
    return 1722056360 + clock_type::now().time_since_epoch().count();
78
#else
79
0
    return ::time(nullptr);
80
0
#endif
81
0
  }
82
} }