-
-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1009 from stan-dev/add_datetime_to_CSV
Add datetime to CSV
- Loading branch information
Showing
3 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(¤t_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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |