Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NTFileTimeTimeConverter #3941

Merged
merged 3 commits into from
Sep 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion sdk/core/azure-core/inc/azure/core/datetime.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -311,6 +311,51 @@ namespace Core { namespace _internal {
*/
~PosixTimeConverter() = delete;
};

/**
* @brief Provides conversion methods for Win32 FILETIME to an #Azure::DateTime.
*
*/
class Win32FileTimeConverter final {
public:
/**
* @brief Converts Win32 FILETIME to an #Azure::DateTime.
*
* @param win32Filetime The number of 100-nanoseconds since 1601-01-01.
* @return Calculated #Azure::DateTime.
*/
static DateTime Win32FileTimeToDateTime(int64_t win32Filetime)
{
auto t = DateTime(1601) + Azure::_detail::Clock::duration(win32Filetime);
return DateTime(t);
}

/**
* @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 DateTimeToWin32FileTime(DateTime const& dateTime)
{
return std::chrono::duration_cast<Azure::_detail::Clock::duration>(dateTime - DateTime(1601))
.count();
Jinming-Hu marked this conversation as resolved.
Show resolved Hide resolved
}

private:
/**
* @brief An instance of `%Win32FileTimeConverter` class cannot be created.
*
*/
Win32FileTimeConverter() = delete;

/**
* @brief An instance of `%Win32FileTimeConverter` class cannot be destructed, because no
* instance can be created.
*
*/
~Win32FileTimeConverter() = delete;
};
}} // namespace Core::_internal

} // namespace Azure
41 changes: 41 additions & 0 deletions sdk/core/azure-core/test/ut/datetime_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -895,3 +895,44 @@ TEST(DateTime, LeapYear)
EXPECT_NO_THROW(static_cast<void>(DateTime(2021, 2, 28)));
EXPECT_THROW(static_cast<void>(DateTime(2021, 2, 29)), std::invalid_argument);
}

TEST(DateTime, Win32FileTimeConverter)
{
using namespace Azure::Core::_internal;

EXPECT_EQ(
Win32FileTimeConverter::DateTimeToWin32FileTime(
Win32FileTimeConverter::Win32FileTimeToDateTime(0)),
0);
EXPECT_EQ(
Win32FileTimeConverter::DateTimeToWin32FileTime(
Win32FileTimeConverter::Win32FileTimeToDateTime(1)),
1);
EXPECT_EQ(
Win32FileTimeConverter::DateTimeToWin32FileTime(
Win32FileTimeConverter::Win32FileTimeToDateTime(133080315699856412)),
133080315699856412);
EXPECT_EQ(
Win32FileTimeConverter::DateTimeToWin32FileTime(
Win32FileTimeConverter::Win32FileTimeToDateTime(2650467743995784569)),
2650467743995784569);

EXPECT_EQ(
Win32FileTimeConverter::Win32FileTimeToDateTime(0).ToString(
Azure::DateTime::DateFormat::Rfc3339, Azure::DateTime::TimeFractionFormat::AllDigits),
"1601-01-01T00:00:00.0000000Z");
EXPECT_EQ(
Win32FileTimeConverter::Win32FileTimeToDateTime(1).ToString(
Azure::DateTime::DateFormat::Rfc3339, Azure::DateTime::TimeFractionFormat::AllDigits),
"1601-01-01T00:00:00.0000001Z");
EXPECT_EQ(
Win32FileTimeConverter::Win32FileTimeToDateTime(133080315699856412)
.ToString(
Azure::DateTime::DateFormat::Rfc3339, Azure::DateTime::TimeFractionFormat::AllDigits),
"2022-09-19T03:26:09.9856412Z");
EXPECT_EQ(
Win32FileTimeConverter::Win32FileTimeToDateTime(2650467743995784569)
.ToString(
Azure::DateTime::DateFormat::Rfc3339, Azure::DateTime::TimeFractionFormat::AllDigits),
"9999-12-31T23:59:59.5784569Z");
}