• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++03, c++11, c++14, c++17
10 // UNSUPPORTED: no-filesystem
11 
12 // UNSUPPORTED: availability-filesystem-missing
13 
14 // <chrono>
15 //
16 // file_clock
17 //
18 // template<class Duration>
19 // static sys_time<see-below> to_sys(const file_time<Duration>&);
20 //
21 // template<class Duration>
22 // static file_time<see-below> from_sys(const sys_time<Duration>&);
23 
24 #include <chrono>
25 #include <cassert>
26 
main(int,char **)27 int main(int, char**) {
28   // Test round-trip through the system clock, starting from file_clock::now()
29   {
30     std::chrono::file_clock::time_point const ft = std::chrono::file_clock::now();
31     auto st = std::chrono::file_clock::to_sys(ft);
32     assert(ft == std::chrono::file_clock::from_sys(st));
33   }
34 
35   // Test round-trip through the system clock, starting from system_clock::now()
36   {
37     std::chrono::system_clock::time_point const st = std::chrono::system_clock::now();
38     auto ft = std::chrono::file_clock::from_sys(st);
39     assert(st == std::chrono::file_clock::to_sys(ft));
40   }
41 
42   // Make sure the value we get is in the ballpark of something reasonable
43   {
44     std::chrono::file_clock::time_point const file_now = std::chrono::file_clock::now();
45     std::chrono::system_clock::time_point const sys_now = std::chrono::system_clock::now();
46     {
47       auto diff = sys_now - std::chrono::file_clock::to_sys(file_now);
48       assert(std::chrono::milliseconds(-500) < diff && diff < std::chrono::milliseconds(500));
49     }
50     {
51       auto diff = std::chrono::file_clock::from_sys(sys_now) - file_now;
52       assert(std::chrono::milliseconds(-500) < diff && diff < std::chrono::milliseconds(500));
53     }
54   }
55 
56   // Make sure to_sys and from_sys are consistent with each other
57   {
58     std::chrono::file_clock::time_point const ft = std::chrono::file_clock::now();
59     std::chrono::system_clock::time_point const st = std::chrono::system_clock::now();
60     auto sys_diff = std::chrono::file_clock::to_sys(ft) - st;
61     auto file_diff = ft - std::chrono::file_clock::from_sys(st);
62     assert(sys_diff == file_diff);
63   }
64 
65   return 0;
66 }
67