Skip to content

Commit

Permalink
Add timestamp test, fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
simonkimi committed Aug 30, 2024
1 parent 9e6367e commit b654f25
Show file tree
Hide file tree
Showing 18 changed files with 229 additions and 56 deletions.
10 changes: 10 additions & 0 deletions include/mdf/iheader.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <vector>
#include "mdf/iblock.h"
#include "itimestamp.h"
#include "imdftimestamp.h"

namespace mdf {

Expand Down Expand Up @@ -168,6 +169,15 @@ class IHeader : public IBlock {
*/
[[nodiscard]] virtual uint64_t StartTime() const = 0;

/** \brief Returns the start timestamp of the measurement.
*
* This function returns the start timestamp of the measurement as a pointer
* to an IMdfTimeStamp object.
* @return Pointer to an IMdfTimeStamp object representing the start
* timestamp.
*/
[[nodiscard]] virtual const mdf::IMdfTimeStamp* StartTimestamp() const = 0;

/** \brief Returns meta data information object.
*
* Returns meta data as a text string typically as an XML snippets.
Expand Down
38 changes: 38 additions & 0 deletions include/mdf/imdftimestamp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#pragma once

#include "mdf/itimestamp.h"
#include "mdfblock.h"

Check failure on line 4 in include/mdf/imdftimestamp.h

View workflow job for this annotation

GitHub Actions / run on macos-latest(x64)

'mdfblock.h' file not found

Check failure on line 4 in include/mdf/imdftimestamp.h

View workflow job for this annotation

GitHub Actions / run on macos-latest(x64)

'mdfblock.h' file not found

namespace mdf {
/**
* \brief Interface for MDF timestamp handling.
*/
class IMdfTimeStamp {
public:
/**
* \brief Set the time in nanoseconds.
* \param time The time in nanoseconds.
*/
virtual void SetTime(uint64_t time) = 0;
/**
* \brief Set the time using an ITimestamp object.
* \param timestamp An ITimestamp object representing the time.
*/
virtual void SetTime(ITimestamp &timestamp) = 0;
/**
* \brief Get the time in nanoseconds.
* \return The time in nanoseconds.
*/
[[nodiscard]] virtual uint64_t GetTimeNs() const = 0;
/**
* \brief Get the timezone offset in minutes.
* \return The timezone offset in minutes.
*/
[[nodiscard]] virtual uint16_t GetTzOffsetMin() const = 0;
/**
* \brief Get the daylight saving time offset in minutes.
* \return The daylight saving time offset in minutes.
*/
[[nodiscard]] virtual uint16_t GetDstOffsetMin() const = 0;
};
} // namespace mdf::detail
3 changes: 3 additions & 0 deletions include/mdf/mdfwriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ class MdfWriter {
virtual void StartMeasurement(ITimestamp &start_time);
/** \brief Stops the measurement. */
virtual void StopMeasurement(uint64_t stop_time);
/** \brief Stops the measurement. */
virtual void StopMeasurement(ITimestamp &start_time);

/** \brief Stop the sample queue and write all unwritten blocks to
* the file.*/
virtual bool FinalizeMeasurement();
Expand Down
2 changes: 1 addition & 1 deletion mdflib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ add_library(mdf
src/mdfconverter.cpp
src/mdfconverter.h
src/timestamp.cpp
src/imdftimestamp.h
../include/mdf/imdftimestamp.h
src/timestamp.cpp
src/mdf3timestamp.cpp
src/mdf3timestamp.h
Expand Down
2 changes: 1 addition & 1 deletion mdflib/src/fh4block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void Fh4Block::Time(uint64_t ns_since_1970) {
timestamp_.SetTime(ns_since_1970);
}

uint64_t Fh4Block::Time() const { return timestamp_.GetTime(); }
uint64_t Fh4Block::Time() const { return timestamp_.GetTimeNs(); }

void Fh4Block::Time(ITimestamp &timestamp) { timestamp_.SetTime(timestamp); }

Expand Down
12 changes: 8 additions & 4 deletions mdflib/src/hd3block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void Hd3Block::GetBlockProperty(BlockPropertyList &dest) const {
dest.emplace_back("Project", project_);
dest.emplace_back("Organization", organisation_);
dest.emplace_back("Time Stamp [ns]", std::to_string(timestamp_.local_timestamp_));
dest.emplace_back("UTC Time Offset [h]", std::to_string(timestamp_.dst_offset_));
dest.emplace_back("UTC Time Offset [h]", std::to_string(timestamp_.utc_offset_));
switch (timestamp_.time_quality_) {
case 0:
dest.emplace_back("Time Source", "PC Time");
Expand Down Expand Up @@ -123,7 +123,7 @@ size_t Hd3Block::Read(std::FILE *file) {
bytes += ReadNumber(file, timestamp_.local_timestamp_);
break;
case 11:
bytes += ReadNumber(file, timestamp_.dst_offset_);
bytes += ReadNumber(file, timestamp_.utc_offset_);
break;
case 12:
bytes += ReadNumber(file, timestamp_.time_quality_);
Expand Down Expand Up @@ -168,7 +168,7 @@ size_t Hd3Block::Write(std::FILE *file) {
bytes += WriteStr(file, project_, 32);
bytes += WriteStr(file, subject_, 32);
bytes += WriteNumber(file, timestamp_.local_timestamp_);
bytes += WriteNumber(file, timestamp_.dst_offset_);
bytes += WriteNumber(file, timestamp_.utc_offset_);
bytes += WriteNumber(file, timestamp_.time_quality_);
bytes += WriteStr(file, timestamp_.timer_id_, 32);

Expand Down Expand Up @@ -271,7 +271,7 @@ void Hd3Block::StartTime(uint64_t ns_since_1970) {
}

uint64_t Hd3Block::StartTime() const {
return timestamp_.utc_timestamp_;
return timestamp_.local_timestamp_;
}

void Hd3Block::StartTime(ITimestamp &timestamp) {
Expand Down Expand Up @@ -301,4 +301,8 @@ void Hd3Block::AddDg3(std::unique_ptr<Dg3Block> &dg3) {
dg_list_.push_back(std::move(dg3));
}

const mdf::IMdfTimeStamp *Hd3Block::StartTimestamp() const {
return &timestamp_;
}

} // end namespace mdf::detail
1 change: 1 addition & 0 deletions mdflib/src/hd3block.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class Hd3Block : public MdfBlock, public IHeader {
void StartTime(ITimestamp& timestamp) override;

[[nodiscard]] uint64_t StartTime() const override;
[[nodiscard]] const mdf::IMdfTimeStamp* StartTimestamp() const override;

[[nodiscard]] IDataGroup *CreateDataGroup() override;
[[nodiscard]] std::vector<IDataGroup *> DataGroups() const override;
Expand Down
6 changes: 5 additions & 1 deletion mdflib/src/hd4block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ void Hd4Block::StartTime(ITimestamp& timestamp) {
timestamp_.SetTime(timestamp);
}

uint64_t Hd4Block::StartTime() const { return timestamp_.GetTime(); }
uint64_t Hd4Block::StartTime() const { return timestamp_.GetTimeNs(); }

IMetaData* Hd4Block::CreateMetaData() {
return MdfBlock::CreateMetaData();
Expand Down Expand Up @@ -516,4 +516,8 @@ bool Hd4Block::UpdateCgAndVlsdBlocks(std::FILE* file, bool update_cg,
update_vlsd);
}

const mdf::IMdfTimeStamp* Hd4Block::StartTimestamp() const {
return &timestamp_;
}

} // namespace mdf::detail
1 change: 1 addition & 0 deletions mdflib/src/hd4block.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class Hd4Block : public MdfBlock, public IHeader {
void StartTime(ITimestamp &timestamp) override;

[[nodiscard]] uint64_t StartTime() const override;
[[nodiscard]] const mdf::IMdfTimeStamp* StartTimestamp() const override;

[[nodiscard]] IMetaData *CreateMetaData() override;
[[nodiscard]] IMetaData *MetaData() const override;
Expand Down
15 changes: 0 additions & 15 deletions mdflib/src/imdftimestamp.h

This file was deleted.

34 changes: 19 additions & 15 deletions mdflib/src/mdf3timestamp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,27 @@

#include "mdf/mdfhelper.h"

void mdf::detail::Mdf3Timestamp::GetBlockProperty(
mdf::detail::BlockPropertyList& dest) const {
namespace mdf::detail {

void Mdf3Timestamp::GetBlockProperty(detail::BlockPropertyList& dest) const {
MdfBlock::GetBlockProperty(dest);
}

size_t mdf::detail::Mdf3Timestamp::Read(std::FILE* file) { return 0; }
size_t Mdf3Timestamp::Read(std::FILE* file) { return 0; }

size_t mdf::detail::Mdf3Timestamp::Write(std::FILE* file) {
return MdfBlock::Write(file);
}
size_t Mdf3Timestamp::Write(std::FILE* file) { return MdfBlock::Write(file); }

void mdf::detail::Mdf3Timestamp::SetTime(uint64_t time) {
void Mdf3Timestamp::SetTime(uint64_t time) {
date_ = MdfHelper::NanoSecToDDMMYYYY(time);
time_ = MdfHelper::NanoSecToHHMMSS(time);
local_timestamp_ = time + MdfHelper::GmtOffsetNs();
utc_timestamp_ = time;
dst_offset_ = static_cast<int16_t>(MdfHelper::DstOffsetNs() /
utc_offset_ = static_cast<int16_t>(MdfHelper::GmtOffsetNs() /
timeunits::kNanosecondsPerHour);
time_quality_ = 0;
timer_id_ = "Local PC Reference Time";
}

void mdf::detail::Mdf3Timestamp::SetTime(mdf::ITimestamp& timestamp) {
void Mdf3Timestamp::SetTime(mdf::ITimestamp& timestamp) {
if (dynamic_cast<UtcTimeStamp*>(&timestamp)) {
SetTime(timestamp.GetTimeNs());
return;
Expand All @@ -34,8 +32,8 @@ void mdf::detail::Mdf3Timestamp::SetTime(mdf::ITimestamp& timestamp) {
date_ = MdfHelper::NanoSecUtcToDDMMYYYY(local_time->GetTimeNs());
time_ = MdfHelper::NanoSecUtcToHHMMSS(local_time->GetTimeNs());
local_timestamp_ = timestamp.GetTimeNs() - MdfHelper::DstOffsetNs();
utc_timestamp_ = local_time->GetUtcTimeNs();
dst_offset_ = static_cast<int16_t>(local_time->GetDstMin() / 60);
utc_offset_ = static_cast<int16_t>(MdfHelper::GmtOffsetNs() /
timeunits::kNanosecondsPerHour);
time_quality_ = 0;
timer_id_ = "Local PC Reference Time";
return;
Expand All @@ -48,12 +46,18 @@ void mdf::detail::Mdf3Timestamp::SetTime(mdf::ITimestamp& timestamp) {
tz->GetTimezoneMin(), tz->GetDstMin());
local_timestamp_ = timestamp.GetTimeNs() +
tz->GetTimezoneMin() * timeunits::kNanosecondsPerMinute;
utc_timestamp_ = tz->GetTimeNs();
dst_offset_ = static_cast<int16_t>(timestamp.GetDstMin() / 60);
utc_offset_ = static_cast<int16_t>(timestamp.GetTimezoneMin() / 60);
time_quality_ = 0;
timer_id_ = "Local PC Reference Time";
return;
}
}

uint64_t mdf::detail::Mdf3Timestamp::GetTime() const { return 0; }
uint64_t Mdf3Timestamp::GetTimeNs() const { return local_timestamp_; }
uint16_t Mdf3Timestamp::GetTzOffsetMin() const { return utc_offset_ * 60; }
uint16_t Mdf3Timestamp::GetDstOffsetMin() const {
// mdf3 timestamp does not have DST offset
return 0;
}

} // namespace mdf::detail
13 changes: 7 additions & 6 deletions mdflib/src/mdf3timestamp.h
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
#pragma once

#include "imdftimestamp.h"
#include "mdf/imdftimestamp.h"
namespace mdf::detail {

class Mdf3Timestamp : public IMdfTimeStamp {
class Mdf3Timestamp : public IMdfTimeStamp, public MdfBlock {
public:
void GetBlockProperty(BlockPropertyList &dest) const override;
void GetBlockProperty(detail::BlockPropertyList &dest) const override;
size_t Read(std::FILE *file) override;
size_t Write(std::FILE *file) override;

void SetTime(uint64_t time) override;
void SetTime(ITimestamp &timestamp) override;
[[nodiscard]] uint64_t GetTime() const override;
[[nodiscard]] uint64_t GetTimeNs() const override;
[[nodiscard]] uint16_t GetTzOffsetMin() const override;
[[nodiscard]] uint16_t GetDstOffsetMin() const override;

std::string date_ = "01:01:1970";
std::string time_ = "00:00:00";
uint64_t local_timestamp_ =
0; ///< Nanosecond since 1 Jan 1970 with DST (local time)
uint64_t utc_timestamp_ = 0 ; ///< Nanosecond since 1 Jan 1970 without DST
int16_t dst_offset_ = 0; ///< DST offset in hours
int16_t utc_offset_ = 0; ///< UTC offset in hours
uint16_t time_quality_ = 0; ///< Default local PC time
std::string timer_id_ = "Local PC Reference Time";
};
Expand Down
12 changes: 7 additions & 5 deletions mdflib/src/mdf4timestamp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace mdf::detail {

void Mdf4Timestamp::GetBlockProperty(BlockPropertyList &dest) const {
void Mdf4Timestamp::GetBlockProperty(detail::BlockPropertyList &dest) const {
dest.emplace_back("ISO Time", GetTimeString());
dest.emplace_back("TZ Offset [min]", std::to_string(tz_offset_));
dest.emplace_back("DST Offset [min]", std::to_string(dst_offset_));
Expand All @@ -21,7 +21,7 @@ void Mdf4Timestamp::GetBlockProperty(BlockPropertyList &dest) const {
}

size_t Mdf4Timestamp::Read(std::FILE *file) {
file_position_ = GetFilePosition(file);
file_position_ = detail::GetFilePosition(file);
size_t bytes = ReadNumber(file, time_);
bytes += ReadNumber(file, tz_offset_);
bytes += ReadNumber(file, dst_offset_);
Expand All @@ -31,9 +31,9 @@ size_t Mdf4Timestamp::Read(std::FILE *file) {

size_t Mdf4Timestamp::Write(std::FILE *file) {
if (file_position_ <= 0) {
file_position_ = GetFilePosition(file);
file_position_ = detail::GetFilePosition(file);
} else {
SetFilePosition(file, file_position_);
detail::SetFilePosition(file, file_position_);
}
auto bytes = WriteNumber(file, time_);
bytes += WriteNumber(file, tz_offset_);
Expand Down Expand Up @@ -104,6 +104,8 @@ std::string Mdf4Timestamp::GetTimeString() const {

return date_time.str();
}
uint64_t Mdf4Timestamp::GetTime() const { return time_; }
uint64_t Mdf4Timestamp::GetTimeNs() const { return time_; }
uint16_t Mdf4Timestamp::GetTzOffsetMin() const { return tz_offset_; }
uint16_t Mdf4Timestamp::GetDstOffsetMin() const { return dst_offset_; }

} // namespace mdf::detail
12 changes: 7 additions & 5 deletions mdflib/src/mdf4timestamp.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#pragma once
#include <cstdio>

#include "imdftimestamp.h"
#include "mdf/imdftimestamp.h"
#include "mdf/itimestamp.h"
#include "mdf/mdfhelper.h"
#include "mdfblock.h"
Expand All @@ -18,18 +18,20 @@ constexpr uint8_t kLocalTimestamp = 0x01;
constexpr uint8_t kTimeOffsetValid = 0x02;
} // namespace TimestampFlag

class Mdf4Timestamp : public IMdfTimeStamp {
class Mdf4Timestamp : public IMdfTimeStamp, public detail::MdfBlock {
public:
Mdf4Timestamp();
void GetBlockProperty(BlockPropertyList &dest) const override;
void GetBlockProperty(detail::BlockPropertyList &dest) const override;
size_t Read(std::FILE *file) override;
size_t Write(std::FILE *file) override;

void SetTime(uint64_t time) override;
void SetTime(ITimestamp &timestamp) override;

[[nodiscard]] std::string GetTimeString() const;
[[nodiscard]] uint64_t GetTime() const override;
[[nodiscard]] uint64_t GetTimeNs() const override;
[[nodiscard]] uint16_t GetTzOffsetMin() const override;
[[nodiscard]] uint16_t GetDstOffsetMin() const override;

private:
uint64_t time_;
Expand Down
4 changes: 4 additions & 0 deletions mdflib/src/mdfwriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,10 @@ void MdfWriter::StopMeasurement(uint64_t stop_time) {
sample_event_.notify_one();
}

void MdfWriter::StopMeasurement(ITimestamp& start_time) {
StopMeasurement(start_time.GetUtcTimeNs());
}

bool MdfWriter::FinalizeMeasurement() {
StopWorkThread();

Expand Down
4 changes: 1 addition & 3 deletions mdflib_test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ add_executable(test_mdf
src/testchannelarray.cpp
src/testchannelarray.h
src/testxtensor.cpp
src/testqueue.cpp
src/testtimestamp.cpp
src/testtimestamp.h)
src/testqueue.cpp)

target_include_directories(test_mdf PRIVATE ../include ../mdflib/src)
if(VCPKG)
Expand Down
Loading

0 comments on commit b654f25

Please sign in to comment.