Files

165 lines
6.4 KiB
C++
Raw Permalink Normal View History

2017-09-28 08:04:27 +02:00
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2017 Statoil ASA
//
2017-09-28 08:04:27 +02:00
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
2017-09-28 08:04:27 +02:00
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
2017-09-28 08:04:27 +02:00
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#pragma once
2022-03-14 09:18:48 +01:00
#include "RiaDateTimeDefines.h"
#include <QString>
#include <ctime>
#include <set>
2017-09-28 08:04:27 +02:00
#include <string>
#include <vector>
2017-09-28 08:04:27 +02:00
class QDateTime;
class QDate;
class QTime;
class DateTimeSpan;
namespace caf
{
class PdmOptionItemInfo;
};
2017-09-28 08:04:27 +02:00
//==================================================================================================
//
2017-09-28 08:04:27 +02:00
//==================================================================================================
class RiaQDateTimeTools
2017-09-28 08:04:27 +02:00
{
public:
static Qt::TimeSpec currentTimeSpec();
static QDateTime fromString( const QString& dateString, const QString& format );
static QDateTime fromYears( double years );
static QDateTime fromTime_t( time_t t );
static QDateTime addMSecs( const QDateTime& dt, double msecs );
static QDateTime addDays( const QDateTime& dt, double days );
static QDateTime addYears( const QDateTime& dt, double years );
static QDateTime addSpan( const QDateTime& dt, DateTimeSpan span );
static QDateTime subtractSpan( const QDateTime& dt, DateTimeSpan span );
2022-03-14 09:18:48 +01:00
static QDateTime addPeriod( const QDateTime& dt, RiaDefines::DateTimePeriod period );
static QDateTime subtractPeriod( const QDateTime& dt, RiaDefines::DateTimePeriod period );
2017-09-28 08:04:27 +02:00
static QDateTime createDateTime( const QDate& date, Qt::TimeSpec timeSpec = Qt::LocalTime );
2022-03-11 13:24:01 +01:00
// Number of calendar years/months between two time steps. The remainder is expressed as a fraction of the calendar
// unit it falls inside, so a time step exactly N calendar years/months after the start reports exactly N. Time
// steps before the start time are reported as negative values.
static double calendarYearsBetween( const QDateTime& startTime, const QDateTime& endTime );
static double calendarMonthsBetween( const QDateTime& startTime, const QDateTime& endTime );
static QDateTime epoch();
2017-09-28 08:04:27 +02:00
2017-10-13 16:54:04 +02:00
static QDateTime createUtcDateTime();
static QDateTime createUtcDateTime( const QDate& date );
static QDateTime createUtcDateTime( const QDate& date, const QTime& time );
static QDateTime createUtcDateTime( const QDateTime& dt );
static bool lessThan( const QDateTime& dt1, const QDateTime& dt2 );
2022-03-14 09:18:48 +01:00
static const DateTimeSpan timeSpan( RiaDefines::DateTimePeriod period );
static QDateTime truncateTime( const QDateTime& dt, RiaDefines::DateTimePeriod period );
2022-03-14 09:18:48 +01:00
static std::vector<RiaDefines::DateTimePeriod> dateTimePeriods();
static QString dateTimePeriodName( RiaDefines::DateTimePeriod period );
// This function uses C locale to make sure the text representation of a date is stable, independent of the locale
// settings on local machine. Required for stable regression testing.
static QString toStringUsingApplicationLocale( const QDateTime& dt, const QString& format );
static QString createTimeFormatStringFromDates( const std::vector<QDateTime>& dates );
static QString dateFormatString();
static std::vector<QString> supportedDateFormats();
static std::vector<QString> supportedTimeFormats();
2023-02-26 10:48:40 +01:00
static QString
dateFormatString( const QString& fullDateFormat,
RiaDefines::DateFormatComponents dateComponents = RiaDefines::DateFormatComponents::DATE_FORMAT_YEAR_MONTH_DAY );
static QString
2023-04-13 07:05:53 +02:00
timeFormatString( const QString& fullTimeFormat,
2023-02-26 10:48:40 +01:00
RiaDefines::TimeFormatComponents timeComponents = RiaDefines::TimeFormatComponents::TIME_FORMAT_HOUR_MINUTE_SECOND );
static QList<caf::PdmOptionItemInfo> createOptionItems( const std::vector<time_t>& timeSteps );
static std::set<QDateTime> createEvenlyDistributedDates( const std::vector<QDateTime>& inputDates, int numDates );
2023-04-26 12:31:57 +02:00
static std::set<QDateTime>
createEvenlyDistributedDatesInInterval( const QDateTime& fromTimeStep, const QDateTime& toTimeStep, int numDates );
static std::vector<QDateTime>
getTimeStepsWithinSelectedRange( const std::vector<QDateTime>& timeSteps, const QDateTime& fromTimeStep, const QDateTime& toTimeStep );
2017-09-28 08:04:27 +02:00
private:
static double calendarUnitsBetween( const QDateTime& startTime, const QDateTime& endTime, bool useMonths );
2023-04-20 10:16:43 +02:00
static const DateTimeSpan TIMESPAN_MINUTE;
static const DateTimeSpan TIMESPAN_HOUR;
2022-03-14 09:18:48 +01:00
static const DateTimeSpan TIMESPAN_DAY;
static const DateTimeSpan TIMESPAN_WEEK;
static const DateTimeSpan TIMESPAN_MONTH;
static const DateTimeSpan TIMESPAN_QUARTER;
static const DateTimeSpan TIMESPAN_HALFYEAR;
static const DateTimeSpan TIMESPAN_YEAR;
static const DateTimeSpan TIMESPAN_DECADE;
static quint64 secondsInDay();
static quint64 secondsInYear();
2023-04-20 10:16:43 +02:00
static quint64 secondsInHour();
static quint64 secondsInMinute();
2017-09-28 08:04:27 +02:00
};
//==================================================================================================
///
//==================================================================================================
class DateTimeSpan
{
public:
DateTimeSpan()
: m_years( 0 )
, m_months( 0 )
, m_days( 0 )
2023-04-20 10:16:43 +02:00
, m_hours( 0 )
, m_minutes( 0 )
{
}
2023-04-20 10:16:43 +02:00
DateTimeSpan( int years, int months, int days, int hours = 0, int minutes = 0 )
: m_years( years )
, m_months( months )
, m_days( days )
2023-04-20 10:16:43 +02:00
, m_hours( hours )
, m_minutes( minutes )
{
}
int years() const { return m_years; }
int months() const { return m_months; }
int days() const { return m_days; }
2023-04-20 10:16:43 +02:00
int hours() const { return m_hours; }
int minutes() const { return m_minutes; }
2023-04-20 10:16:43 +02:00
bool isEmpty() { return m_years == 0 && m_months == 0 && m_days == 0 && m_hours == 0 && m_minutes == 0; }
private:
int m_years;
int m_months;
int m_days;
2023-04-20 10:16:43 +02:00
int m_hours;
int m_minutes;
};