Merge pull request #225 from rhalkyard/macbinary-timestamp

Set a valid timestamp on MacBinary output.
FIXME: makes nix-builds of samples non-deterministic, will need to add support for SOURCE_DATE_EPOCH.
This commit is contained in:
Wolfgang Thaller 2024-01-15 21:06:33 +01:00 committed by GitHub
commit 9d542ac1c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 2 deletions

View File

@ -3,6 +3,8 @@
#include <boost/filesystem.hpp>
#include "boost/filesystem/fstream.hpp"
#include <chrono>
#include <ctime>
#include <sstream>
#include <iostream>
@ -73,6 +75,13 @@ static void writeMacBinary(std::ostream& out, std::string filename,
const std::string& rsrcBytes = resstream.str();
// Calculate Mac-style timestamp (seconds since 1 January 1904 00:00:00)
std::tm mac_epoch_tm = {
0, 0, 0, // 00:00:00
1, 0, 4 // 1 January 1904
};
auto mac_epoch = std::chrono::system_clock::from_time_t(std::mktime(&mac_epoch_tm));
uint32_t mac_time = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now() - mac_epoch).count();
std::ostringstream header;
byte(header, 0);
@ -91,8 +100,8 @@ static void writeMacBinary(std::ostream& out, std::string filename,
byte(header, 0);
longword(header, (int)data.size());
longword(header, (int)rsrcBytes.size());
longword(header, 0); // creation date
longword(header, 0); // modification date
longword(header, mac_time); // creation date
longword(header, mac_time); // modification date
while((int)header.tellp() < 124)
byte(header,0);
std::string headerData = header.str();