ewmscp  ..
dateTest.cpp
Go to the documentation of this file.
1 #include <OptionsChrono.h>
2 #include <time.h>
3 
4 typedef std::chrono::system_clock clock_type;
5 
6 std::ostream& operator<< (std::ostream& stream,
7  const clock_type::time_point& t) {
8  stream << std::fixed;
9  stream << std::chrono::duration_cast<std::chrono::duration<double>>(t.time_since_epoch()).count();
10  return stream;
11 }
12 std::ostream& operator<< (std::ostream& stream,
13  const clock_type::duration& dt) {
14  stream << std::fixed;
15  stream << std::chrono::duration_cast<std::chrono::duration<double>>(dt).count();
16  return stream;
17 }
18 
19 int main(int argc, const char* argv[]) {
20  options::parser parser("", "", {});
22  "a time point");
24  "a duration");
25  options::single<std::string> readFormat('r',"readFormat",
26  "timestamp read format",
27  "%Y-%m-%d_%H:%M:%S%z");
28  options::single<std::string> writeFormat('w',"writeFormat",
29  "timestamp write format",
30  "%Y-%m-%d_%H:%M:%S%z");
31  options::single<bool> quiet('q',"quiet","be quiet, minimal out");
32  auto strings = parser.fParse(argc, argv);
33 
34  if (t.fIsSet()) {
35  if (quiet) {
36  std::cout << std::chrono::duration_cast<std::chrono::seconds>(t.time_since_epoch()).count() << "\n";
37  } else {
38  std::cout << " t is " << t << "\n";
39  }
40  }
41  if (d.fIsSet()) {
42  if (quiet) {
43  std::cout << std::chrono::duration_cast<std::chrono::seconds>(d).count() << "\n";
44  } else {
45  std::cout << "dt is " << d << "\n";
46  }
47  }
48 
49  for (const auto& string : strings) {
50  struct tm tmStruct{};
51 
52  strptime(string.c_str(), readFormat.c_str(), &tmStruct);
53  auto ts = clock_type::time_point(std::chrono::duration_cast<clock_type::duration>(std::chrono::duration<double>(mktime(&tmStruct))));
54  if (readFormat.back() == 'z') {
55  auto tzStart=string.find_last_of("+-");
56  if (tzStart != std::string::npos) {
57  auto offset = std::stoi(string.substr(tzStart));
58  ts -= std::chrono::hours(offset/100);
59  ts -= std::chrono::minutes(offset%100);
60  }
61  }
62  if (quiet) {
63  std::cout << std::chrono::duration_cast<std::chrono::seconds>(ts.time_since_epoch()).count() << "\n";
64  } else {
65  std::cout << "\"" << string << "\" as time: " << ts << "\n";
66  }
67  }
68 }
options::parser
class that contains the parser, i.e. does that option handling
Definition: Options.h:363
options::single
generic option class with any type that can be used with std::istream and std::ostream
Definition: Options.h:533
options::single< bool >
class specialisation for options of type bool
Definition: Options.h:595
options::base::fIsSet
virtual bool fIsSet() const
check if this option was set, regardless of from command line or config file
Definition: Options.h:263
OptionsChrono.h
operator<<
std::ostream & operator<<(std::ostream &stream, const clock_type::time_point &t)
Definition: dateTest.cpp:6
clock_type
std::chrono::system_clock clock_type
Definition: dateTest.cpp:4
quiet
static options::single< bool > quiet('q', "quiet", "be quiet, no prefix to output lines")
main
int main(int argc, const char *argv[])
Definition: dateTest.cpp:19