date_time.h 3.89 KB
Newer Older
1 2 3 4 5 6 7
#ifndef AIRMAP_DATE_TIME_H_
#define AIRMAP_DATE_TIME_H_

#include <memory>
#include <string>

namespace airmap {
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
class DateTime;
template <typename Tag>
class Duration;

namespace detail {
class Duration;
}  // namespace detail

namespace tag {

struct Hours {};
struct Minutes {};
struct Seconds {};
struct Milliseconds {};
struct Microseconds {};

}  // namespace tag

using Hours        = Duration<tag::Hours>;
using Minutes      = Duration<tag::Minutes>;
using Seconds      = Duration<tag::Seconds>;
using Milliseconds = Duration<tag::Milliseconds>;
using Microseconds = Duration<tag::Microseconds>;
31 32

/// Clock marks the reference for time measurements.
33 34 35 36 37 38 39 40 41 42 43 44 45
class Clock {
 public:
  static DateTime universal_time();
  static DateTime local_time();
};

namespace boost_iso {

DateTime datetime(const std::string &iso_time);
std::string to_iso_string(const DateTime &);

}  // namespace boost_iso

46
/// DateTime marks a specific point in time, in reference to Clock.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
class DateTime {
 public:
  DateTime();
  ~DateTime();
  DateTime(DateTime const &);
  DateTime(DateTime &&);
  DateTime &operator=(const DateTime &);
  DateTime &operator=(DateTime &&);

  DateTime operator+(const detail::Duration &) const;
  Microseconds operator-(const DateTime &) const;
  bool operator==(const DateTime &) const;
  bool operator!=(const DateTime &) const;

  friend std::istream &operator>>(std::istream &, DateTime &);
  friend std::ostream &operator<<(std::ostream &, const DateTime &);

  DateTime date() const;
  Microseconds time_of_day() const;

 private:
  struct Impl;
  std::unique_ptr<Impl> impl;

  explicit DateTime(std::unique_ptr<Impl> &&);
  friend DateTime Clock::universal_time();
  friend DateTime Clock::local_time();
  friend DateTime boost_iso::datetime(const std::string &iso_time);
  friend std::string boost_iso::to_iso_string(const DateTime &datetime);
};

Hours hours(int64_t raw);
Minutes minutes(int64_t raw);
Seconds seconds(int64_t raw);
Milliseconds milliseconds(int64_t raw);
Microseconds microseconds(int64_t raw);

namespace detail {

class Duration {
 public:
  Duration();
  ~Duration();
  Duration(Duration const &old);
  Duration &operator=(const Duration &);

  uint64_t total_seconds() const;
  uint64_t total_milliseconds() const;
  uint64_t total_microseconds() const;

  uint64_t hours() const;

 private:
  struct Impl;
  std::unique_ptr<Impl> impl;

  friend DateTime DateTime::operator+(const detail::Duration &) const;
  friend Microseconds DateTime::operator-(const DateTime &) const;
  friend Microseconds DateTime::time_of_day() const;

  friend Hours airmap::hours(int64_t raw);
  friend Minutes airmap::minutes(int64_t raw);
  friend Seconds airmap::seconds(int64_t raw);
  friend Milliseconds airmap::milliseconds(int64_t raw);
  friend Microseconds airmap::microseconds(int64_t raw);
};

}  // namespace detail

template <typename Tag>
class Duration : public detail::Duration {};
118 119

/// milliseconds_since_epoch returns the milliseconds that elapsed since the UNIX epoch.
120
uint64_t milliseconds_since_epoch(const DateTime &dt);
121
/// microseconds_since_epoch returns the microseconds that elapsed since the UNIX epoch.
122
uint64_t microseconds_since_epoch(const DateTime &dt);
123
/// from_seconds_since_epoch returns a DateTime.
124
DateTime from_seconds_since_epoch(const Seconds &s);
125
/// from_milliseconds_since_epoch returns a DateTime.
126
DateTime from_milliseconds_since_epoch(const Milliseconds &ms);
127
/// from_microseconds_since_epoch returns a DateTime.
128
DateTime from_microseconds_since_epoch(const Microseconds &us);
129 130

// moves the datetime forward to the specified hour
131
DateTime move_to_hour(const DateTime &dt, uint64_t hour);
132 133 134 135

namespace iso8601 {

/// parse parses a DateTime instance from the string s in iso8601 format.
136
DateTime parse(const std::string &s);
137
/// generate returns a string in iso8601 corresponding to 'dt'.
138
std::string generate(const DateTime &dt);
139 140 141 142 143 144

}  // namespace iso8601

}  // namespace airmap

#endif  // AIRMAP_DATE_TIME_H_