-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OrcLib: TypeTraits: add TimeUtc<> strong type
- Loading branch information
1 parent
7368439
commit 1ea8365
Showing
5 changed files
with
127 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// | ||
// SPDX-License-Identifier: LGPL-2.1-or-later | ||
// | ||
// Copyright © 2020 ANSSI. All Rights Reserved. | ||
// | ||
// Author(s): fabienfl (ANSSI) | ||
// | ||
|
||
#pragma once | ||
|
||
#include <string_view> | ||
|
||
#include <fmt/format.h> | ||
|
||
namespace Orc { | ||
namespace Traits { | ||
|
||
template <typename T> | ||
struct TimeUtc; | ||
|
||
} // namespace Traits | ||
} // namespace Orc | ||
|
||
template <typename T> | ||
struct fmt::formatter<Orc::Traits::TimeUtc<T>> : public fmt::formatter<std::string_view> | ||
{ | ||
template <typename FormatContext> | ||
auto format(const Orc::Traits::TimeUtc<T>& time, FormatContext& ctx) -> decltype(ctx.out()); | ||
}; | ||
|
||
template <typename T> | ||
struct fmt::formatter<Orc::Traits::TimeUtc<T>, wchar_t> : public fmt::formatter<std::wstring_view, wchar_t> | ||
{ | ||
template <typename FormatContext> | ||
auto format(const Orc::Traits::TimeUtc<T>& time, FormatContext& ctx) -> decltype(ctx.out()); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// | ||
// SPDX-License-Identifier: LGPL-2.1-or-later | ||
// | ||
// Copyright © 2020 ANSSI. All Rights Reserved. | ||
// | ||
// Author(s): fabienfl (ANSSI) | ||
// | ||
|
||
#pragma once | ||
|
||
#include "Output/Text/Fmt/Fwd/TimeUtc.h" | ||
|
||
#include <array> | ||
#include <string> | ||
|
||
#include <windows.h> | ||
|
||
#include "Utils/TypeTraits.h" | ||
#include "Output/Text/Fmt/Fwd/SYSTEMTIME.h" | ||
|
||
namespace Orc { | ||
namespace Text { | ||
|
||
inline auto FormatSystemTimeUtcA(const SYSTEMTIME& st) | ||
{ | ||
return fmt::format( | ||
"{}-{:02}-{:02}T{:02}:{:02}:{:02}.{:03}Z", | ||
st.wYear, | ||
st.wMonth, | ||
st.wDay, | ||
st.wHour, | ||
st.wMinute, | ||
st.wSecond, | ||
st.wMilliseconds); | ||
} | ||
|
||
inline auto FormatSystemTimeUtcW(const SYSTEMTIME& st) | ||
{ | ||
return fmt::format( | ||
L"{}-{:02}-{:02}T{:02}:{:02}:{:02}.{:03}Z", | ||
st.wYear, | ||
st.wMonth, | ||
st.wDay, | ||
st.wHour, | ||
st.wMinute, | ||
st.wSecond, | ||
st.wMilliseconds); | ||
} | ||
|
||
} // namespace Text | ||
} // namespace Orc | ||
|
||
template <typename T> | ||
template <typename FormatContext> | ||
auto fmt::formatter<Orc::Traits::TimeUtc<T>>::format(const Orc::Traits::TimeUtc<T>& time, FormatContext& ctx) | ||
-> decltype(ctx.out()) | ||
{ | ||
std::string s = Orc::Text::FormatSystemTimeUtcA(time); | ||
return formatter<std::string_view>::format(s, ctx); | ||
} | ||
|
||
template <typename T> | ||
template <typename FormatContext> | ||
auto fmt::formatter<Orc::Traits::TimeUtc<T>, wchar_t>::format(const Orc::Traits::TimeUtc<T>& time, FormatContext& ctx) | ||
-> decltype(ctx.out()) | ||
{ | ||
std::wstring s = Orc::Text::FormatSystemTimeUtcW(time); | ||
return formatter<std::wstring_view, wchar_t>::format(s, ctx); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters