AppInstallerDateTime.h (2973B)
1 // Copyright (c) Microsoft Corporation. 2 // Licensed under the MIT License. 3 #pragma once 4 #include <AppInstallerVersions.h> 5 #include <chrono> 6 #include <ostream> 7 8 namespace AppInstaller::Utility 9 { 10 // The individual aspects of a time point. 11 enum class TimeFacet 12 { 13 None = 0x000, 14 Millisecond = 0x001, 15 Second = 0x002, 16 Minute = 0x004, 17 Hour = 0x008, 18 Day = 0x010, 19 Month = 0x020, 20 Year = 0x040, 21 // `Year - 2000` [2 digits for 75 more years] 22 ShortYear = 0x080, 23 // Includes unspecified time zone 24 RFC3339 = 0x100, 25 // Limits special character use 26 Filename = 0x200, 27 28 Default = Year | Month | Day | Hour | Minute | Second | Millisecond, 29 ShortYearSecondPrecision = ShortYear | Month | Day | Hour | Minute | Second, 30 }; 31 32 DEFINE_ENUM_FLAG_OPERATORS(TimeFacet); 33 34 // Writes the given time to the given stream. 35 // Assumes that system_clock uses Linux epoch (as required by C++20 standard). 36 // Time is also assumed to be after the epoch. 37 void OutputTimePoint(std::ostream& stream, const std::chrono::system_clock::time_point& time, bool useRFC3339 = false); 38 void OutputTimePoint(std::ostream& stream, const std::chrono::system_clock::time_point& time, TimeFacet facet); 39 40 // Converts the time point to a string using OutputTimePoint. 41 std::string TimePointToString(const std::chrono::system_clock::time_point& time, bool useRFC3339 = false); 42 std::string TimePointToString(const std::chrono::system_clock::time_point& time, TimeFacet facet); 43 44 // Gets the current time as a string. Can be used as a file name. 45 // Tries to make things a little bit shorter when shortTime == true. 46 std::string GetCurrentTimeForFilename(bool shortTime = false); 47 48 // Gets the current date as a string to be used in the ARP registry. 49 std::string GetCurrentDateForARP(); 50 51 // Gets the current time as a unix epoch value. 52 int64_t GetCurrentUnixEpoch(); 53 54 // Converts the given unix epoch time to a system_clock::time_point. 55 int64_t ConvertSystemClockToUnixEpoch(const std::chrono::system_clock::time_point& time); 56 57 // Converts the given unix epoch time to a system_clock::time_point. 58 std::chrono::system_clock::time_point ConvertUnixEpochToSystemClock(int64_t epoch); 59 60 // Converts the given file time to a system_clock::time_point. 61 std::chrono::system_clock::time_point ConvertFiletimeToSystemClock(const FILETIME& fileTime); 62 63 // Converts the given package version into a time_point using our custom format. 64 // Ensure that the package is expected to use this format, or you may get strange times. 65 // If the version is not convertable, the minimum time is returned. 66 std::chrono::system_clock::time_point GetTimePointFromVersion(const UInt64Version& version); 67 }