forked from microsoft/PowerToys
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimeutil.h
More file actions
57 lines (48 loc) · 1.34 KB
/
timeutil.h
File metadata and controls
57 lines (48 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#pragma once
#include <ctime>
#include <cinttypes>
#include <string>
#include <optional>
#include <winrt/base.h>
namespace timeutil
{
inline std::wstring to_string(const time_t time)
{
return std::to_wstring(static_cast<uint64_t>(time));
}
inline std::optional<std::time_t> from_string(const std::wstring& s)
{
try
{
uint64_t i = std::stoull(s);
return static_cast<std::time_t>(i);
}
catch (...)
{
return std::nullopt;
}
}
inline std::time_t now()
{
return winrt::clock::to_time_t(winrt::clock::now());
}
namespace diff
{
inline int64_t in_seconds(const std::time_t to, const std::time_t from)
{
return static_cast<int64_t>(std::difftime(to, from));
}
inline int64_t in_minutes(const std::time_t to, const std::time_t from)
{
return static_cast<int64_t>(std::difftime(to, from) / 60);
}
inline int64_t in_hours(const std::time_t to, const std::time_t from)
{
return static_cast<int64_t>(std::difftime(to, from) / 3600);
}
inline int64_t in_days(const std::time_t to, const std::time_t from)
{
return static_cast<int64_t>(std::difftime(to, from) / (3600 * (int64_t)24));
}
}
}