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 1 commit
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
45 changes: 45 additions & 0 deletions sdk/core/azure-core/inc/azure/core/datetime.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,51 @@ namespace Core { namespace _internal {
*/
~PosixTimeConverter() = delete;
};

/**
* @brief Provides convertion methods for Windows NT filetime to an #Azure::DateTime.
Jinming-Hu marked this conversation as resolved.
Show resolved Hide resolved
Jinming-Hu marked this conversation as resolved.
Show resolved Hide resolved
*
*/
class NTFileTimeTimeConverter final {
Jinming-Hu marked this conversation as resolved.
Show resolved Hide resolved
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<Azure::_detail::Clock::duration>(dateTime - DateTime(1601))
.count();
Jinming-Hu marked this conversation as resolved.
Show resolved Hide resolved
}

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
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, 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");
}