Skip to content
Open
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
2 changes: 2 additions & 0 deletions src/windows/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ set(SOURCES
string.cpp
SubProcess.cpp
svccomm.cpp
timestamp.cpp
VTSupport.cpp
WindowsUpdateIntegration.cpp
WSLCContainerLauncher.cpp
Expand Down Expand Up @@ -125,6 +126,7 @@ set(HEADERS
Stringify.h
SubProcess.h
svccomm.hpp
timestamp.hpp
VTSupport.h
WindowsUpdateIntegration.h
WSLCContainerLauncher.h
Expand Down
1 change: 1 addition & 0 deletions src/windows/common/precomp.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ Module Name:
#include "conncheckshared.h"
#include "helpers.hpp"
#include "string.hpp"
#include "timestamp.hpp"
#include "filesystem.hpp"
#include "Localization.h"
#include "wslutil.h"
Expand Down
63 changes: 0 additions & 63 deletions src/windows/common/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,66 +425,3 @@ std::string wsl::windows::common::string::TruncateId(_In_ std::string_view id, b
{
return TruncateIdImpl(id, shortenLength);
}

std::uint64_t wsl::windows::common::string::Rfc3339ToEpoch(const std::string& timestamp)
{
std::chrono::sys_seconds utcSeconds;
std::istringstream stream(timestamp);
stream >> std::chrono::parse("%FT%H:%M:%S%Z", utcSeconds);
THROW_HR_IF_MSG(E_INVALIDARG, stream.fail(), "Failed to parse timestamp '%hs'", timestamp.c_str());

return static_cast<std::uint64_t>(utcSeconds.time_since_epoch().count());
}

std::string wsl::windows::common::string::EpochToLocalDisplayTime(LONGLONG timestamp)
{
const auto time =
std::chrono::floor<std::chrono::seconds>(std::chrono::system_clock::from_time_t(static_cast<std::time_t>(timestamp)));

try
{
const auto* zone = std::chrono::current_zone();
return std::format("{:%F %T %z} {}", std::chrono::zoned_time{zone, time}, zone->get_info(time).abbrev);
}
catch (...)
{
// The time zone database is unavailable, so report UTC rather than failing the caller.
LOG_CAUGHT_EXCEPTION();
return std::format("{:%F %T} +0000 UTC", time);
}
}

std::string wsl::windows::common::string::Rfc3339ToUtcDisplayTime(std::string_view timestamp)
{
if (timestamp.empty())
{
return {};
}

// Fractional digits vary in length, so they are captured verbatim and re-inserted after formatting.
std::string parsable{timestamp};
std::string fraction;
const auto separator = parsable.find('.');
if (separator != std::string::npos)
{
auto end = separator + 1;
while (end < parsable.size() && (std::isdigit(static_cast<unsigned char>(parsable[end])) != 0))
{
end++;
}

fraction = parsable.substr(separator, end - separator);
parsable.erase(separator, end - separator);
}

std::chrono::sys_seconds parsed{};
std::istringstream stream(parsable);
stream >> std::chrono::parse("%FT%H:%M:%S%Z", parsed);
if (stream.fail())
{
return std::string{timestamp};
}

// Network timestamps are reported in UTC rather than the local time zone.
return std::format("{:%F %T}{} +0000 UTC", parsed, fraction);
}
12 changes: 0 additions & 12 deletions src/windows/common/string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,6 @@ std::string WideToMultiByte(_In_ std::wstring_view Source);
std::wstring TruncateId(_In_ std::wstring_view id, bool shortenLength = true);
std::string TruncateId(_In_ std::string_view id, bool shortenLength = true);

// Converts an RFC 3339 timestamp to seconds since the unix epoch. Only the 'Z' zone designator is
// accepted; numeric offsets are not.
std::uint64_t Rfc3339ToEpoch(const std::string& timestamp);

// Renders seconds since the unix epoch in the local time zone, using the layout
// "2006-01-02 15:04:05 -0700 MST". Falls back to UTC when the time zone database is unavailable.
std::string EpochToLocalDisplayTime(LONGLONG timestamp);

// Renders an RFC 3339 timestamp in the same layout, but as UTC and with its fractional seconds
// preserved. The input is returned unchanged when it cannot be parsed.
std::string Rfc3339ToUtcDisplayTime(std::string_view timestamp);

// Template implementation for TruncateId to avoid code duplication.
// Algorithm inspired from Moby for consistency in presentation of shortened IDs.
// Always strips the algorithm prefix (e.g., "sha256:") if present, and optionally shortens to 12 characters.
Expand Down
Loading
Loading