Skip to content

Commit

Permalink
Merge pull request #1009 from stan-dev/add_datetime_to_CSV
Browse files Browse the repository at this point in the history
Add datetime to CSV
  • Loading branch information
rok-cesnovar authored May 19, 2021
2 parents da4be45 + 837a2fe commit 182c579
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/cmdstan/command.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <cmdstan/arguments/arg_profile_file.hpp>
#include <cmdstan/arguments/argument_parser.hpp>
#include <cmdstan/io/json/json_data.hpp>
#include <cmdstan/write_datetime.hpp>
#include <cmdstan/write_model_compile_info.hpp>
#include <cmdstan/write_model.hpp>
#include <cmdstan/write_opencl_device.hpp>
Expand Down Expand Up @@ -230,6 +231,7 @@ int command(int argc, const char *argv[]) {

write_stan(sample_writer);
write_model(sample_writer, model.model_name());
write_datetime(sample_writer);
parser.print(sample_writer);
write_parallel_info(sample_writer);
write_opencl_device(sample_writer);
Expand Down
28 changes: 28 additions & 0 deletions src/cmdstan/write_datetime.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef CMDSTAN_WRITE_DATETIME_HPP
#define CMDSTAN_WRITE_DATETIME_HPP

#include <stan/callbacks/writer.hpp>
#include <stan/version.hpp>
#include <chrono>
#include <iomanip>
#include <string>

namespace cmdstan {

void write_datetime(stan::callbacks::writer& writer) {
const std::time_t current_datetime
= std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
std::tm* curr_tm = std::gmtime(&current_datetime);
std::stringstream current_datetime_msg;
current_datetime_msg << "start_datetime = " << std::setfill('0')
<< (1900 + curr_tm->tm_year) << "-" << std::setw(2)
<< (curr_tm->tm_mon + 1) << "-" << std::setw(2)
<< curr_tm->tm_mday << " " << std::setw(2)
<< curr_tm->tm_hour << ":" << std::setw(2)
<< curr_tm->tm_min << ":" << std::setw(2)
<< curr_tm->tm_sec << " UTC";
writer(current_datetime_msg.str());
}

} // namespace cmdstan
#endif
68 changes: 68 additions & 0 deletions src/test/interface/datetime_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <stan/mcmc/chains.hpp>
#include <stan/services/error_codes.hpp>
#include <test/utility.hpp>
#include <gtest/gtest.h>
#include <ctime>
#include <fstream>

using cmdstan::test::convert_model_path;
using cmdstan::test::run_command;
using cmdstan::test::run_command_output;

TEST(interface, csv_header_consistency) {
std::vector<std::string> model_path;
model_path.push_back("src");
model_path.push_back("test");
model_path.push_back("test-models");
model_path.push_back("test_model");

std::string path = convert_model_path(model_path);
std::string samples = path + ".csv";

std::string command
= path + " sample num_warmup=1 num_samples=1" + " output file=" + samples;

run_command_output out = run_command(command);
EXPECT_EQ(int(stan::services::error_codes::OK), out.err_code);
EXPECT_FALSE(out.hasError);

std::ifstream in;
in.open(samples.c_str());

std::stringstream ss;
std::string line;

while (in.peek() == '#') {
std::getline(in, line);
ss << line << '\n';
}
ss.seekg(std::ios_base::beg);

char comment;
std::string lhs;

std::string name;
std::string value;

while (ss.good()) {
ss >> comment;
std::getline(ss, lhs);
size_t equal = lhs.find("=");
if (equal != std::string::npos) {
name = lhs.substr(0, equal);
boost::trim(name);
if (name.compare("start_datetime") == 0) {
value = lhs.substr(equal + 1, lhs.size());
boost::trim(value);
break;
}
}
}
EXPECT_EQ(value.size(), 23);
time_t theTime = time(NULL);
struct tm *aTime = std::gmtime(&theTime);

EXPECT_EQ(std::atoi(value.substr(0, 4).c_str()), (aTime->tm_year + 1900));
EXPECT_EQ(value.substr(value.size() - 3, 3), "UTC");
in.close();
}

0 comments on commit 182c579

Please sign in to comment.