Coverage Report

Created: 2021-08-28 18:14

D:\git\skunkworks\herald-for-cpp\herald\src\datatype\date.cpp
Line
Count
Source (jump to first uncovered line)
1
//  Copyright 2020-2021 Herald Project Contributors
2
//  SPDX-License-Identifier: Apache-2.0
3
//
4
5
#include "herald/datatype/date.h"
6
#include "herald/datatype/time_interval.h"
7
8
#ifdef __ZEPHYR__
9
#include <kernel.h>
10
#else
11
#include <chrono>
12
#endif
13
14
// #include <ctime>
15
16
namespace herald {
17
namespace datatype {
18
19
// DATE DECLARATIONS
20
21
Date::Date()
22
  :
23
#ifdef __ZEPHYR__
24
  seconds(0.001 * k_uptime_get())
25
#else
26
  seconds(std::chrono::duration_cast<std::chrono::seconds>(
27
    std::chrono::system_clock::now().time_since_epoch()).count())
28
#endif
29
1.06k
{
30
1.06k
}
31
32
Date::Date(std::uint64_t secondsSinceEpochOrRestart)
33
 : seconds(secondsSinceEpochOrRestart)
34
1.31k
{
35
1.31k
  ;
36
1.31k
}
37
38
Date::Date(const Date& from)
39
 : seconds(from.seconds)
40
738
{
41
738
  ;
42
738
}
43
44
Date::Date(Date&& from)
45
 : seconds(from.seconds)
46
702
{
47
702
  ;
48
702
}
49
50
3.81k
Date::~Date() = default;
51
52
void
53
Date::setToNow() noexcept
54
58
{
55
  #ifdef __ZEPHYR__
56
    seconds = 0.001 * k_uptime_get();
57
  #else
58
    seconds = std::chrono::duration_cast<std::chrono::seconds>(
59
58
      std::chrono::system_clock::now().time_since_epoch()).count();
60
58
  #endif
61
58
}
62
63
Date&
64
Date::operator=(const Date& other) noexcept
65
15
{
66
15
  seconds = other.seconds;
67
15
  return *this;
68
15
}
69
70
Date&
71
Date::operator=(Date&& other) noexcept
72
908
{
73
908
  seconds = other.seconds;
74
908
  return *this;
75
908
}
76
77
Date
78
Date::operator-(const TimeInterval& other) noexcept
79
10
{
80
10
  return Date(seconds - other.seconds());
81
10
}
82
83
Date&
84
Date::operator-=(const TimeInterval& other) noexcept
85
0
{
86
0
  seconds -= other.seconds();
87
0
  return *this;
88
0
}
89
90
Date
91
Date::operator+(const TimeInterval& other) noexcept
92
19
{
93
19
  return Date(seconds + other.seconds());
94
19
}
95
96
Date&
97
Date::operator+=(const TimeInterval& other) noexcept
98
0
{
99
0
  seconds += other.seconds();
100
0
  return *this;
101
0
}
102
103
std::string
104
4
Date::iso8601DateTime() const noexcept {
105
4
  // time_t t(seconds);
106
4
  // char buf[21];
107
4
  // strftime(buf, sizeof(buf), "%FT%TZ", gmtime(&t));
108
4
  // return std::string(buf);
109
4
  return std::to_string(seconds);
110
4
}
111
112
2
Date::operator std::string() const noexcept {
113
2
  return iso8601DateTime();
114
2
}
115
116
std::uint64_t
117
1.08k
Date::secondsSinceUnixEpoch() const noexcept {
118
1.08k
  return seconds;
119
1.08k
}
120
121
bool
122
Date::operator==(const Date& other) const noexcept
123
2
{
124
2
  return seconds == other.seconds;
125
2
}
126
127
bool
128
Date::operator!=(const Date& other) const noexcept
129
0
{
130
0
  return seconds != other.seconds;
131
0
}
132
133
bool
134
Date::operator<(const Date& other) const noexcept
135
12
{
136
12
  return seconds < other.seconds;
137
12
}
138
139
bool
140
Date::operator>(const Date& other) const noexcept
141
164
{
142
164
  return seconds > other.seconds;
143
164
}
144
145
bool
146
Date::operator<=(const Date& other) const noexcept
147
0
{
148
0
  return seconds <= other.seconds;
149
0
}
150
151
bool
152
Date::operator>=(const Date& other) const noexcept
153
17
{
154
17
  return seconds >= other.seconds;
155
17
}
156
157
158
Date::operator long() const noexcept
159
37
{
160
37
  return seconds;
161
37
}
162
163
164
} // end namespace
165
} // end namespace