From 10d3ced17d7ee131903ba7563d0516eaf2e6e097 Mon Sep 17 00:00:00 2001 From: Jinming Hu Date: Mon, 19 Sep 2022 11:32:36 +0800 Subject: [PATCH 1/3] add NTFileTimeTimeConverter --- .../azure-core/inc/azure/core/datetime.hpp | 45 +++++++++++++++++++ sdk/core/azure-core/test/ut/datetime_test.cpp | 41 +++++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/sdk/core/azure-core/inc/azure/core/datetime.hpp b/sdk/core/azure-core/inc/azure/core/datetime.hpp index 4ed531473c..4d70b36343 100644 --- a/sdk/core/azure-core/inc/azure/core/datetime.hpp +++ b/sdk/core/azure-core/inc/azure/core/datetime.hpp @@ -311,6 +311,51 @@ namespace Core { namespace _internal { */ ~PosixTimeConverter() = delete; }; + + /** + * @brief Provides convertion methods for Windows NT filetime to an #Azure::DateTime. + * + */ + class NTFileTimeTimeConverter final { + public: + /** + * @brief Converts Windows NT filetime to an #Azure::DateTime. + * + * @param ntFiletime The number of 100-nanoseconds since 1601-01-01. + * @return Calculated #Azure::DateTime. + */ + static DateTime NTFileTimeToDateTime(int64_t ntFiletime) + { + auto t = DateTime(1601) + Azure::_detail::Clock::duration(ntFiletime); + return DateTime(t); + } + + /** + * @brief Converts a DateTime to Windows NT filetime. + * + * @param dateTime The `%DateTime` to convert. + * @return The number of 100-nanoseconds since 1601-01-01. + */ + static int64_t DateTimeToNTFileTime(DateTime const& dateTime) + { + return std::chrono::duration_cast(dateTime - DateTime(1601)) + .count(); + } + + private: + /** + * @brief An instance of `%NTFileTimeTimeConverter` class cannot be created. + * + */ + NTFileTimeTimeConverter() = delete; + + /** + * @brief An instance of `%NTFileTimeTimeConverter` class cannot be destructed, because no + * instance can be created. + * + */ + ~NTFileTimeTimeConverter() = delete; + }; }} // namespace Core::_internal } // namespace Azure diff --git a/sdk/core/azure-core/test/ut/datetime_test.cpp b/sdk/core/azure-core/test/ut/datetime_test.cpp index 2496819925..9b591270d2 100644 --- a/sdk/core/azure-core/test/ut/datetime_test.cpp +++ b/sdk/core/azure-core/test/ut/datetime_test.cpp @@ -895,3 +895,44 @@ TEST(DateTime, LeapYear) EXPECT_NO_THROW(static_cast(DateTime(2021, 2, 28))); EXPECT_THROW(static_cast(DateTime(2021, 2, 29)), std::invalid_argument); } + +TEST(DateTime, WindowsNTFileTimeConverter) +{ + using namespace Azure::Core::_internal; + + EXPECT_EQ( + NTFileTimeTimeConverter::DateTimeToNTFileTime( + NTFileTimeTimeConverter::NTFileTimeToDateTime(0)), + 0); + EXPECT_EQ( + NTFileTimeTimeConverter::DateTimeToNTFileTime( + NTFileTimeTimeConverter::NTFileTimeToDateTime(1)), + 1); + EXPECT_EQ( + NTFileTimeTimeConverter::DateTimeToNTFileTime( + NTFileTimeTimeConverter::NTFileTimeToDateTime(133080315699856412)), + 133080315699856412); + EXPECT_EQ( + NTFileTimeTimeConverter::DateTimeToNTFileTime( + NTFileTimeTimeConverter::NTFileTimeToDateTime(2650467743995784569)), + 2650467743995784569); + + EXPECT_EQ( + NTFileTimeTimeConverter::NTFileTimeToDateTime(0).ToString( + Azure::DateTime::DateFormat::Rfc3339, Azure::DateTime::TimeFractionFormat::AllDigits), + "1601-01-01T00:00:00.0000000Z"); + EXPECT_EQ( + NTFileTimeTimeConverter::NTFileTimeToDateTime(1).ToString( + Azure::DateTime::DateFormat::Rfc3339, Azure::DateTime::TimeFractionFormat::AllDigits), + "1601-01-01T00:00:00.0000001Z"); + EXPECT_EQ( + NTFileTimeTimeConverter::NTFileTimeToDateTime(133080315699856412) + .ToString( + Azure::DateTime::DateFormat::Rfc3339, Azure::DateTime::TimeFractionFormat::AllDigits), + "2022-09-19T03:26:09.9856412Z"); + EXPECT_EQ( + NTFileTimeTimeConverter::NTFileTimeToDateTime(2650467743995784569) + .ToString( + Azure::DateTime::DateFormat::Rfc3339, Azure::DateTime::TimeFractionFormat::AllDigits), + "9999-12-31T23:59:59.5784569Z"); +} From d1c02008f7ba5e20052c0429d2e3a7b70644c2de Mon Sep 17 00:00:00 2001 From: Jinming Hu Date: Tue, 20 Sep 2022 10:03:45 +0800 Subject: [PATCH 2/3] resolve comments --- sdk/core/azure-core/inc/azure/core/datetime.hpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sdk/core/azure-core/inc/azure/core/datetime.hpp b/sdk/core/azure-core/inc/azure/core/datetime.hpp index 4d70b36343..3f76cde411 100644 --- a/sdk/core/azure-core/inc/azure/core/datetime.hpp +++ b/sdk/core/azure-core/inc/azure/core/datetime.hpp @@ -269,7 +269,7 @@ inline bool operator>=(std::chrono::system_clock::time_point const& tp, DateTime namespace Core { namespace _internal { /** - * @brief Provides convertion methods for POSIX time to an #Azure::DateTime. + * @brief Provides conversion methods for POSIX time to an #Azure::DateTime. * */ class PosixTimeConverter final { @@ -313,13 +313,13 @@ namespace Core { namespace _internal { }; /** - * @brief Provides convertion methods for Windows NT filetime to an #Azure::DateTime. + * @brief Provides conversion methods for Windows NT FILETIME to an #Azure::DateTime. * */ class NTFileTimeTimeConverter final { public: /** - * @brief Converts Windows NT filetime to an #Azure::DateTime. + * @brief Converts Windows NT FILETIME to an #Azure::DateTime. * * @param ntFiletime The number of 100-nanoseconds since 1601-01-01. * @return Calculated #Azure::DateTime. @@ -331,7 +331,7 @@ namespace Core { namespace _internal { } /** - * @brief Converts a DateTime to Windows NT filetime. + * @brief Converts a DateTime to Windows NT FILETIME. * * @param dateTime The `%DateTime` to convert. * @return The number of 100-nanoseconds since 1601-01-01. From 1df34e7b3ee82da8b7b92d4caf946b43fcdc0d07 Mon Sep 17 00:00:00 2001 From: Jinming Hu Date: Tue, 20 Sep 2022 12:29:28 +0800 Subject: [PATCH 3/3] rename class and functions --- .../azure-core/inc/azure/core/datetime.hpp | 24 ++++++++--------- sdk/core/azure-core/test/ut/datetime_test.cpp | 26 +++++++++---------- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/sdk/core/azure-core/inc/azure/core/datetime.hpp b/sdk/core/azure-core/inc/azure/core/datetime.hpp index 3f76cde411..53eecb5f9f 100644 --- a/sdk/core/azure-core/inc/azure/core/datetime.hpp +++ b/sdk/core/azure-core/inc/azure/core/datetime.hpp @@ -313,30 +313,30 @@ namespace Core { namespace _internal { }; /** - * @brief Provides conversion methods for Windows NT FILETIME to an #Azure::DateTime. + * @brief Provides conversion methods for Win32 FILETIME to an #Azure::DateTime. * */ - class NTFileTimeTimeConverter final { + class Win32FileTimeConverter final { public: /** - * @brief Converts Windows NT FILETIME to an #Azure::DateTime. + * @brief Converts Win32 FILETIME to an #Azure::DateTime. * - * @param ntFiletime The number of 100-nanoseconds since 1601-01-01. + * @param win32Filetime The number of 100-nanoseconds since 1601-01-01. * @return Calculated #Azure::DateTime. */ - static DateTime NTFileTimeToDateTime(int64_t ntFiletime) + static DateTime Win32FileTimeToDateTime(int64_t win32Filetime) { - auto t = DateTime(1601) + Azure::_detail::Clock::duration(ntFiletime); + auto t = DateTime(1601) + Azure::_detail::Clock::duration(win32Filetime); return DateTime(t); } /** - * @brief Converts a DateTime to Windows NT FILETIME. + * @brief Converts a DateTime to Win32 FILETIME. * * @param dateTime The `%DateTime` to convert. * @return The number of 100-nanoseconds since 1601-01-01. */ - static int64_t DateTimeToNTFileTime(DateTime const& dateTime) + static int64_t DateTimeToWin32FileTime(DateTime const& dateTime) { return std::chrono::duration_cast(dateTime - DateTime(1601)) .count(); @@ -344,17 +344,17 @@ namespace Core { namespace _internal { private: /** - * @brief An instance of `%NTFileTimeTimeConverter` class cannot be created. + * @brief An instance of `%Win32FileTimeConverter` class cannot be created. * */ - NTFileTimeTimeConverter() = delete; + Win32FileTimeConverter() = delete; /** - * @brief An instance of `%NTFileTimeTimeConverter` class cannot be destructed, because no + * @brief An instance of `%Win32FileTimeConverter` class cannot be destructed, because no * instance can be created. * */ - ~NTFileTimeTimeConverter() = delete; + ~Win32FileTimeConverter() = delete; }; }} // namespace Core::_internal diff --git a/sdk/core/azure-core/test/ut/datetime_test.cpp b/sdk/core/azure-core/test/ut/datetime_test.cpp index 9b591270d2..02534134f2 100644 --- a/sdk/core/azure-core/test/ut/datetime_test.cpp +++ b/sdk/core/azure-core/test/ut/datetime_test.cpp @@ -896,42 +896,42 @@ TEST(DateTime, LeapYear) EXPECT_THROW(static_cast(DateTime(2021, 2, 29)), std::invalid_argument); } -TEST(DateTime, WindowsNTFileTimeConverter) +TEST(DateTime, Win32FileTimeConverter) { using namespace Azure::Core::_internal; EXPECT_EQ( - NTFileTimeTimeConverter::DateTimeToNTFileTime( - NTFileTimeTimeConverter::NTFileTimeToDateTime(0)), + Win32FileTimeConverter::DateTimeToWin32FileTime( + Win32FileTimeConverter::Win32FileTimeToDateTime(0)), 0); EXPECT_EQ( - NTFileTimeTimeConverter::DateTimeToNTFileTime( - NTFileTimeTimeConverter::NTFileTimeToDateTime(1)), + Win32FileTimeConverter::DateTimeToWin32FileTime( + Win32FileTimeConverter::Win32FileTimeToDateTime(1)), 1); EXPECT_EQ( - NTFileTimeTimeConverter::DateTimeToNTFileTime( - NTFileTimeTimeConverter::NTFileTimeToDateTime(133080315699856412)), + Win32FileTimeConverter::DateTimeToWin32FileTime( + Win32FileTimeConverter::Win32FileTimeToDateTime(133080315699856412)), 133080315699856412); EXPECT_EQ( - NTFileTimeTimeConverter::DateTimeToNTFileTime( - NTFileTimeTimeConverter::NTFileTimeToDateTime(2650467743995784569)), + Win32FileTimeConverter::DateTimeToWin32FileTime( + Win32FileTimeConverter::Win32FileTimeToDateTime(2650467743995784569)), 2650467743995784569); EXPECT_EQ( - NTFileTimeTimeConverter::NTFileTimeToDateTime(0).ToString( + Win32FileTimeConverter::Win32FileTimeToDateTime(0).ToString( Azure::DateTime::DateFormat::Rfc3339, Azure::DateTime::TimeFractionFormat::AllDigits), "1601-01-01T00:00:00.0000000Z"); EXPECT_EQ( - NTFileTimeTimeConverter::NTFileTimeToDateTime(1).ToString( + Win32FileTimeConverter::Win32FileTimeToDateTime(1).ToString( Azure::DateTime::DateFormat::Rfc3339, Azure::DateTime::TimeFractionFormat::AllDigits), "1601-01-01T00:00:00.0000001Z"); EXPECT_EQ( - NTFileTimeTimeConverter::NTFileTimeToDateTime(133080315699856412) + Win32FileTimeConverter::Win32FileTimeToDateTime(133080315699856412) .ToString( Azure::DateTime::DateFormat::Rfc3339, Azure::DateTime::TimeFractionFormat::AllDigits), "2022-09-19T03:26:09.9856412Z"); EXPECT_EQ( - NTFileTimeTimeConverter::NTFileTimeToDateTime(2650467743995784569) + Win32FileTimeConverter::Win32FileTimeToDateTime(2650467743995784569) .ToString( Azure::DateTime::DateFormat::Rfc3339, Azure::DateTime::TimeFractionFormat::AllDigits), "9999-12-31T23:59:59.5784569Z");