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

Commit

Permalink
Properly calculate UTC/Local timestamps in SER files. Fixes #15
Browse files Browse the repository at this point in the history
  • Loading branch information
GuLinux committed Jun 27, 2017
1 parent 107b950 commit befbb24
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
14 changes: 9 additions & 5 deletions src/commons/ser_header.cpp
Expand Up @@ -17,7 +17,7 @@
*/
#include "ser_header.h"
#include <map>

#include <QDebug>
using namespace std;

namespace {
Expand All @@ -38,6 +38,7 @@ map<Frame::ColorFormat, SER_Header::ColorId> color_format_conversion(){
};
return _map;
};
static const QDateTime reference_datetime{{1,1,1},{0,0,0}, Qt::UTC};
}

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

SER_Timestamp SER_Header::timestamp(const QDateTime& datetime)
{
static const QDateTime reference{{1, 1, 1}, {0,0,0}};
return qToLittleEndian(reference.msecsTo(datetime) * 10000);
auto diff_msecs = reference_datetime.msecsTo(datetime);
if(datetime.timeSpec() == Qt::LocalTime) {
diff_msecs += datetime.offsetFromUtc() * 1000l;
}
return qToLittleEndian(diff_msecs * 10000);
}

QDateTime SER_Header::qdatetime(const SER_Timestamp& timestamp)
{
static const QDateTime reference{{1, 1, 1}, {0,0,0}};
return reference.addMSecs( qFromLittleEndian(timestamp) / 10000);
// TODO: support also local timestamp?
return reference_datetime.addMSecs(qFromLittleEndian(timestamp) / 10000);
}

1 change: 0 additions & 1 deletion src/image_handlers/output_writers/serwriter.cpp
Expand Up @@ -51,7 +51,6 @@ SERWriter::SERWriter ( const QString& deviceName, const Configuration &configura
SER_Header empty_header;
empty_header.datetime = SER_Header::timestamp(QDateTime::currentDateTime());
empty_header.datetime_utc = SER_Header::timestamp(QDateTime::currentDateTimeUtc());
qDebug() << "Starting datetime: " << QDateTime::currentDateTimeUtc() << ", : " << SER_Header::qdatetime(empty_header.datetime_utc);
::strcpy(empty_header.camera, deviceName.left(40).toLatin1());
::strcpy(empty_header.observer, configuration.observer().left(40).toLatin1());
::strcpy(empty_header.telescope, configuration.telescope().left(40).toLatin1());
Expand Down
9 changes: 8 additions & 1 deletion tests/CMakeLists.txt
Expand Up @@ -2,7 +2,14 @@ include_directories(googletest/include)
add_executable(test_qimage_destructor test_qimage_destructor.cpp)
target_link_libraries(test_qimage_destructor gtest_main Qt5::Widgets)
add_test(qimage_destructor test_qimage_destructor)
add_subdirectory(googletest)

add_executable(test_roi_validator test_roi_validator.cpp)
target_link_libraries(test_roi_validator gtest_main drivers Qt5::Widgets)
add_test(roi_validator_test test_roi_validator)

add_executable(test_ser_header test_ser_header.cpp ${CMAKE_SOURCE_DIR}/src/commons/ser_header.cpp)
target_link_libraries(test_ser_header gtest_main ${OpenCV_LIBS} Qt5::Widgets)
add_test(ser_header_test test_ser_header)


add_subdirectory(googletest)
20 changes: 20 additions & 0 deletions tests/test_ser_header.cpp
@@ -0,0 +1,20 @@
#include "gtest/gtest.h"
#include "commons/ser_header.h"
#include <QDateTime>
#include <QDebug>
using namespace std;


TEST(SerHeader, test_qdatetime_utc_to_ser) {
QDateTime now{{2017, 06, 20}, {10, 10, 00}, Qt::UTC};
auto ser_datetime = SER_Header::timestamp(now);
ASSERT_EQ(636335502000000000, ser_datetime);
}


TEST(SerHeader, test_qdatetime_local_to_ser) {
QDateTime now{{2017, 06, 20}, {10, 10, 00}, Qt::LocalTime, 3600};
auto ser_datetime = SER_Header::timestamp(now);
ASSERT_EQ(636335502000000000, ser_datetime);
}

0 comments on commit befbb24

Please sign in to comment.