Skip to content

Commit

Permalink
Add .net DateTime support
Browse files Browse the repository at this point in the history
  • Loading branch information
simonkimi committed Sep 2, 2024
1 parent b770f49 commit c37c4aa
Show file tree
Hide file tree
Showing 19 changed files with 268 additions and 64 deletions.
2 changes: 1 addition & 1 deletion include/mdf/iheader.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ class IHeader : public IBlock {
* @return Pointer to an IMdfTimestamp object representing the start
* timestamp.
*/
[[nodiscard]] virtual const mdf::IMdfTimestamp* StartTimestamp() const = 0;
[[nodiscard]] virtual const IMdfTimestamp* StartTimestamp() const = 0;

/** \brief Returns meta data information object.
*
Expand Down
20 changes: 19 additions & 1 deletion include/mdf/imdftimestamp.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@
#include "mdf/itimestamp.h"

namespace mdf {

namespace timetype
{
enum MdfTimestampType {
kUtcTime, /**< Represents the UTC time. */
kLocalTime, /**< Represents the local time. */
kLocalTimeTz, /**< Represents the local time with timezone offset. */
kTimezoneTime /**< Represents the time with timezone offset. */
};
}

class ITimestamp;

/**
* \brief Interface for MDF timestamp handling.
*/
Expand Down Expand Up @@ -33,5 +46,10 @@ class IMdfTimestamp {
* \return The daylight saving time offset in minutes.
*/
[[nodiscard]] virtual uint16_t GetDstOffsetMin() const = 0;
/**
* \brief Get the type of MDF timestamp.
* \return The MDF timestamp type.
*/
[[nodiscard]] virtual timetype::MdfTimestampType GetTimeType() const = 0;
};
} // namespace mdf::detail
} // namespace mdf
2 changes: 1 addition & 1 deletion mdflib/src/hd4block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ bool Hd4Block::UpdateCgAndVlsdBlocks(std::FILE* file, bool update_cg,
update_vlsd);
}

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

Expand Down
4 changes: 3 additions & 1 deletion mdflib/src/mdf3timestamp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ void Mdf3Timestamp::SetTime(mdf::ITimestamp& timestamp) {
utc_offset_ = static_cast<int16_t>(timestamp.GetTimezoneMin() / 60);
time_quality_ = 0;
timer_id_ = "Local PC Reference Time";
return;
}
}

Expand All @@ -60,4 +59,7 @@ uint16_t Mdf3Timestamp::GetDstOffsetMin() const {
return 0;
}

timetype::MdfTimestampType Mdf3Timestamp::GetTimeType() const {
return timetype::kLocalTimeTz;
}
} // namespace mdf::detail
1 change: 1 addition & 0 deletions mdflib/src/mdf3timestamp.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Mdf3Timestamp : public IMdfTimestamp, public MdfBlock {
[[nodiscard]] uint64_t GetTimeNs() const override;
[[nodiscard]] uint16_t GetTzOffsetMin() const override;
[[nodiscard]] uint16_t GetDstOffsetMin() const override;
[[nodiscard]] timetype::MdfTimestampType GetTimeType() const override;

std::string date_ = "01:01:1970";
std::string time_ = "00:00:00";
Expand Down
9 changes: 9 additions & 0 deletions mdflib/src/mdf4timestamp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,13 @@ uint64_t Mdf4Timestamp::GetTimeNs() const { return time_; }
uint16_t Mdf4Timestamp::GetTzOffsetMin() const { return tz_offset_; }
uint16_t Mdf4Timestamp::GetDstOffsetMin() const { return dst_offset_; }

timetype::MdfTimestampType Mdf4Timestamp::GetTimeType() const {
if (flags_ & TimestampFlag::kTimeOffsetValid) {
return timetype::kTimezoneTime;
}
if (flags_ & TimestampFlag::kLocalTimestamp) {
return timetype::kLocalTime;
}
return timetype::kUtcTime;
}
} // namespace mdf::detail
1 change: 1 addition & 0 deletions mdflib/src/mdf4timestamp.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Mdf4Timestamp : public IMdfTimestamp, public detail::MdfBlock {
[[nodiscard]] uint64_t GetTimeNs() const override;
[[nodiscard]] uint16_t GetTzOffsetMin() const override;
[[nodiscard]] uint16_t GetDstOffsetMin() const override;
[[nodiscard]] timetype::MdfTimestampType GetTimeType() const override;

private:
uint64_t time_;
Expand Down
5 changes: 2 additions & 3 deletions mdflibrary/src/MdfFileHistory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,13 @@ void MdfFileHistory::SetStartTime(IMdfTimeStamp^ timestamp) {
}
}

