Skip to content

Commit

Permalink
OrcLib: Utils: add Time.cpp, Time.h
Browse files Browse the repository at this point in the history
  • Loading branch information
fabienfl-orc committed Jan 26, 2021
1 parent dfd7eab commit f9b13f4
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/OrcCommand/UtilitiesMain.cpp
Expand Up @@ -38,8 +38,8 @@ using namespace Orc;
using namespace Orc::Command;

UtilitiesMain::UtilitiesMain()
: theStartTime({0})
, theFinishTime({0})
: theStartTime()
, theFinishTime()
{
theStartTickCount = 0L;
theFinishTickCount = 0L;
Expand Down
2 changes: 2 additions & 0 deletions src/OrcLib/CMakeLists.txt
Expand Up @@ -722,6 +722,8 @@ set(SRC_UTILITIES
"Utils/Iconv.h"
"Utils/MakeArray.h"
"Utils/Result.h"
"Utils/Time.cpp"
"Utils/Time.h"
"Utils/TypeTraits.h"
)

Expand Down
98 changes: 98 additions & 0 deletions src/OrcLib/Utils/Time.cpp
@@ -0,0 +1,98 @@
//
// SPDX-License-Identifier: LGPL-2.1-or-later
//
// Copyright © 2020 ANSSI. All Rights Reserved.
//
// Author(s): fabienfl (ANSSI)
//

#include <fmt/format.h>

#include "Utils/Time.h"

using namespace Orc::Traits;

namespace Orc {

Result<std::chrono::system_clock::time_point> FromSystemTime(const Traits::TimeUtc<SYSTEMTIME>& st)
{
return FromSystemTime(st.value);
}

Result<std::chrono::system_clock::time_point> FromSystemTime(const SYSTEMTIME& st)
{
FILETIME ft;
if (!SystemTimeToFileTime(&st, &ft))
{
return LastWin32Error();
}

return FromFileTime(ft);
}

std::chrono::system_clock::time_point FromFileTime(const FILETIME& ft)
{
const auto time = ToTime(ft);
return std::chrono::system_clock::from_time_t(time);
}

FILETIME ToFileTime(const std::chrono::system_clock::time_point& tp)
{
const auto time = std::chrono::system_clock::to_time_t(tp);
return ToFileTime(time);
}

time_t ToTime(const FILETIME& ft)
{
// A FILETIME is the number of 100-nanosecond intervals since January 1, 1601
// A time_t is the number of 1 - second intervals since January 1, 1970
ULARGE_INTEGER ull;
ull.LowPart = ft.dwLowDateTime;
ull.HighPart = ft.dwHighDateTime;
return ull.QuadPart / 10000000ULL - 11644473600ULL;
}

FILETIME ToFileTime(const time_t& time)
{
ULARGE_INTEGER ll;
ll.QuadPart = UInt32x32To64(time, 10000000) + 116444736000000000;

FILETIME ft;
ft.dwLowDateTime = ll.LowPart;
ft.dwHighDateTime = ll.HighPart;
return ft;
}

Result<TimeUtc<SYSTEMTIME>> ToSystemTime(const std::chrono::system_clock::time_point& tp)
{
const auto time = std::chrono::system_clock::to_time_t(tp);
const auto ft = ToFileTime(time);

Traits::TimeUtc<SYSTEMTIME> st;
if (!FileTimeToSystemTime(&ft, &st.value))
{
return LastWin32Error();
}

return st;
}

std::wstring ToStringIso8601(const Traits::TimeUtc<SYSTEMTIME>& time)
{
const auto& st = time.value;
return fmt::format(
L"{}-{:02}-{:02}T{:02}:{:02}:{:02}Z", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
}

Result<std::wstring> ToStringIso8601(const std::chrono::system_clock::time_point& tp)
{
const auto time = ToSystemTime(tp);
if (time.has_error())
{
return time.error();
}

return ToStringIso8601(time.value());
}

} // namespace Orc
33 changes: 33 additions & 0 deletions src/OrcLib/Utils/Time.h
@@ -0,0 +1,33 @@
//
// SPDX-License-Identifier: LGPL-2.1-or-later
//
// Copyright © 2020 ANSSI. All Rights Reserved.
//
// Author(s): fabienfl (ANSSI)
//
#pragma once

#include <chrono>
#include <time.h>

#include <windows.h>

#include "TypeTraits.h"

namespace Orc {

Result<std::chrono::system_clock::time_point> FromSystemTime(const SYSTEMTIME& st);
Result<std::chrono::system_clock::time_point> FromSystemTime(const Traits::TimeUtc<SYSTEMTIME>& st);
std::chrono::system_clock::time_point FromFileTime(const FILETIME& ft);

FILETIME ToFileTime(const std::chrono::system_clock::time_point& tp);
FILETIME ToFileTime(const time_t& time);

Result<Traits::TimeUtc<SYSTEMTIME>> ToSystemTime(const std::chrono::system_clock::time_point& tp);

time_t ToTime(const FILETIME& ft);

std::wstring ToStringIso8601(const Traits::TimeUtc<SYSTEMTIME>& time);
Result<std::wstring> ToStringIso8601(const std::chrono::system_clock::time_point& tp);

} // namespace Orc
5 changes: 3 additions & 2 deletions src/OrcLib/Utils/TypeTraits.h
Expand Up @@ -394,8 +394,9 @@ struct TimeUtc
{
using value_type = T;

TimeUtc(T quantity)
: value(quantity)
template <typename... Args>
explicit TimeUtc(Args&&... args)
: value(std::forward<Args>(args)...)
{
}

Expand Down

0 comments on commit f9b13f4

Please sign in to comment.