forked from microsoft/winget-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateTime.cpp
More file actions
124 lines (103 loc) · 4.85 KB
/
Copy pathDateTime.cpp
File metadata and controls
124 lines (103 loc) · 4.85 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "Public/AppInstallerDateTime.h"
namespace AppInstaller::Utility
{
// If moved to C++20, this can be replaced with standard library implementations.
void OutputTimePoint(std::ostream& stream, const std::chrono::system_clock::time_point& time, bool useRFC3339)
{
using namespace std::chrono;
tm localTime{};
auto tt = system_clock::to_time_t(time);
_localtime64_s(&localTime, &tt);
stream
<< std::setw(4) << (1900 + localTime.tm_year) << '-'
<< std::setw(2) << std::setfill('0') << (1 + localTime.tm_mon) << '-'
<< std::setw(2) << std::setfill('0') << localTime.tm_mday << (useRFC3339 ? 'T' : ' ')
<< std::setw(2) << std::setfill('0') << localTime.tm_hour << ':'
<< std::setw(2) << std::setfill('0') << localTime.tm_min << ':'
<< std::setw(2) << std::setfill('0') << localTime.tm_sec << '.';
// Get partial seconds
auto sinceEpoch = time.time_since_epoch();
auto leftoverMillis = duration_cast<milliseconds>(sinceEpoch) - duration_cast<seconds>(sinceEpoch);
stream << std::setw(3) << std::setfill('0') << leftoverMillis.count();
if (useRFC3339)
{
// RFC 3339 requires adding time zone info.
// No need to bother getting the actual time zone as we don't need it.
// -00:00 represents an unspecified time zone, not UTC.
stream << "-00:00";
}
}
std::string GetCurrentTimeForFilename()
{
std::stringstream stream;
OutputTimePoint(stream, std::chrono::system_clock::now());
auto result = stream.str();
std::replace(result.begin(), result.end(), ':', '-');
std::replace(result.begin(), result.end(), ' ', '-');
return result;
}
std::string GetCurrentDateForARP()
{
auto now = std::chrono::system_clock::now();
std::time_t tt = std::chrono::system_clock::to_time_t(now);
struct tm newTime;
localtime_s(&newTime, &tt);
std::stringstream ss;
ss << std::put_time(&newTime, "%Y%m%d");
return ss.str();
}
int64_t GetCurrentUnixEpoch()
{
static_assert(std::is_same_v<int64_t, decltype(time(nullptr))>, "time returns a 64-bit integer");
time_t now = time(nullptr);
return static_cast<int64_t>(now);
}
int64_t ConvertSystemClockToUnixEpoch(const std::chrono::system_clock::time_point& time)
{
static_assert(std::is_same_v<int64_t, decltype(std::chrono::system_clock::to_time_t(time))>, "to_time_t returns a 64-bit integer");
time_t timeAsTimeT = std::chrono::system_clock::to_time_t(time);
return static_cast<int64_t>(timeAsTimeT);
}
std::chrono::system_clock::time_point ConvertUnixEpochToSystemClock(int64_t epoch)
{
return std::chrono::system_clock::from_time_t(static_cast<time_t>(epoch));
}
std::chrono::system_clock::time_point GetTimePointFromVersion(const UInt64Version& version)
{
// Our custom format for converting UTC into a version is:
// Major :: `Year` [1, 9999]
// Minor :: `Month * 100 + Day` where Month [1, 12] and Day [1, 31]
// Build :: `Hour * 100 + Minute` where Hour [1, 24] and Minute [0, 59]
// Revision :: Milliseconds, but since no seconds are available we will disregard this
tm versionTime{};
// Limit to the range supported by _mkgmtime64, which is 1970 to 3000 (hello to Y3K maintainers from 2023!)
UINT64 majorVersion = version.Major();
if (majorVersion < 1970 || majorVersion > 3000)
{
return std::chrono::system_clock::time_point::min();
}
versionTime.tm_year = static_cast<int>(majorVersion) - 1900;
UINT64 minorVersion = version.Minor();
UINT64 monthValue = minorVersion / 100;
UINT64 dayValue = minorVersion % 100;
if (monthValue < 1 || monthValue > 12 || dayValue < 1 || dayValue > 31)
{
return std::chrono::system_clock::time_point::min();
}
versionTime.tm_mon = static_cast<int>(monthValue) - 1;
versionTime.tm_mday = static_cast<int>(dayValue);
UINT64 buildVersion = version.Build();
UINT64 hourValue = buildVersion / 100;
UINT64 minuteValue = buildVersion % 100;
if (hourValue < 1 || hourValue > 24 || minuteValue > 59)
{
return std::chrono::system_clock::time_point::min();
}
versionTime.tm_hour = static_cast<int>(hourValue) - 1;
versionTime.tm_min = static_cast<int>(minuteValue);
return std::chrono::system_clock::from_time_t(_mkgmtime64(&versionTime));
}
}