DateTime.cpp (3476B)
1 // Copyright (c) Microsoft Corporation. 2 // Licensed under the MIT License. 3 #include "pch.h" 4 #include "TestCommon.h" 5 #include <AppInstallerDateTime.h> 6 7 using namespace AppInstaller::Utility; 8 using namespace TestCommon; 9 using namespace std::chrono; 10 11 namespace Catch 12 { 13 template<> 14 struct StringMaker<std::chrono::system_clock::time_point> 15 { 16 static std::string convert(const std::chrono::system_clock::time_point& value) 17 { 18 std::ostringstream stream; 19 OutputTimePoint(stream, value); 20 return std::move(stream).str(); 21 } 22 }; 23 } 24 25 void VerifyGetTimePointFromVersion(std::string_view version, int year, int month, int day, int hour, int minute) 26 { 27 system_clock::time_point result = GetTimePointFromVersion(UInt64Version{ std::string{ version } }); 28 29 tm time{}; 30 auto tt = system_clock::to_time_t(result); 31 _gmtime64_s(&time, &tt); 32 33 REQUIRE(year == time.tm_year + 1900); 34 REQUIRE(month == time.tm_mon + 1); 35 REQUIRE(day == time.tm_mday); 36 REQUIRE(hour == time.tm_hour); 37 REQUIRE(minute == time.tm_min); 38 } 39 40 std::string StringFromTimePoint(system_clock::time_point input) 41 { 42 tm time{}; 43 auto tt = system_clock::to_time_t(input); 44 _gmtime64_s(&time, &tt); 45 46 std::ostringstream stream; 47 stream << time.tm_year + 1900 << '.' << ((time.tm_mon + 1) * 100) + time.tm_mday << '.' << ((time.tm_hour + 1) * 100) + time.tm_min; 48 return std::move(stream).str(); 49 } 50 51 TEST_CASE("GetTimePointFromVersion", "[datetime]") 52 { 53 // Years out of range 54 REQUIRE(GetTimePointFromVersion(UInt64Version{ "1969.1231.2459.0" }) == system_clock::time_point::min()); 55 REQUIRE(GetTimePointFromVersion(UInt64Version{ "3001.101.100.0" }) == system_clock::time_point::min()); 56 57 // Months out of range 58 REQUIRE(GetTimePointFromVersion(UInt64Version{ "2023.1.100.0" }) == system_clock::time_point::min()); 59 REQUIRE(GetTimePointFromVersion(UInt64Version{ "2023.1301.100.0" }) == system_clock::time_point::min()); 60 61 // Days out of range 62 REQUIRE(GetTimePointFromVersion(UInt64Version{ "2023.100.100.0" }) == system_clock::time_point::min()); 63 REQUIRE(GetTimePointFromVersion(UInt64Version{ "2023.132.100.0" }) == system_clock::time_point::min()); 64 65 // Hours out of range 66 REQUIRE(GetTimePointFromVersion(UInt64Version{ "2023.101.0.0" }) == system_clock::time_point::min()); 67 REQUIRE(GetTimePointFromVersion(UInt64Version{ "2023.101.2500.0" }) == system_clock::time_point::min()); 68 69 // Minutes out of range 70 REQUIRE(GetTimePointFromVersion(UInt64Version{ "2023.101.160.0" }) == system_clock::time_point::min()); 71 72 // In range baseline 73 VerifyGetTimePointFromVersion("2023.101.100.0", 2023, 1, 1, 0, 0); 74 75 // Time for presents! 76 VerifyGetTimePointFromVersion("2023.1225.814.0", 2023, 12, 25, 7, 14); 77 78 // Epoch time 79 REQUIRE(GetTimePointFromVersion(UInt64Version{ "1970.101.100.0" }) == system_clock::time_point{}); 80 81 // Round trip now 82 system_clock::time_point now = system_clock::now(); 83 REQUIRE(GetTimePointFromVersion(UInt64Version{ StringFromTimePoint(now) }) == time_point_cast<minutes>(now)); 84 } 85 86 TEST_CASE("ShortFileTime", "[datetime]") 87 { 88 auto shortTime = GetCurrentTimeForFilename(true); 89 auto longTime = GetCurrentTimeForFilename(false); 90 INFO(shortTime); 91 INFO(longTime); 92 REQUIRE(shortTime.length() < longTime.length()); 93 }