aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2015-03-20 22:03:15 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2015-03-20 22:03:15 +0100
commitf12bef0c3696f87c55210557fb3b056284e82494 (patch)
tree32fa94036a21f277be425e8a276e2e033529a76c
parent3af7994f38791deeb1c5b746c049e702abb48878 (diff)
downloadble-toys-f12bef0c3696f87c55210557fb3b056284e82494.tar.gz
ble-toys-f12bef0c3696f87c55210557fb3b056284e82494.tar.bz2
ble-toys-f12bef0c3696f87c55210557fb3b056284e82494.tar.xz
ble-toys-f12bef0c3696f87c55210557fb3b056284e82494.zip
o Support for converting from millisecond timestamps to second timestamps.
-rw-r--r--apps/sample-timestamp.cpp81
1 files changed, 62 insertions, 19 deletions
diff --git a/apps/sample-timestamp.cpp b/apps/sample-timestamp.cpp
index df4a89e..6672091 100644
--- a/apps/sample-timestamp.cpp
+++ b/apps/sample-timestamp.cpp
@@ -11,15 +11,44 @@ using namespace trygvis::apps;
using namespace trygvis::soil_moisture;
namespace po = boost::program_options;
+enum class time_resolution {
+ SECONDS,
+ MILLISECONDS,
+};
+
+std::ostream& operator<<(std::ostream& os, time_resolution const& type) {
+ if (type == time_resolution::SECONDS) {
+ os << "seconds";
+ } else if(type == time_resolution::MILLISECONDS) {
+ os << "milliseconds";
+ }
+
+ return os;
+}
+
+std::istream& operator>>(std::istream& is, time_resolution & type) {
+ string s;
+
+ is >> s;
+
+ if(s == "seconds") {
+ type = time_resolution::SECONDS;
+ } else if(s == "milliseconds") {
+ type = time_resolution::MILLISECONDS;
+ }
+
+ return is;
+}
+
class TimestampFixingSampleOutputStream : public SampleOutputStream {
public:
- TimestampFixingSampleOutputStream(shared_ptr<SampleOutputStream> output, KeyDictionary &dict, string timestamp_name, string now_name, time_t start_time) :
- timestamp_key(dict.indexOf(timestamp_name)), now_key(dict.indexOf(now_name)), start_time_(start_time), output_(output) {
+ TimestampFixingSampleOutputStream(shared_ptr<SampleOutputStream> output, KeyDictionary &dict, string timestamp_name, string relative_name, time_resolution input_time_resolution, time_t start_time) :
+ timestamp_key(dict.indexOf(timestamp_name)), relative_key(dict.indexOf(relative_name)), input_time_resolution_(input_time_resolution), start_time_(start_time), output_(output) {
}
virtual void write(SampleRecord const &sample) override {
- o<long> relative_time_o = sample.lexical_at<long>(now_key);
+ o<long> relative_time_o = sample.lexical_at<long>(relative_key);
if (!relative_time_o) {
return;
@@ -27,26 +56,35 @@ public:
long relative_time = relative_time_o.get();
- string new_value = std::to_string(start_time_ + relative_time);
+ long updated_time;
+ if (input_time_resolution_ == time_resolution::SECONDS) {
+ updated_time = start_time_ + relative_time;
+ } else if (input_time_resolution_ == time_resolution::MILLISECONDS) {
+ updated_time = start_time_ + relative_time / 1000;
+ }
+
+ string updated_value = std::to_string(updated_time);
- SampleRecord updatedSample(sample);
+ SampleRecord updated_sample(sample);
- updatedSample.set(timestamp_key, new_value);
+ updated_sample.set(timestamp_key, updated_value);
- output_->write(updatedSample);
+ output_->write(updated_sample);
};
private:
- const SampleKey* now_key, *timestamp_key;
+ const SampleKey* relative_key, *timestamp_key;
time_t start_time_;
+ time_resolution input_time_resolution_;
shared_ptr<SampleOutputStream> output_;
};
class sample_timestamp : public app {
private:
- string input_file, timestamp_name, now_name;
- SampleKey* now_key;
+ string input_file, timestamp_name, relative_name;
+ time_resolution relative_resolution;
+ SampleKey* relative_key;
public:
sample_timestamp() : input_file("") {
@@ -56,7 +94,8 @@ public:
options
("help", "produce this help message")
("input", po::value<string>(&input_file)->required())
- ("now-name", po::value<string>(&now_name)->default_value("now"))
+ ("relative-name", po::value<string>(&relative_name)->default_value("relative"))
+ ("relative-resolution", po::value<time_resolution>(&relative_resolution)->default_value(time_resolution::SECONDS))
("timestamp-name", po::value<string>(&timestamp_name)->default_value("timestamp"));
}
@@ -81,7 +120,7 @@ public:
KeyDictionary dict;
- now_key = dict.indexOf(now_name);
+ relative_key = dict.indexOf(relative_name);
auto sample_buffer = make_shared<VectorSampleOutputStream>();
auto parser = open_sample_input_stream(sample_buffer, dict);
@@ -104,24 +143,28 @@ public:
SampleRecord sample = *--sample_buffer->samples.end();
- o<string> s = sample.at(now_key);
+ o<string> s = sample.at(relative_key);
if (!s) {
- cerr << "Missing key '" + now_name + "'." << endl;
+ cerr << "Missing key '" + relative_name + "'." << endl;
cerr << "keys: " << sample.to_string() << endl;
return EXIT_FAILURE;
}
- long now;
+ long relative;
try {
- now = boost::lexical_cast<long>(s.get());
+ relative = boost::lexical_cast<long>(s.get());
} catch (const boost::bad_lexical_cast &e) {
cerr << "Bad integer value '" + s.get() + "'." << endl;
return EXIT_FAILURE;
}
- time_t start_time = end_time - now;
+ if (relative_resolution == time_resolution::MILLISECONDS) {
+ relative /= 1000;
+ }
+
+ time_t start_time = end_time - relative;
cerr << "end_time " << end_time << endl;
- cerr << "now " << now << endl;
+ cerr << "relative " << relative << endl;
cerr << "start_time " << start_time << endl;
// Restart the reading of the input file and add the adjusted timestamp
@@ -134,7 +177,7 @@ public:
unique_ptr<SampleOutputStream> unique_output_stream = open_sample_output_stream(shared_ptr<ostream>(&cout, noop_deleter), dict, parser->type());
shared_ptr<SampleOutputStream> output_stream{std::move(unique_output_stream)};
- auto p = make_shared<TimestampFixingSampleOutputStream>(output_stream, dict, timestamp_name, now_name, start_time);
+ shared_ptr<SampleOutputStream> p = make_shared<TimestampFixingSampleOutputStream>(output_stream, dict, timestamp_name, relative_name, relative_resolution, start_time);
parser = open_sample_input_stream(p, dict, parser->type());
int recordCount = 0;