DUDS
Distributed Update of Data from Something
Hectoform.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 
25 struct Hectoform {
31  unsigned int e10 : 25;
37  unsigned int e8 : 7;
43  unsigned int M : 7;
50  unsigned int ma : 7;
56  unsigned int h : 7;
60  unsigned int s : 7;
66  unsigned int neg : 1;
70  Hectoform() = default;
77  template <class Rep, class Period>
78  Hectoform(const std::chrono::duration<Rep, Period> &d) {
79  setDuration(d);
80  }
87  template <class Clock, class Duration>
88  Hectoform(const std::chrono::time_point<Clock, Duration> &t) {
89  setTime(t);
90  }
96  template <class Int>
97  void setToSeconds(Int t) {
98  Int sec;
99  if (t < 0) {
100  sec = -t; // std::abs() is not defined for __int128
101  neg = 1;
102  } else {
103  sec = t;
104  neg = 0;
105  }
106  // typecasting is required to work reliably with all valid types for
107  // Int, especially boost::multiprecision::int128_t
108  s = (unsigned int)(sec % 100);
109  sec /= 100;
110  h = (unsigned int)(sec % 100);
111  sec /= 100;
112  ma = (unsigned int)(sec % 100);
113  sec /= 100;
114  M = (unsigned int)(sec % 100);
115  sec /= 100;
116  e8 = (unsigned int)(sec % 100);
117  e10 = (unsigned int)(sec / 100);
118  }
125  template <class Duration>
126  void setDuration(const Duration &d) {
127  std::chrono::duration<typename Duration::rep> sec =
128  std::chrono::duration_cast<std::chrono::seconds>(d);
129  setToSeconds(sec.count());
130  }
131  template <class Time>
132  void setTime(const Time &d) {
133  std::chrono::duration<typename Time::rep> sec =
134  std::chrono::duration_cast<std::chrono::seconds>(d.time_since_epoch());
135  setToSeconds(sec.count());
136  }
137 };
138 
143 std::ostream &operator << (std::ostream &os, const Hectoform &h);
144 
145 } } }
Hectoform(const std::chrono::duration< Rep, Period > &d)
Constructs a Hectoform with the duration contained in d truncated to seconds.
Definition: Hectoform.hpp:78
Hectoform(const std::chrono::time_point< Clock, Duration > &t)
Constructs a Hectoform with the time contained in t truncated to seconds.
Definition: Hectoform.hpp:88
unsigned int e8
1e8 seconds field.
Definition: Hectoform.hpp:37
unsigned int ma
Myriaseconds field.
Definition: Hectoform.hpp:50
Hectoform()=default
Default constructor.
void setToSeconds(Int t)
Sets the stored time to be the same as the time given.
Definition: Hectoform.hpp:97
Holds Interstellar Time down to seconds in fields that increase by a power of 100, two decimal digits each, up to a 1e10 seconds field.
Definition: Hectoform.hpp:25
std::ostream & operator<<(std::ostream &os, const Hectoform &h)
Writes the Hectoform time in its Human readable format in plain text.
unsigned int s
Seconds field.
Definition: Hectoform.hpp:60
unsigned int e10
1e10 seconds field.
Definition: Hectoform.hpp:31
unsigned int neg
The negative flag.
Definition: Hectoform.hpp:66
unsigned int h
Hectoseconds field.
Definition: Hectoform.hpp:56
unsigned int M
Megaseconds field.
Definition: Hectoform.hpp:43
void setDuration(const Duration &d)
Sets the stored time to be the same as the time given truncated to seconds.
Definition: Hectoform.hpp:126