DUDS
Distributed Update of Data from Something
Metricform.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of the DUDS project. It is subject to the BSD-style
3  * license terms in the LICENSE file found in the top-level directory of this
4  * distribution and at https://github.com/jjackowski/duds/blob/master/LICENSE.
5  * No part of DUDS, including this file, may be copied, modified, propagated,
6  * or distributed except according to the terms contained in the LICENSE file.
7  *
8  * Copyright (C) 2017 Jeff Jackowski
9  */
10 #include <iostream>
11 #include <chrono>
12 
13 namespace duds { namespace time { namespace interstellar {
14 
23  struct Metricform {
29  unsigned int G : 32;
35  unsigned int M : 10;
41  unsigned int k : 10;
45  unsigned int s : 10;
51  unsigned int neg : 1;
55  Metricform() = default;
62  template <class Rep, class Period>
63  Metricform(const std::chrono::duration<Rep, Period> &d) {
64  setDuration(d);
65  }
72  template <class Clock, class Duration>
73  Metricform(const std::chrono::time_point<Clock, Duration> &t) {
74  setTime(t);
75  }
81  template <class Int>
82  void setToSeconds(Int t) {
83  Int sec;
84  if (t < 0) {
85  sec = -t; // std::abs() is not defined for __int128
86  neg = 1;
87  } else {
88  sec = t;
89  neg = 0;
90  }
91  // typecasting is required to work reliably with all valid types for
92  // Int, especially boost::multiprecision::int128_t
93  s = (unsigned int)(sec % 1000);
94  sec /= 1000;
95  k = (unsigned int)(sec % 1000);
96  sec /= 1000;
97  M = (unsigned int)(sec % 1000);
98  G = (unsigned int)(sec / 1000);
99  }
106  template <class Duration>
107  void setDuration(const Duration &d) {
108  std::chrono::duration<typename Duration::rep> sec =
109  std::chrono::duration_cast<std::chrono::seconds>(d);
110  setToSeconds(sec.count());
111  }
112  template <class Time>
113  void setTime(const Time &d) {
114  std::chrono::duration<typename Time::rep> sec =
115  std::chrono::duration_cast<std::chrono::seconds>(d.time_since_epoch());
116  setToSeconds(sec.count());
117  }
118 };
119 
124 std::ostream &operator << (std::ostream &os, const Metricform &m);
125 
126 } } }
127 
unsigned int k
Kiloseconds field.
Definition: Metricform.hpp:41
unsigned int G
Gigaseconds field.
Definition: Metricform.hpp:29
unsigned int neg
The negative flag.
Definition: Metricform.hpp:51
Holds Interstellar Time down to seconds in fields that increase by a power of 1000, three decimal digits each, up to a gigaseconds field.
Definition: Metricform.hpp:23
Metricform(const std::chrono::duration< Rep, Period > &d)
Constructs a Metricform with the duration contained in d truncated to seconds.
Definition: Metricform.hpp:63
void setDuration(const Duration &d)
Sets the stored time to be the same as the time given truncated to seconds.
Definition: Metricform.hpp:107
std::ostream & operator<<(std::ostream &os, const Hectoform &h)
Writes the Hectoform time in its Human readable format in plain text.
Metricform(const std::chrono::time_point< Clock, Duration > &t)
Constructs a Metricform with the time contained in t truncated to seconds.
Definition: Metricform.hpp:73
unsigned int M
Megaseconds field.
Definition: Metricform.hpp:35
unsigned int s
Seconds field.
Definition: Metricform.hpp:45
Metricform()=default
Default constructor.
void setToSeconds(Int t)
Sets the stored time to be the same as the time given.
Definition: Metricform.hpp:82