Skip to content

Commit

Permalink
OrcLib: TypeTraits: add TimeUtc<> strong type
Browse files Browse the repository at this point in the history
  • Loading branch information
fabienfl-orc committed Nov 9, 2020
1 parent 7368439 commit 1ea8365
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/OrcLib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,7 @@ set(SRC_INOUT_TEXT_FMT
"Output/Text/Fmt/PartitionFlags.h"
"Output/Text/Fmt/PartitionType.h"
"Output/Text/Fmt/SYSTEMTIME.h"
"Output/Text/Fmt/TimeUtc.h"
"Output/Text/Fmt/Fwd/ByteQuantity.h"
"Output/Text/Fmt/Fwd/CryptoHashStreamAlgorithm.h"
"Output/Text/Fmt/Fwd/error_code.h"
Expand All @@ -591,7 +592,8 @@ set(SRC_INOUT_TEXT_FMT
"Output/Text/Fmt/Fwd/Partition.h"
"Output/Text/Fmt/Fwd/PartitionFlags.h"
"Output/Text/Fmt/Fwd/PartitionType.h"
"Output/Text/Fmt/Fwd/SYSTEMTIME.h")
"Output/Text/Fmt/Fwd/SYSTEMTIME.h"
"Output/Text/Fmt/Fwd/TimeUtc.h")

source_group(In&Out\\Output\\Text\\Fmt FILES ${SRC_INOUT_TEXT_FMT})

Expand Down
1 change: 1 addition & 0 deletions src/OrcLib/Output/Text/Fmt/Formatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@
#include "Output/Text/Fmt/Fwd/PartitionType.h"
#include "Output/Text/Fmt/Fwd/PartitionFlags.h"
#include "Output/Text/Fmt/Fwd/SYSTEMTIME.h"
#include "Output/Text/Fmt/Fwd/TimeUtc.h"
36 changes: 36 additions & 0 deletions src/OrcLib/Output/Text/Fmt/Fwd/TimeUtc.h
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());
};
69 changes: 69 additions & 0 deletions src/OrcLib/Output/Text/Fmt/TimeUtc.h
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);
}
18 changes: 18 additions & 0 deletions src/OrcLib/Utils/TypeTraits.h
Original file line number Diff line number Diff line change
Expand Up @@ -386,5 +386,23 @@ struct Offset
T value;
};

//
// Strong type for UTC time
//
template <class T>
struct TimeUtc
{
using value_type = T;

TimeUtc(T quantity)
: value(quantity)
{
}

operator T&() { return value; }
operator T() const { return value; }
T value;
};

} // namespace Traits
} // namespace Orc

0 comments on commit 1ea8365

Please sign in to comment.