From a4e035ba6447749c51f76838bab2705232c42fd6 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Thu, 9 Apr 2026 22:25:46 -0700 Subject: [PATCH] Replace gmtime/localtime with std::chrono (#8735) Fixes #8734. --- datalog/examples/printlog/printlog.cpp | 9 +++++---- ntcoreffi/src/main/native/cpp/DataLogManager.cpp | 15 +++++++-------- .../datalogtool/src/main/native/cpp/Exporter.cpp | 9 +++++---- .../src/main/native/cpp/system/DataLogManager.cpp | 15 +++++++-------- 4 files changed, 24 insertions(+), 24 deletions(-) diff --git a/datalog/examples/printlog/printlog.cpp b/datalog/examples/printlog/printlog.cpp index 12df2a2cd0..c1ec2fe622 100644 --- a/datalog/examples/printlog/printlog.cpp +++ b/datalog/examples/printlog/printlog.cpp @@ -2,7 +2,7 @@ // Open Source Software; you can modify and/or share it under the terms of // the WPILib BSD license file in the root directory of this project. -#include +#include #include #include @@ -93,9 +93,10 @@ int main(int argc, const char** argv) { if (entry->second.name == "systemTime" && entry->second.type == "int64") { int64_t val; if (record.GetInteger(&val)) { - std::time_t timeval = val / 1000000; - wpi::util::print(" {:%Y-%m-%d %H:%M:%S}.{:06}\n", - *std::localtime(&timeval), val % 1000000); + auto timeval = std::chrono::system_clock::time_point( + std::chrono::microseconds(val)); + wpi::util::print(" {:%Y-%m-%d %H:%M:%S}.{:06}\n", timeval, + val % 1000000); } else { wpi::util::print(" invalid\n"); } diff --git a/ntcoreffi/src/main/native/cpp/DataLogManager.cpp b/ntcoreffi/src/main/native/cpp/DataLogManager.cpp index d22a7ff169..4db9d62220 100644 --- a/ntcoreffi/src/main/native/cpp/DataLogManager.cpp +++ b/ntcoreffi/src/main/native/cpp/DataLogManager.cpp @@ -5,7 +5,7 @@ #include "DataLogManager.h" #include -#include +#include #include #include #include @@ -376,9 +376,8 @@ void Thread::Main() { } if (dsAttachCount > 50) { // 1 second if (RobotController::IsSystemTimeValid()) { - std::time_t now = std::time(nullptr); - auto tm = std::gmtime(&now); - m_log.SetFilename(fmt::format("WPILIB_{:%Y%m%d_%H%M%S}.wpilog", *tm)); + auto now = std::chrono::system_clock::now(); + m_log.SetFilename(fmt::format("WPILIB_{:%Y%m%d_%H%M%S}.wpilog", now)); dsRenamed = true; } else { dsAttachCount = 0; // wait a bit and try again @@ -415,11 +414,11 @@ void Thread::Main() { matchTypeChar = '_'; break; } - std::time_t now = std::time(nullptr); + auto now = std::chrono::system_clock::now(); m_log.SetFilename( - fmt::format("WPILIB_{:%Y%m%d_%H%M%S}_{}_{}{}.wpilog", - *std::gmtime(&now), DriverStation::GetEventName(), - matchTypeChar, DriverStation::GetMatchNumber())); + fmt::format("WPILIB_{:%Y%m%d_%H%M%S}_{}_{}{}.wpilog", now, + DriverStation::GetEventName(), matchTypeChar, + DriverStation::GetMatchNumber())); fmsRenamed = true; dsRenamed = true; // don't override FMS rename } diff --git a/tools/datalogtool/src/main/native/cpp/Exporter.cpp b/tools/datalogtool/src/main/native/cpp/Exporter.cpp index 24c0b2cc6e..73bf85d1da 100644 --- a/tools/datalogtool/src/main/native/cpp/Exporter.cpp +++ b/tools/datalogtool/src/main/native/cpp/Exporter.cpp @@ -5,7 +5,7 @@ #include "Exporter.hpp" #include -#include +#include #include #include #include @@ -462,9 +462,10 @@ static void ValueToCsv(wpi::util::raw_ostream& os, const Entry& entry, if (entry.name == "systemTime" && entry.type == "int64") { int64_t val; if (record.GetInteger(&val)) { - std::time_t timeval = val / 1000000; - wpi::util::print(os, "{:%Y-%m-%d %H:%M:%S}.{:06}", - *std::localtime(&timeval), val % 1000000); + auto timeval = + std::chrono::system_clock::time_point(std::chrono::microseconds(val)); + wpi::util::print(os, "{:%Y-%m-%d %H:%M:%S}.{:06}", timeval, + val % 1000000); return; } } else if (entry.type == "double") { diff --git a/wpilibc/src/main/native/cpp/system/DataLogManager.cpp b/wpilibc/src/main/native/cpp/system/DataLogManager.cpp index 9c487b2cf2..c7c6d608f4 100644 --- a/wpilibc/src/main/native/cpp/system/DataLogManager.cpp +++ b/wpilibc/src/main/native/cpp/system/DataLogManager.cpp @@ -5,7 +5,7 @@ #include "wpi/system/DataLogManager.hpp" #include -#include +#include #include #include #include @@ -225,9 +225,8 @@ void Thread::Main() { } if (dsAttachCount > 50) { // 1 second if (RobotController::IsSystemTimeValid()) { - std::time_t now = std::time(nullptr); - auto tm = std::gmtime(&now); - m_log.SetFilename(fmt::format("WPILIB_{:%Y%m%d_%H%M%S}.wpilog", *tm)); + auto now = std::chrono::system_clock::now(); + m_log.SetFilename(fmt::format("WPILIB_{:%Y%m%d_%H%M%S}.wpilog", now)); dsRenamed = true; } else { dsAttachCount = 0; // wait a bit and try again @@ -263,11 +262,11 @@ void Thread::Main() { matchTypeChar = '_'; break; } - std::time_t now = std::time(nullptr); + auto now = std::chrono::system_clock::now(); m_log.SetFilename( - fmt::format("WPILIB_{:%Y%m%d_%H%M%S}_{}_{}{}.wpilog", - *std::gmtime(&now), DriverStation::GetEventName(), - matchTypeChar, DriverStation::GetMatchNumber())); + fmt::format("WPILIB_{:%Y%m%d_%H%M%S}_{}_{}{}.wpilog", now, + DriverStation::GetEventName(), matchTypeChar, + DriverStation::GetMatchNumber())); fmsRenamed = true; dsRenamed = true; // don't override FMS rename }