Skip to content

Commit

Permalink
add date::year_month constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
unicorny committed Oct 8, 2024
1 parent 7c6f0da commit e75ae19
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 39 deletions.
2 changes: 2 additions & 0 deletions include/gradido_blockchain/lib/DataTypeConverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "rapidjson/document.h"

#include <string>
#include "date/date.h"

namespace DataTypeConverter
{
Expand Down Expand Up @@ -57,6 +58,7 @@ namespace DataTypeConverter
//! \param fmt: https://howardhinnant.github.io/date/date.html#from_stream_formatting
GRADIDOBLOCKCHAIN_EXPORT Timepoint dateTimeStringToTimePoint(const std::string &dateTimeString, const char *fmt = "%F %T");
GRADIDOBLOCKCHAIN_EXPORT std::string timespanToString(const std::chrono::seconds timespan);
GRADIDOBLOCKCHAIN_EXPORT Timepoint monthYearToTimepoint(const date::year_month& ym);

//! \brief go through json object and replace every string entry in base64 format into hex format
//! \return count of replaced strings
Expand Down
5 changes: 5 additions & 0 deletions include/gradido_blockchain/lib/TimepointInterval.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "gradido_blockchain/types.h"
#include "gradido_blockchain/GradidoBlockchainException.h"
#include "gradido_blockchain/lib/DataTypeConverter.h"
#include "date/date.h"

class TimepointInterval
Expand All @@ -16,6 +17,10 @@ class TimepointInterval
throw EndDateBeforeStartDateException("end date for timepoint interval is before start date", startDate, endDate);
}
}
TimepointInterval(const date::year_month& startDate, const date::year_month& endDate)
: TimepointInterval(DataTypeConverter::monthYearToTimepoint(startDate), DataTypeConverter::monthYearToTimepoint(endDate))
{
}

TimepointInterval(Timepoint date)
: mStartDate(date), mEndDate(date) {}
Expand Down
43 changes: 4 additions & 39 deletions src/lib/DataTypeConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,47 +309,12 @@ namespace DataTypeConverter

return fmt.str();
}
using namespace std::chrono;
/*
const Timepoint convertFromProtoTimestamp(const proto::gradido::Timestamp& timestamp)
Timepoint monthYearToTimepoint(const date::year_month& ym)
{
// Convert the seconds and nanoseconds to microseconds
int64_t microseconds = timestamp.seconds() * static_cast<int64_t>(1e6) + timestamp.nanos() / static_cast<int64_t>(1e3);
return system_clock::time_point(std::chrono::microseconds(microseconds));
date::year_month_day ymd(ym.year(), ym.month(), date::day(1));
return std::chrono::sys_days{ ymd };
}
void convertToProtoTimestamp(const Timepoint timestamp, proto::gradido::Timestamp* protoTimestamp)
{
// Convert time_point to duration since epoch
auto duration = timestamp.time_since_epoch();
// Convert duration to seconds and nanoseconds
auto seconds = duration_cast<std::chrono::seconds>(duration);
auto nanos = duration_cast<std::chrono::nanoseconds>(duration) - duration_cast<std::chrono::nanoseconds>(seconds);
// Set the protobuf timestamp fields
protoTimestamp->set_seconds(seconds.count());
protoTimestamp->set_nanos(nanos.count());
}
Timepoint convertFromProtoTimestampSeconds(const proto::gradido::TimestampSeconds& timestampSeconds)
{
// Convert seconds to a duration
auto seconds = std::chrono::seconds{ timestampSeconds.seconds() };
// Create a time_point from the duration
return system_clock::time_point{ seconds };
}
void convertToProtoTimestampSeconds(const Timepoint timestamp, proto::gradido::TimestampSeconds* protoTimestampSeconds)
{
// Get the duration since epoch
auto duration = timestamp.time_since_epoch();
// Convert the duration to seconds
auto seconds = duration_cast<std::chrono::seconds>(duration);
// Set the protobuf timestamp fields
protoTimestampSeconds->set_seconds(seconds.count());
}
//*/

int replaceBase64WithHex(rapidjson::Value& json, rapidjson::Document::AllocatorType& alloc)
{
int count_replacements = 0;
Expand Down

0 comments on commit e75ae19

Please sign in to comment.