Skip to content

Commit

Permalink
#9773 Add option for resampling to hour intervals.
Browse files Browse the repository at this point in the history
  • Loading branch information
kriben committed Apr 19, 2023
1 parent aaa45d9 commit 4aa3f7b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
1 change: 1 addition & 0 deletions ApplicationLibCode/Application/RiaDateTimeDefines.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ enum class TimeFormatComponents
enum class DateTimePeriod
{
NONE = -1,
HOUR,
DAY,
WEEK,
MONTH,
Expand Down
27 changes: 25 additions & 2 deletions ApplicationLibCode/Application/Tools/RiaQDateTimeTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <QDateTime>
#include <QLocale>
#include <QString>
#include <QTime>

#include "cafPdmUiItem.h"

Expand All @@ -32,6 +33,7 @@
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
const DateTimeSpan RiaQDateTimeTools::TIMESPAN_HOUR = DateTimeSpan( 0, 0, 0, 1 );
const DateTimeSpan RiaQDateTimeTools::TIMESPAN_DAY = DateTimeSpan( 0, 0, 1 );
const DateTimeSpan RiaQDateTimeTools::TIMESPAN_WEEK = DateTimeSpan( 0, 0, 7 );
const DateTimeSpan RiaQDateTimeTools::TIMESPAN_MONTH = DateTimeSpan( 0, 1, 0 );
Expand All @@ -48,6 +50,14 @@ Qt::TimeSpec RiaQDateTimeTools::currentTimeSpec()
return Qt::UTC;
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
quint64 RiaQDateTimeTools::secondsInHour()
{
return 60 * 60;
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -142,15 +152,23 @@ QDateTime RiaQDateTimeTools::addYears( const QDateTime& dt, double years )
//--------------------------------------------------------------------------------------------------
QDateTime RiaQDateTimeTools::addSpan( const QDateTime& dt, DateTimeSpan span )
{
return createUtcDateTime( dt ).addYears( span.years() ).addMonths( span.months() ).addDays( span.days() );
return createUtcDateTime( dt )
.addYears( span.years() )
.addMonths( span.months() )
.addDays( span.days() )
.addSecs( span.hours() * RiaQDateTimeTools::secondsInHour() );
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QDateTime RiaQDateTimeTools::subtractSpan( const QDateTime& dt, DateTimeSpan span )
{
return createUtcDateTime( dt ).addYears( -span.years() ).addMonths( -span.months() ).addDays( -span.days() );
return createUtcDateTime( dt )
.addYears( -span.years() )
.addMonths( -span.months() )
.addDays( -span.days() )
.addSecs( -span.hours() * RiaQDateTimeTools::secondsInHour() );
}

//--------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -250,6 +268,8 @@ const DateTimeSpan RiaQDateTimeTools::timeSpan( RiaDefines::DateTimePeriod perio
{
switch ( period )
{
case RiaDefines::DateTimePeriod::HOUR:
return TIMESPAN_HOUR;
case RiaDefines::DateTimePeriod::DAY:
return TIMESPAN_DAY;
case RiaDefines::DateTimePeriod::WEEK:
Expand Down Expand Up @@ -278,9 +298,12 @@ QDateTime RiaQDateTimeTools::truncateTime( const QDateTime& dt, RiaDefines::Date
int m = dt.date().month();
int d = dt.date().day();
int dow = dt.date().dayOfWeek();
int h = dt.time().hour();

switch ( period )
{
case RiaDefines::DateTimePeriod::HOUR:
return createUtcDateTime( QDate( y, m, d ), QTime( h, 0, 0 ) );
case RiaDefines::DateTimePeriod::DAY:
return createUtcDateTime( QDate( y, m, d ) );
case RiaDefines::DateTimePeriod::WEEK:
Expand Down
10 changes: 8 additions & 2 deletions ApplicationLibCode/Application/Tools/RiaQDateTimeTools.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class RiaQDateTimeTools
getTimeStepsWithinSelectedRange( const std::vector<QDateTime>& timeSteps, const QDateTime& fromTimeStep, const QDateTime& toTimeStep );

private:
static const DateTimeSpan TIMESPAN_HOUR;
static const DateTimeSpan TIMESPAN_DAY;
static const DateTimeSpan TIMESPAN_WEEK;
static const DateTimeSpan TIMESPAN_MONTH;
Expand All @@ -109,6 +110,7 @@ class RiaQDateTimeTools

static quint64 secondsInDay();
static quint64 secondsInYear();
static quint64 secondsInHour();
};

//==================================================================================================
Expand All @@ -121,23 +123,27 @@ class DateTimeSpan
: m_years( 0 )
, m_months( 0 )
, m_days( 0 )
, m_hours( 0 )
{
}
DateTimeSpan( int years, int months, int days )
DateTimeSpan( int years, int months, int days, int hours = 0 )
: m_years( years )
, m_months( months )
, m_days( days )
, m_hours( hours )
{
}

int years() const { return m_years; }
int months() const { return m_months; }
int days() const { return m_days; }
int hours() const { return m_hours; }

bool isEmpty() { return m_years == 0 && m_months == 0 && m_days; }
bool isEmpty() { return m_years == 0 && m_months == 0 && m_days == 0 && m_hours == 0; }

private:
int m_years;
int m_months;
int m_days;
int m_hours;
};

0 comments on commit 4aa3f7b

Please sign in to comment.