MdfFileTimestamp^ MdfFileHistory::GetStartTime() {
IMdfFileTimestamp^ MdfFileHistory::GetStartTime() {
if (history_ == nullptr) { return nullptr; }
const auto* time = history_->StartTimestamp();
if (time == nullptr) {
return nullptr;
}
return gcnew MdfFileTimestamp(time->GetTimeNs(), time->GetTzOffsetMin(),
time->GetDstOffsetMin());
return GetMdfFileTimestampByIMdfTimestamp(time);
}

MdfFileHistory::MdfFileHistory(mdf::IFileHistory* history)
Expand Down
21 changes: 11 additions & 10 deletions mdflibrary/src/MdfFileHistory.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@
#include "MdfFileTimestamp.h"

using namespace System;
namespace MdfLibrary {
interface class IMdfTimeStamp;
ref class MdfFileTimestamp;

namespace MdfLibrary {
public ref class MdfFileHistory {
public:
property int64_t Index { int64_t get();}
property int64_t Index { int64_t get(); }
property uint64_t Time { uint64_t get(); void set(uint64_t time); }
property MdfMetaData^ MetaData {
property MdfMetaData^ MetaData
{
MdfMetaData^ get();
}
property String^ Description { String^ get(); void set(String^ desc); }
Expand All @@ -28,12 +27,14 @@ public ref class MdfFileHistory {
property String^ UserName { String^ get(); void set(String^ user); }

void SetStartTime(IMdfTimeStamp^ timestamp);
MdfFileTimestamp^ GetStartTime();
IMdfFileTimestamp^ GetStartTime();

private:
MdfFileHistory() {}
MdfFileHistory() {
}

internal:
mdf::IFileHistory *history_ = nullptr;
MdfFileHistory(mdf::IFileHistory *history);
mdf::IFileHistory* history_ = nullptr;
MdfFileHistory(mdf::IFileHistory* history);
};
}
}
104 changes: 98 additions & 6 deletions mdflibrary/src/MdfFileTimestamp.cpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,116 @@
#include "MdfFileTimestamp.h"

#include "MdfNetHelper.h"


namespace MdfLibrary {
MdfFileTimestamp::MdfFileTimestamp(const uint64_t time_ns,
const int16_t timezone_min,
const int16_t dst_min) {
MdfFileTimezoneTimestamp::MdfFileTimezoneTimestamp(const uint64_t time_ns,
const int16_t timezone_min,
const int16_t dst_min) {
time_ns_ = time_ns;
timezone_min_ = timezone_min;
dst_min_ = dst_min;
}

uint64_t MdfFileTimestamp::TimeNs::get() {
uint64_t MdfFileTimezoneTimestamp::TimeNs::get() {
return time_ns_;
}

int16_t MdfFileTimestamp::TimezoneMin::get() {
int16_t MdfFileTimezoneTimestamp::TimezoneMin::get() {
return timezone_min_;
}

int16_t MdfFileTimestamp::DstMin::get() {
int16_t MdfFileTimezoneTimestamp::DstMin::get() {
return dst_min_;
}

DateTime MdfFileTimezoneTimestamp::Time::get() {
auto epoch = DateTime(1970, 1, 1, 0, 0, 0, System::DateTimeKind::Utc);
return epoch.AddTicks(time_ns_ / 100);
}

IMdfFileTimestamp^ GetMdfFileTimestampByIMdfTimestamp(
const mdf::IMdfTimestamp* timestamp) {
switch (timestamp->GetTimeType()) {
case mdf::timetype::kUtcTime:
return gcnew MdfFileUtcTimestamp(timestamp->GetTimeNs());
case mdf::timetype::kLocalTime:
return gcnew MdfFileLocalTimestamp(timestamp->GetTimeNs());
case mdf::timetype::kLocalTimeTz:
return gcnew MdfFileLocalTimestampTz(timestamp->GetTimeNs(),
timestamp->GetTzOffsetMin());
case mdf::timetype::kTimezoneTime:
return gcnew MdfFileTimezoneTimestamp(timestamp->GetTimeNs(),
timestamp->GetTzOffsetMin(),
timestamp->GetDstOffsetMin());
default:
throw gcnew System::ArgumentOutOfRangeException("Unknown timestamp type");
}
}

MdfFileUtcTimestamp::MdfFileUtcTimestamp(uint64_t utc_time_ns) {
utc_time_ns_ = utc_time_ns;
}

uint64_t MdfFileUtcTimestamp::TimeNs::get() {
return utc_time_ns_;
}

int16_t MdfFileUtcTimestamp::TimezoneMin::get() {
return 0;
}

int16_t MdfFileUtcTimestamp::DstMin::get() {
return 0;
}

DateTime MdfFileUtcTimestamp::Time::get() {
return MdfNetHelper::GetDateTimeFromUnixNanoTimestamp(utc_time_ns_);
}

MdfFileLocalTimestamp::MdfFileLocalTimestamp(uint64_t local_time_ns) {
local_time_ns_ = local_time_ns;
}

uint64_t MdfFileLocalTimestamp::TimeNs::get() {
return local_time_ns_;
}

int16_t MdfFileLocalTimestamp::TimezoneMin::get() {
return 0;
}

int16_t MdfFileLocalTimestamp::DstMin::get() {
return 0;
}

DateTime MdfFileLocalTimestamp::Time::get() {
return MdfNetHelper::GetDateTimeFromLocalNanoTimestamp(local_time_ns_);
}

MdfFileLocalTimestampTz::MdfFileLocalTimestampTz(uint64_t local_time_ns,
int16_t tz_min) {
local_time_ns_ = local_time_ns;
tz_min_ = tz_min;
}

uint64_t MdfFileLocalTimestampTz::TimeNs::get() {
return local_time_ns_;
}

int16_t MdfFileLocalTimestampTz::TimezoneMin::get() {
return tz_min_;
}

int16_t MdfFileLocalTimestampTz::DstMin::get() {
return 0;
}

DateTime MdfFileLocalTimestampTz::Time::get() {
auto epoch = DateTime(1970, 1, 1, 0, 0, 0, System::DateTimeKind::Utc);
int64_t ticks = local_time_ns_ / 100;
auto offset = System::TimeSpan::FromMinutes(tz_min_);
ticks -= offset.Ticks;
return epoch.AddTicks(ticks);
}
}
61 changes: 58 additions & 3 deletions mdflibrary/src/MdfFileTimestamp.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,72 @@
#pragma once
#include <cstdint>
#include "mdf/imdftimestamp.h"



namespace MdfLibrary {
public ref class MdfFileTimestamp {
public:
MdfFileTimestamp(uint64_t time_ns, int16_t timezone_min, int16_t dst_min);
using System::DateTime;

public interface class IMdfFileTimestamp {
property uint64_t TimeNs { uint64_t get(); }
property int16_t TimezoneMin { int16_t get(); }
property int16_t DstMin { int16_t get(); }
property DateTime Time { DateTime get(); }
};


public ref class MdfFileUtcTimestamp : public IMdfFileTimestamp {
public:
explicit MdfFileUtcTimestamp(uint64_t utc_time_ns);
property uint64_t TimeNs { virtual uint64_t get(); }
property int16_t TimezoneMin { virtual int16_t get(); }
property int16_t DstMin { virtual int16_t get(); }
property DateTime Time { virtual DateTime get(); }

private:
uint64_t utc_time_ns_;
};

public ref class MdfFileLocalTimestamp : public IMdfFileTimestamp {
public:
explicit MdfFileLocalTimestamp(uint64_t local_time_ns);
property uint64_t TimeNs { virtual uint64_t get(); }
property int16_t TimezoneMin { virtual int16_t get(); }
property int16_t DstMin { virtual int16_t get(); }
property DateTime Time { virtual DateTime get(); }

private:
uint64_t local_time_ns_;
};

public ref class MdfFileLocalTimestampTz : public IMdfFileTimestamp {
public:
explicit MdfFileLocalTimestampTz(uint64_t local_time_ns, int16_t tz_min);
property uint64_t TimeNs { virtual uint64_t get(); }
property int16_t TimezoneMin { virtual int16_t get(); }
property int16_t DstMin { virtual int16_t get(); }
property DateTime Time { virtual DateTime get(); }

private:
uint64_t local_time_ns_;
int16_t tz_min_;
};


public ref class MdfFileTimezoneTimestamp : public IMdfFileTimestamp {
public:
MdfFileTimezoneTimestamp(uint64_t time_ns, int16_t timezone_min,
int16_t dst_min);
property uint64_t TimeNs { virtual uint64_t get(); }
property int16_t TimezoneMin { virtual int16_t get(); }
property int16_t DstMin { virtual int16_t get(); }
property DateTime Time { virtual DateTime get(); }

private:
uint64_t time_ns_;
int16_t timezone_min_;
int16_t dst_min_;
};

IMdfFileTimestamp^ GetMdfFileTimestampByIMdfTimestamp(const mdf::IMdfTimestamp* timestamp);
}
4 changes: 2 additions & 2 deletions mdflibrary/src/MdfHeader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,12 @@ void MdfHeader::SetStartTime(IMdfTimeStamp^ timestamp) {
}
}

MdfFileTimestamp^ MdfHeader::GetStartTime() {
IMdfFileTimestamp^ MdfHeader::GetStartTime() {
if (header_ == nullptr) {
return nullptr;
}
const auto time = header_->StartTimestamp();
return gcnew MdfFileTimestamp(time->GetTimeNs(), time->GetTzOffsetMin(), time->GetDstOffsetMin());
return GetMdfFileTimestampByIMdfTimestamp(time);
}

bool MdfHeader::IsStartAngleUsed::get() {
Expand Down
2 changes: 1 addition & 1 deletion mdflibrary/src/MdfHeader.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public ref class MdfHeader {
MdfDataGroup^ CreateDataGroup();
MdfMetaData^ CreateMetaData();
void SetStartTime(IMdfTimeStamp^ timestamp);
MdfFileTimestamp^ GetStartTime();
IMdfFileTimestamp^ GetStartTime();

private:
MdfHeader() {};
Expand Down
Loading

0 comments on commit c37c4aa

Please sign in to comment.