Skip to content

Commit 045cfaa

Browse files
authored
Add methods to convert from Fw::Time* to Fw::Time*Value (#4658)
1 parent d8ce9b4 commit 045cfaa

File tree

9 files changed

+61
-0
lines changed

9 files changed

+61
-0
lines changed

Fw/Time/Time.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ bool Time::operator<=(const Time& other) const {
7777
return ((TimeComparison::LT == c) or (TimeComparison::EQ == c));
7878
}
7979

80+
TimeValue Time::asTimeValue() const {
81+
return this->m_val;
82+
}
83+
8084
SerializeStatus Time::serializeTo(SerialBufferBase& buffer, Fw::Endianness mode) const {
8185
return this->m_val.serializeTo(buffer, mode);
8286
}

Fw/Time/Time.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ class Time : public Serializable {
4949
bool operator<=(const Time& other) const;
5050
Time& operator=(const Time& other);
5151

52+
//! \brief get the underlying TimeValue
53+
//! \return the TimeValue representation of this Time as a copy
54+
TimeValue asTimeValue() const;
55+
5256
// Static methods:
5357
//! The type of a comparison result
5458

Fw/Time/TimeInterval.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ bool TimeInterval::operator<=(const TimeInterval& other) const {
5353
return ((LT == c) or (EQ == c));
5454
}
5555

56+
TimeIntervalValue TimeInterval::asTimeIntervalValue() const {
57+
return this->m_val;
58+
}
59+
5660
SerializeStatus TimeInterval::serializeTo(SerialBufferBase& buffer, Fw::Endianness mode) const {
5761
// Use TimeIntervalValue's built-in serialization
5862
return this->m_val.serializeTo(buffer, mode);

Fw/Time/TimeInterval.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ class TimeInterval : public Serializable {
4343
bool operator<=(const TimeInterval& other) const;
4444
TimeInterval& operator=(const TimeInterval& other);
4545

46+
//! \brief get the underlying TimeIntervalValue
47+
//! \return the TimeIntervalValue representation of this TimeInterval as a copy
48+
TimeIntervalValue asTimeIntervalValue() const;
49+
4650
//! The type of a comparison result
4751
typedef enum { LT = -1, EQ = 0, GT = 1, INCOMPARABLE = 2 } Comparison;
4852

Fw/Time/test/ut/TimeIntervalTester.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,18 @@ void TimeIntervalTester::test_TimeIntervalSubtractionTest() {
129129
ASSERT_EQ(result4.getUSeconds(), 0);
130130
}
131131

132+
void TimeIntervalTester::test_TimeIntervalToTimeIntervalValue() {
133+
U32 seconds = 5;
134+
U32 useconds = 500000;
135+
Fw::TimeInterval time_interval(seconds, useconds);
136+
137+
Fw::TimeIntervalValue time_interval_value = time_interval.asTimeIntervalValue();
138+
139+
// Alter the original
140+
time_interval.set(0, 0);
141+
142+
ASSERT_EQ(time_interval_value.get_seconds(), seconds);
143+
ASSERT_EQ(time_interval_value.get_useconds(), useconds);
144+
}
145+
132146
} // namespace Fw

Fw/Time/test/ut/TimeIntervalTester.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class TimeIntervalTester {
2121
void test_TimeIntervalCompareStaticTest();
2222
void test_TimeIntervalAdditionTest();
2323
void test_TimeIntervalSubtractionTest();
24+
void test_TimeIntervalToTimeIntervalValue();
2425
};
2526
} // namespace Fw
2627

Fw/Time/test/ut/TimeTestMain.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ TEST(TimeTestNominal, ZeroTimeEquality) {
2929
tester.test_ZeroTimeEquality();
3030
}
3131

32+
TEST(TimeTestNominal, TimeToTimeValue) {
33+
Fw::TimeTester tester;
34+
tester.test_TimeToTimeValue();
35+
}
36+
3237
// TimeInterval tests
3338
TEST(TimeIntervalTestNominal, test_TimeIntervalInstantiateTest) {
3439
Fw::TimeIntervalTester tester;
@@ -55,6 +60,11 @@ TEST(TimeIntervalTestNominal, test_TimeIntervalSubtractionTest) {
5560
tester.test_TimeIntervalSubtractionTest();
5661
}
5762

63+
TEST(TimeIntervalTestNominal, test_TimeIntervalToTimeIntervalValue) {
64+
Fw::TimeIntervalTester tester;
65+
tester.test_TimeIntervalToTimeIntervalValue();
66+
}
67+
5868
int main(int argc, char* argv[]) {
5969
::testing::InitGoogleTest(&argc, argv);
6070
return RUN_ALL_TESTS();

Fw/Time/test/ut/TimeTester.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,23 @@ void TimeTester::test_ZeroTimeEquality() {
149149
ASSERT_EQ(time2, Fw::ZERO_TIME);
150150
}
151151

152+
void TimeTester::test_TimeToTimeValue() {
153+
U32 seconds = 5;
154+
U32 useconds = 500000;
155+
TimeBase timeBase = TimeBase::TB_WORKSTATION_TIME;
156+
FwTimeContextStoreType context = 3;
157+
158+
Fw::Time time(timeBase, context, seconds, useconds);
159+
160+
Fw::TimeValue time_value = time.asTimeValue();
161+
162+
// Alter the original
163+
time.set(TimeBase::TB_NONE, 0, 0, 0);
164+
165+
ASSERT_EQ(time_value.get_timeBase(), timeBase);
166+
ASSERT_EQ(time_value.get_timeContext(), context);
167+
ASSERT_EQ(time_value.get_seconds(), seconds);
168+
ASSERT_EQ(time_value.get_useconds(), useconds);
169+
}
170+
152171
} // namespace Fw

Fw/Time/test/ut/TimeTester.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class TimeTester {
2020
void test_MathTest();
2121
void test_CopyTest();
2222
void test_ZeroTimeEquality();
23+
void test_TimeToTimeValue();
2324
};
2425
} // namespace Fw
2526

0 commit comments

Comments
 (0)