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 // Filesystem is supported on Apple platforms starting with macosx10.15.
13 // UNSUPPORTED: use_system_cxx_lib && target={{.+}}-apple-macosx10.{{9|10|11|12|13|14}}
14 
15 // <chrono>
16 //
17 // file_clock
18 //
19 // template<class Duration>
20 // static sys_time<see-below> to_sys(const file_time<Duration>&);
21 //
22 // template<class Duration>
23 // static file_time<see-below> from_sys(const sys_time<Duration>&);
24 
25 #include <chrono>
26 #include <cassert>
27 
main(int,char **)28 int main(int, char**) {
29   // Test round-trip through the system clock, starting from file_clock::now()
30   {
31     std::chrono::file_clock::time_point const ft = std::chrono::file_clock::now();
32     auto st = std::chrono::file_clock::to_sys(ft);
33     assert(ft == std::chrono::file_clock::from_sys(st));
34   }
35 
36   // Test round-trip through the system clock, starting from system_clock::now()
37   {
38     std::chrono::system_clock::time_point const st = std::chrono::system_clock::now();
39     auto ft = std::chrono::file_clock::from_sys(st);
40     assert(st == std::chrono::file_clock::to_sys(ft));
41   }
42 
43   // Make sure the value we get is in the ballpark of something reasonable
44   {
45     std::chrono::file_clock::time_point const file_now = std::chrono::file_clock::now();
46     std::chrono::system_clock::time_point const sys_now = std::chrono::system_clock::now();
47     {
48       auto diff = sys_now - std::chrono::file_clock::to_sys(file_now);
49       assert(std::chrono::milliseconds(-500) < diff && diff < std::chrono::milliseconds(500));
50     }
51     {
52       auto diff = std::chrono::file_clock::from_sys(sys_now) - file_now;
53       assert(std::chrono::milliseconds(-500) < diff && diff < std::chrono::milliseconds(500));
54     }
55   }
56 
57   // Make sure to_sys and from_sys are consistent with each other
58   {
59     std::chrono::file_clock::time_point const ft = std::chrono::file_clock::now();
60     std::chrono::system_clock::time_point const st = std::chrono::system_clock::now();
61     auto sys_diff = std::chrono::file_clock::to_sys(ft) - st;
62     auto file_diff = ft - std::chrono::file_clock::from_sys(st);
63     assert(sys_diff == file_diff);
64   }
65 
66   return 0;
67 }
68