ewmscp  ..
Typedefs | Functions
dateTest.cpp File Reference

(v0.19-24-g0617ca1 with changes)

#include <OptionsChrono.h>
#include <time.h>
Include dependency graph for dateTest.cpp:

Go to the source code of this file.

Typedefs

typedef std::chrono::system_clock clock_type
 

Functions

std::ostream & operator<< (std::ostream &stream, const clock_type::time_point &t)
 
std::ostream & operator<< (std::ostream &stream, const clock_type::duration &dt)
 
int main (int argc, const char *argv[])
 

Typedef Documentation

◆ clock_type

typedef std::chrono::system_clock clock_type

Definition at line 4 of file dateTest.cpp.

Function Documentation

◆ main()

int main ( int  argc,
const char *  argv[] 
)

Definition at line 19 of file dateTest.cpp.

19  {
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 }

References options::base::fIsSet(), and quiet.

Here is the call graph for this function:

◆ operator<<() [1/2]

std::ostream& operator<< ( std::ostream &  stream,
const clock_type::duration &  dt 
)

Definition at line 12 of file dateTest.cpp.

13  {
14  stream << std::fixed;
15  stream << std::chrono::duration_cast<std::chrono::duration<double>>(dt).count();
16  return stream;
17 }

◆ operator<<() [2/2]

std::ostream& operator<< ( std::ostream &  stream,
const clock_type::time_point &  t 
)

Definition at line 6 of file dateTest.cpp.

7  {
8  stream << std::fixed;
9  stream << std::chrono::duration_cast<std::chrono::duration<double>>(t.time_since_epoch()).count();
10  return stream;
11 }
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
quiet
static options::single< bool > quiet('q', "quiet", "be quiet, no prefix to output lines")