From dbfc72c491071544cdb8d068d7f709b94c963951 Mon Sep 17 00:00:00 2001 From: Diomendius <42310725+Diomendius@users.noreply.github.com> Date: Tue, 11 Jun 2024 02:59:13 +1200 Subject: [PATCH] Add MakeTimestamp to CMake, port to *nix --- CMakeLists.txt | 4 +++ MakeTimestamp/MakeTimestamp.cpp | 47 +++++++++++++++++++++++++++------ 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7b52ac8..7a39c19 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -349,5 +349,9 @@ target_include_directories(gpr2gpa PRIVATE ) target_link_libraries(gpr2gpa PortabilityLayer MacRomanConversion zlib) +add_executable(MakeTimestamp MakeTimestamp/MakeTimestamp.cpp) +target_include_directories(MakeTimestamp PRIVATE PortabilityLayer UnixCompat) +target_link_libraries(MakeTimestamp PortabilityLayer) + install (TARGETS ${EXECNAME}) diff --git a/MakeTimestamp/MakeTimestamp.cpp b/MakeTimestamp/MakeTimestamp.cpp index bbc9e4e..41d4fa0 100644 --- a/MakeTimestamp/MakeTimestamp.cpp +++ b/MakeTimestamp/MakeTimestamp.cpp @@ -1,6 +1,16 @@ +#ifdef _WIN32 #include -#include + #include +#else +#include + +#include + +#include "UnixCompat.h" +#endif + +#include #include "CombinedTimestamp.h" @@ -13,6 +23,7 @@ int main(int argc, const char **argv) return -1; } +#ifdef _WIN32 SYSTEMTIME epochStart; epochStart.wYear = 1904; epochStart.wMonth = 1; @@ -34,13 +45,6 @@ int main(int argc, const char **argv) int64_t currentTime64 = (static_cast(timestampFT.dwLowDateTime) & 0xffffffff) | (static_cast(timestampFT.dwHighDateTime) << 32); int64_t timeDelta = (currentTime64 - epochStart64) / 10000000; - FILE *f = nullptr; - if (fopen_s(&f, argv[1], "wb")) - { - fprintf(stderr, "Error opening output file"); - return -1; - } - TIME_ZONE_INFORMATION tz; GetTimeZoneInformation(&tz); @@ -60,9 +64,36 @@ int main(int argc, const char **argv) ts.m_localHour = localST.wHour; ts.m_localMinute = localST.wMinute; ts.m_localSecond = localST.wSecond; +#else + time_t currentTimeUnix = time(nullptr); + + tm *currentTimeStruct = localtime(¤tTimeUnix); + if (currentTimeStruct == nullptr) { + fprintf(stderr, "Error converting system time to calendar format"); + return -1; + } + + PortabilityLayer::CombinedTimestamp ts; + ts.SetMacEpochTime(currentTimeUnix + ts.kMacEpochToUTC); + + ts.SetLocalYear(currentTimeStruct->tm_year + 1900); + ts.m_localMonth = currentTimeStruct->tm_mon; + ts.m_localDay = currentTimeStruct->tm_mday; + + ts.m_localHour = currentTimeStruct->tm_hour; + ts.m_localMinute = currentTimeStruct->tm_min; + ts.m_localSecond = currentTimeStruct->tm_sec; +#endif memset(ts.m_padding, 0, sizeof(ts.m_padding)); + FILE *f; + if (fopen_s(&f, argv[1], "wb")) + { + fprintf(stderr, "Error opening output file"); + return -1; + } + fwrite(&ts, sizeof(ts), 1, f); fclose(f);