2017-09-28 01:04:27 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Copyright (C) 2017 Statoil ASA
|
2019-09-06 03:40:57 -05:00
|
|
|
//
|
2017-09-28 01:04:27 -05: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.
|
2019-09-06 03:40:57 -05:00
|
|
|
//
|
2017-09-28 01:04:27 -05: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.
|
2019-09-06 03:40:57 -05:00
|
|
|
//
|
|
|
|
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
|
2017-09-28 01:04:27 -05:00
|
|
|
// for more details.
|
|
|
|
//
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2018-06-20 02:41:01 -05:00
|
|
|
#include <QString>
|
|
|
|
|
2017-09-28 01:04:27 -05:00
|
|
|
#include <string>
|
2018-06-20 02:41:01 -05:00
|
|
|
#include <vector>
|
2017-09-28 01:04:27 -05:00
|
|
|
|
2022-02-04 06:07:40 -06:00
|
|
|
#include "cafAppEnum.h"
|
|
|
|
|
2017-10-03 07:03:23 -05:00
|
|
|
class QDateTime;
|
2017-10-09 06:03:42 -05:00
|
|
|
class QDate;
|
|
|
|
class QTime;
|
2018-06-12 04:04:36 -05:00
|
|
|
class DateTimeSpan;
|
|
|
|
|
2020-03-29 06:49:55 -05:00
|
|
|
namespace caf
|
|
|
|
{
|
|
|
|
class PdmOptionItemInfo;
|
|
|
|
};
|
|
|
|
|
2017-09-28 01:04:27 -05:00
|
|
|
//==================================================================================================
|
2019-09-06 03:40:57 -05:00
|
|
|
//
|
2017-09-28 01:04:27 -05:00
|
|
|
//==================================================================================================
|
2017-10-03 07:03:23 -05:00
|
|
|
class RiaQDateTimeTools
|
2017-09-28 01:04:27 -05:00
|
|
|
{
|
2018-06-12 04:04:36 -05:00
|
|
|
static const DateTimeSpan TIMESPAN_DAY;
|
2018-07-02 04:45:05 -05:00
|
|
|
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;
|
2018-06-12 04:04:36 -05:00
|
|
|
|
2017-09-28 01:04:27 -05:00
|
|
|
public:
|
2019-08-19 02:37:42 -05:00
|
|
|
enum DateFormatComponents
|
|
|
|
{
|
2019-09-06 03:40:57 -05:00
|
|
|
DATE_FORMAT_UNSPECIFIED = -2,
|
|
|
|
DATE_FORMAT_NONE = -1,
|
|
|
|
DATE_FORMAT_YEAR = 0,
|
2019-08-19 02:37:42 -05:00
|
|
|
DATE_FORMAT_YEAR_MONTH,
|
|
|
|
DATE_FORMAT_YEAR_MONTH_DAY,
|
|
|
|
DATE_FORMAT_SIZE
|
|
|
|
};
|
|
|
|
|
2020-04-24 01:10:48 -05:00
|
|
|
enum class TimeFormatComponents
|
2019-08-19 02:37:42 -05:00
|
|
|
{
|
2019-09-06 03:40:57 -05:00
|
|
|
TIME_FORMAT_UNSPECIFIED = -2,
|
|
|
|
TIME_FORMAT_NONE = -1,
|
2019-08-19 02:37:42 -05:00
|
|
|
TIME_FORMAT_HOUR,
|
|
|
|
TIME_FORMAT_HOUR_MINUTE,
|
|
|
|
TIME_FORMAT_HOUR_MINUTE_SECOND,
|
|
|
|
TIME_FORMAT_HOUR_MINUTE_SECOND_MILLISECOND,
|
|
|
|
TIME_FORMAT_SIZE
|
|
|
|
};
|
|
|
|
|
2020-04-02 00:16:44 -05:00
|
|
|
enum class DateTimePeriod
|
|
|
|
{
|
|
|
|
NONE = -1,
|
|
|
|
DAY,
|
|
|
|
WEEK,
|
|
|
|
MONTH,
|
|
|
|
QUARTER,
|
|
|
|
HALFYEAR,
|
|
|
|
YEAR,
|
|
|
|
DECADE
|
|
|
|
};
|
|
|
|
using DateTimePeriodEnum = caf::AppEnum<DateTimePeriod>;
|
2018-07-02 04:45:05 -05:00
|
|
|
|
2017-10-09 06:03:42 -05:00
|
|
|
static Qt::TimeSpec currentTimeSpec();
|
|
|
|
|
2019-09-06 03:40:57 -05:00
|
|
|
static QDateTime fromString( const QString& dateString, const QString& format );
|
|
|
|
static QDateTime fromYears( double years );
|
|
|
|
static QDateTime fromTime_t( time_t t );
|
2018-06-12 04:04:36 -05:00
|
|
|
|
2019-09-06 03:40:57 -05:00
|
|
|
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 );
|
2020-04-02 00:16:44 -05:00
|
|
|
static QDateTime addPeriod( const QDateTime& dt, RiaQDateTimeTools::DateTimePeriod period );
|
|
|
|
static QDateTime subtractPeriod( const QDateTime& dt, RiaQDateTimeTools::DateTimePeriod period );
|
2017-09-28 01:04:27 -05:00
|
|
|
|
2017-10-03 07:03:23 -05:00
|
|
|
static QDateTime epoch();
|
2017-09-28 01:04:27 -05:00
|
|
|
|
2017-10-13 09:54:04 -05:00
|
|
|
static QDateTime createUtcDateTime();
|
2019-09-06 03:40:57 -05:00
|
|
|
static QDateTime createUtcDateTime( const QDate& date );
|
|
|
|
static QDateTime createUtcDateTime( const QDate& date, const QTime& time );
|
|
|
|
static QDateTime createUtcDateTime( const QDateTime& dt );
|
2018-06-12 04:04:36 -05:00
|
|
|
|
2019-09-06 03:40:57 -05:00
|
|
|
static bool lessThan( const QDateTime& dt1, const QDateTime& dt2 );
|
2018-06-12 04:04:36 -05:00
|
|
|
|
2020-04-02 00:16:44 -05:00
|
|
|
static const DateTimeSpan timeSpan( RiaQDateTimeTools::DateTimePeriod period );
|
|
|
|
static QDateTime truncateTime( const QDateTime& dt, RiaQDateTimeTools::DateTimePeriod period );
|
2017-10-09 06:03:42 -05:00
|
|
|
|
2020-04-02 00:16:44 -05:00
|
|
|
static std::vector<RiaQDateTimeTools::DateTimePeriod> dateTimePeriods();
|
2020-04-24 01:10:48 -05:00
|
|
|
static QString dateTimePeriodName( RiaQDateTimeTools::DateTimePeriod period );
|
2018-06-20 02:41:01 -05:00
|
|
|
|
2018-10-01 03:19:59 -05:00
|
|
|
// 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.
|
2019-09-06 03:40:57 -05:00
|
|
|
static QString toStringUsingApplicationLocale( const QDateTime& dt, const QString& format );
|
2018-10-01 03:19:59 -05:00
|
|
|
|
2019-09-06 03:40:57 -05:00
|
|
|
static QString createTimeFormatStringFromDates( const std::vector<QDateTime>& dates );
|
2019-02-27 04:38:35 -06:00
|
|
|
static QString dateFormatString();
|
|
|
|
|
2019-08-19 02:37:42 -05:00
|
|
|
static std::vector<QString> supportedDateFormats();
|
|
|
|
static std::vector<QString> supportedTimeFormats();
|
|
|
|
|
2020-09-03 01:42:05 -05:00
|
|
|
static QString
|
|
|
|
dateFormatString( const QString& fullDateFormat,
|
|
|
|
DateFormatComponents dateComponents = DateFormatComponents::DATE_FORMAT_YEAR_MONTH_DAY );
|
|
|
|
static QString
|
|
|
|
timeFormatString( const QString& fullTimeFormat,
|
|
|
|
TimeFormatComponents timeComponents = TimeFormatComponents::TIME_FORMAT_HOUR_MINUTE_SECOND );
|
2019-08-19 02:37:42 -05:00
|
|
|
|
2020-03-29 06:49:55 -05:00
|
|
|
static QList<caf::PdmOptionItemInfo> createOptionItems( const std::vector<time_t>& timeSteps );
|
|
|
|
|
2017-09-28 01:04:27 -05:00
|
|
|
private:
|
2019-09-06 03:40:57 -05:00
|
|
|
static quint64 secondsInDay();
|
|
|
|
static quint64 secondsInYear();
|
2017-09-28 01:04:27 -05:00
|
|
|
};
|
2018-06-12 04:04:36 -05:00
|
|
|
|
|
|
|
//==================================================================================================
|
2019-09-06 03:40:57 -05:00
|
|
|
///
|
2018-06-12 04:04:36 -05:00
|
|
|
//==================================================================================================
|
|
|
|
class DateTimeSpan
|
|
|
|
{
|
|
|
|
public:
|
2019-09-06 03:40:57 -05:00
|
|
|
DateTimeSpan()
|
|
|
|
: m_years( 0 )
|
|
|
|
, m_months( 0 )
|
|
|
|
, m_days( 0 )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
DateTimeSpan( int years, int months, int days )
|
|
|
|
: m_years( years )
|
|
|
|
, m_months( months )
|
|
|
|
, m_days( days )
|
|
|
|
{
|
|
|
|
}
|
2018-06-12 04:04:36 -05:00
|
|
|
|
2020-02-12 04:13:38 -06:00
|
|
|
int years() const { return m_years; }
|
|
|
|
int months() const { return m_months; }
|
|
|
|
int days() const { return m_days; }
|
2018-06-12 04:04:36 -05:00
|
|
|
|
2020-02-12 04:13:38 -06:00
|
|
|
bool isEmpty() { return m_years == 0 && m_months == 0 && m_days; }
|
2018-06-12 04:04:36 -05:00
|
|
|
|
|
|
|
private:
|
|
|
|
int m_years;
|
|
|
|
int m_months;
|
|
|
|
int m_days;
|
|
|
|
};
|