Skip to content
This repository was archived by the owner on Oct 10, 2022. It is now read-only.

Commit a0dc3ff

Browse files
committed
Properly calculate UTC/Local timestamps in SER files. Fixes #15
1 parent 1a5587e commit a0dc3ff

4 files changed

Lines changed: 37 additions & 7 deletions

File tree

src/commons/ser_header.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*/
1818
#include "ser_header.h"
1919
#include <map>
20-
20+
#include <QDebug>
2121
using namespace std;
2222

2323
namespace {
@@ -38,6 +38,7 @@ map<Frame::ColorFormat, SER_Header::ColorId> color_format_conversion(){
3838
};
3939
return _map;
4040
};
41+
static const QDateTime reference_datetime{{1,1,1},{0,0,0}, Qt::UTC};
4142
}
4243

4344
size_t SER_Header::frame_size() const
@@ -70,13 +71,16 @@ void SER_Header::set_color_format(const Frame::ColorFormat& format)
7071

7172
SER_Timestamp SER_Header::timestamp(const QDateTime& datetime)
7273
{
73-
static const QDateTime reference{{1, 1, 1}, {0,0,0}};
74-
return qToLittleEndian(reference.msecsTo(datetime) * 10000);
74+
auto diff_msecs = reference_datetime.msecsTo(datetime);
75+
if(datetime.timeSpec() == Qt::LocalTime) {
76+
diff_msecs += datetime.offsetFromUtc() * 1000l;
77+
}
78+
return qToLittleEndian(diff_msecs * 10000);
7579
}
7680

7781
QDateTime SER_Header::qdatetime(const SER_Timestamp& timestamp)
7882
{
79-
static const QDateTime reference{{1, 1, 1}, {0,0,0}};
80-
return reference.addMSecs( qFromLittleEndian(timestamp) / 10000);
83+
// TODO: support also local timestamp?
84+
return reference_datetime.addMSecs(qFromLittleEndian(timestamp) / 10000);
8185
}
8286

src/image_handlers/output_writers/serwriter.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ SERWriter::SERWriter ( const QString& deviceName, const Configuration::ptr & con
5151
SER_Header empty_header;
5252
empty_header.datetime = SER_Header::timestamp(QDateTime::currentDateTime());
5353
empty_header.datetime_utc = SER_Header::timestamp(QDateTime::currentDateTimeUtc());
54-
qDebug() << "Starting datetime: " << QDateTime::currentDateTimeUtc() << ", : " << SER_Header::qdatetime(empty_header.datetime_utc);
5554
::strcpy(empty_header.camera, deviceName.left(40).toLatin1());
5655
::strcpy(empty_header.observer, configuration->observer().left(40).toLatin1());
5756
::strcpy(empty_header.telescope, configuration->telescope().left(40).toLatin1());

tests/CMakeLists.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@ include_directories(googletest/include)
22
add_executable(test_qimage_destructor test_qimage_destructor.cpp)
33
target_link_libraries(test_qimage_destructor gtest_main Qt5::Widgets)
44
add_test(qimage_destructor test_qimage_destructor)
5-
add_subdirectory(googletest)
5+
66
add_executable(test_roi_validator test_roi_validator.cpp)
77
target_link_libraries(test_roi_validator gtest_main drivers Qt5::Widgets)
88
add_test(roi_validator_test test_roi_validator)
9+
10+
add_executable(test_ser_header test_ser_header.cpp ${CMAKE_SOURCE_DIR}/src/commons/ser_header.cpp)
11+
target_link_libraries(test_ser_header gtest_main ${OpenCV_LIBS} Qt5::Widgets)
12+
add_test(ser_header_test test_ser_header)
13+
14+
15+
add_subdirectory(googletest)

tests/test_ser_header.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include "gtest/gtest.h"
2+
#include "commons/ser_header.h"
3+
#include <QDateTime>
4+
#include <QDebug>
5+
using namespace std;
6+
7+
8+
TEST(SerHeader, test_qdatetime_utc_to_ser) {
9+
QDateTime now{{2017, 06, 20}, {10, 10, 00}, Qt::UTC};
10+
auto ser_datetime = SER_Header::timestamp(now);
11+
ASSERT_EQ(636335502000000000, ser_datetime);
12+
}
13+
14+
15+
TEST(SerHeader, test_qdatetime_local_to_ser) {
16+
QDateTime now{{2017, 06, 20}, {10, 10, 00}, Qt::LocalTime, 3600};
17+
auto ser_datetime = SER_Header::timestamp(now);
18+
ASSERT_EQ(636335502000000000, ser_datetime);
19+
}
20+

0 commit comments

Comments
 (0)