SOIL C++
C++ Unified Device Interface
Time.cpp
Go to the documentation of this file.
1#include "Time.h"
2#include <boost/algorithm/string/replace.hpp>
3
4
5
6std::string SOIL::Time::rfc3339(void) const
7{
8 if (is_null())
9 {
10 return "";
11 }
12 else
13 {
14 std::string timestring = boost::posix_time::to_iso_extended_string(timestamp);
15 boost::replace_all(timestring, ",", ".");
16 if (timestring == "not-a-date-time")
17 {
18 return "";
19 }
20 else
21 {
22 return timestring + "Z";
23 }
24 }
25}
26
28{
29}
30
31SOIL::Time::Time(std::string value)
32{
33 if (value == "")
34 {
35 _null = true;
36 }
37 else
38 {
39 boost::replace_all(value, "T", " ");
40 boost::replace_all(value, "Z", "");
41 timestamp = boost::posix_time::time_from_string(value);
42 _null = false;
43 }
44}
45
46SOIL::Time::Time(boost::posix_time::ptime value)
47{
48 _null = false;
49 timestamp = value;
50}
51
52
54{
55}
56
57boost::posix_time::ptime SOIL::Time::utc_now(void)
58{
59 return boost::posix_time::microsec_clock::universal_time();
60}
61
62std::vector<unsigned char> SOIL::Time::serialize(void) const
63{
64 if (is_null())
65 {
66 throw std::logic_error("Time is not set");
67 }
68 std::vector<unsigned char> serialization;
69 size_t size = sizeof(uint16_t) + 5 * sizeof(uint8_t) + sizeof(uint32_t);
70 serialization.reserve(size);
71
72 auto date = timestamp.date();
73 auto time = timestamp.time_of_day();
74 uint16_t year = static_cast<uint16_t>(date.year());
75 uint8_t month = static_cast<uint8_t>(date.month().as_number());
76 uint8_t day = static_cast<uint8_t>(date.day().as_number());
77 uint8_t hour = static_cast<uint8_t>(time.hours());
78 uint8_t minute = static_cast<uint8_t>(time.minutes());
79 uint8_t seconds = static_cast<uint8_t>(time.seconds());
80 uint32_t nanoseconds = static_cast<uint32_t>(time.fractional_seconds());
81
82 switch (time.num_fractional_digits())
83 {
84 case 3:
85 nanoseconds *= 1000000;
86 break;
87 case 6:
88 nanoseconds *= 1000;
89 break;
90 case 9:
91 break;
92 default:
93 throw std::logic_error("Unknown fractional seconds!");
94 }
95
96 unsigned char* pointer = reinterpret_cast<unsigned char*>(&year);
97 for (int i = 0; i < sizeof(uint16_t); i++)
98 {
99 serialization.push_back(pointer[i]);
100 };
101 serialization.push_back(month);
102 serialization.push_back(day);
103 serialization.push_back(hour);
104 serialization.push_back(minute);
105 serialization.push_back(seconds);
106
107 pointer = reinterpret_cast<unsigned char*>(&nanoseconds);
108 for (int i = 0; i < sizeof(uint32_t); i++)
109 {
110 serialization.push_back(pointer[i]);
111 };
112
113 return serialization;
114}
115
116bool SOIL::operator>=(const SOIL::Time & t1, const SOIL::Time & t2)
117{
118 if (t1.is_null() || t2.is_null())
119 {
120 return false;
121 }
122 else
123 {
124 return (t1.timestamp >= t2.timestamp);
125 }
126}
127
128bool SOIL::operator<=(const SOIL::Time & t1, const SOIL::Time & t2)
129{
130 if (t1.is_null() || t2.is_null())
131 {
132 return false;
133 }
134 else
135 {
136 return (t1.timestamp <= t2.timestamp);
137 }
138}
SOIL Time.
Definition: Time.h:13
bool is_null(void) const
Is Null?
Definition: Time.h:92
DLL ~Time()
Destructor.
Definition: Time.cpp:53
DLL Time()
Uninitialized Constructor.
Definition: Time.cpp:27
DLL std::string rfc3339(void) const
RFC3339 representation.
Definition: Time.cpp:6
DLL std::vector< unsigned char > serialize(void) const
Bytewise serialization.
Definition: Time.cpp:62
static DLL boost::posix_time::ptime utc_now(void)
Current Time.
Definition: Time.cpp:57
DLL bool operator<=(const Time &t1, const Time &t2)
LEQ Time Operator.
Definition: Time.cpp:128
DLL bool operator>=(const Time &t1, const Time &t2)
GEQ Time Operator.
Definition: Time.cpp:116