diff --git a/src/filewriter.cpp b/src/filewriter.cpp index 89abc81f20553ac64889db8fbc7853c7418b74e1..a86db052ff8c6b4ac0d5740ba0a540da20186fe1 100644 --- a/src/filewriter.cpp +++ b/src/filewriter.cpp @@ -8,7 +8,9 @@ #include <stdexcept> #include <vector> #include <iostream> -#include <ctime> +#include <chrono> +#include <sstream> +#include <iomanip> FileWriter::FileWriter(std::string &filename_template, size_t num_channels, size_t sample_rate) : filename_template(filename_template), num_channels(num_channels), sample_rate(sample_rate) { @@ -22,9 +24,20 @@ FileWriter::~FileWriter() { std::string FileWriter::generate_filename() { // this has of course nothing to do with file writing and could be pulled // out, but I doubt anybody will ever care - time_t timer; - time(&timer); - struct tm *timeinfo = localtime(&timer); + using namespace std::chrono; + auto miliseconds = duration_cast<milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count(); + long seconds = miliseconds/1000; + struct tm *timeinfo = localtime(&seconds); + + size_t found = filename_template.find("%z"); + while(found != std::string::npos){ + std::stringstream ss; + ss << std::setw(3) << std::setfill('0') << miliseconds%1000; + std::string s = ss.str(); + filename_template.replace(found, 2, s); + found = filename_template.find("%z", found+3); + } + size_t length = 0; size_t space = 0; std::vector<char> buffer; diff --git a/src/main.cpp b/src/main.cpp index 3e6e06bd91ce7530e62903208be88e36f7cfe1e9..8adee388f5b5a329f931cf094e9d550cf2ff5386 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -20,7 +20,7 @@ void print_usage(char *name) { std::cout << " CHANNELS: number of channels to record (1 to 5)" << std::endl; std::cout << " RATE: sample rate in Hz to record at (integral number)" << std::endl; std::cout << " FILENAME: output file name; should include strftime() format specifiers" << std::endl; - std::cout << " if CHUNKLEN is specified. Example: location/recording_%Y%m%dT%H%M%S.wav" << std::endl; + std::cout << " if CHUNKLEN is specified. For miliseconds, use %z. Example: location/recording_%Y%m%dT%H%M%S%z.wav" << std::endl; std::cout << " CHUNKLEN: length per output file in seconds; will start a new file whenever" << std::endl; std::cout << " this length is reached. If not given or zero, will record a single file." << std::endl; std::cout << " TOTALLEN: total recording length; will stop when this length is reached." << std::endl;