2017-10-03 07:03:23 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Copyright (C) 2017 Statoil ASA
|
2019-09-06 03:40:57 -05:00
|
|
|
//
|
2017-10-03 07:03:23 -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-10-03 07:03:23 -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-10-03 07:03:23 -05:00
|
|
|
// for more details.
|
|
|
|
//
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "RiaQDateTimeTools.h"
|
|
|
|
|
|
|
|
#include <QDateTime>
|
2018-10-01 03:19:59 -05:00
|
|
|
#include <QLocale>
|
|
|
|
#include <QString>
|
2017-10-03 07:03:23 -05:00
|
|
|
|
2020-03-29 06:49:55 -05:00
|
|
|
#include "cafPdmUiItem.h"
|
|
|
|
|
2018-06-12 04:04:36 -05:00
|
|
|
#include <cvfAssert.h>
|
|
|
|
|
2017-10-03 07:03:23 -05:00
|
|
|
#include <cmath>
|
2019-09-06 03:40:57 -05:00
|
|
|
#include <ctime>
|
2017-10-09 06:03:42 -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
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
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 );
|
|
|
|
const DateTimeSpan RiaQDateTimeTools::TIMESPAN_QUARTER = DateTimeSpan( 0, 3, 0 );
|
|
|
|
const DateTimeSpan RiaQDateTimeTools::TIMESPAN_HALFYEAR = DateTimeSpan( 0, 6, 0 );
|
|
|
|
const DateTimeSpan RiaQDateTimeTools::TIMESPAN_YEAR = DateTimeSpan( 1, 0, 0 );
|
|
|
|
const DateTimeSpan RiaQDateTimeTools::TIMESPAN_DECADE = DateTimeSpan( 10, 0, 0 );
|
|
|
|
|
2017-10-09 06:03:42 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
///
|
2017-10-09 06:03:42 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
Qt::TimeSpec RiaQDateTimeTools::currentTimeSpec()
|
|
|
|
{
|
|
|
|
return Qt::UTC;
|
|
|
|
}
|
|
|
|
|
2017-10-03 07:03:23 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
///
|
2017-10-03 07:03:23 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
quint64 RiaQDateTimeTools::secondsInDay()
|
|
|
|
{
|
|
|
|
return 60 * 60 * 24;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
///
|
2017-10-03 07:03:23 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
quint64 RiaQDateTimeTools::secondsInYear()
|
|
|
|
{
|
|
|
|
return 60 * 60 * 24 * 365;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
///
|
2017-10-03 07:03:23 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
QDateTime RiaQDateTimeTools::fromString( const QString& dateString, const QString& format )
|
2017-10-03 07:03:23 -05:00
|
|
|
{
|
2019-09-06 03:40:57 -05:00
|
|
|
QDateTime dt = QDateTime::fromString( dateString, format );
|
|
|
|
dt.setTimeSpec( currentTimeSpec() );
|
2017-10-03 07:03:23 -05:00
|
|
|
|
|
|
|
return dt;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
///
|
2017-10-03 07:03:23 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
QDateTime RiaQDateTimeTools::fromYears( double years )
|
2017-10-03 07:03:23 -05:00
|
|
|
{
|
|
|
|
double yearsAfterEpoch = years - 1970.0;
|
|
|
|
|
|
|
|
QDateTime dt = RiaQDateTimeTools::epoch();
|
|
|
|
|
2019-09-06 03:40:57 -05:00
|
|
|
return RiaQDateTimeTools::addYears( dt, yearsAfterEpoch );
|
2017-10-03 07:03:23 -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
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
QDateTime RiaQDateTimeTools::fromTime_t( time_t t )
|
2018-06-12 04:04:36 -05:00
|
|
|
{
|
|
|
|
auto qdt = createUtcDateTime();
|
2021-02-22 00:37:45 -06:00
|
|
|
qdt.setSecsSinceEpoch( t );
|
2018-06-12 04:04:36 -05:00
|
|
|
return qdt;
|
|
|
|
}
|
|
|
|
|
2017-11-30 06:41:33 -06:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
///
|
2017-11-30 06:41:33 -06:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
QDateTime RiaQDateTimeTools::addMSecs( const QDateTime& dt, double msecs )
|
2017-11-30 06:41:33 -06:00
|
|
|
{
|
2019-09-06 03:40:57 -05:00
|
|
|
return dt.addMSecs( msecs );
|
2017-11-30 06:41:33 -06:00
|
|
|
}
|
|
|
|
|
2017-10-03 07:03:23 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
///
|
2017-10-03 07:03:23 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
QDateTime RiaQDateTimeTools::addDays( const QDateTime& dt, double days )
|
2017-10-03 07:03:23 -05:00
|
|
|
{
|
2019-09-06 03:40:57 -05:00
|
|
|
double integerPart = 0.0;
|
2017-10-03 07:03:23 -05:00
|
|
|
double fractionPart = 0.0;
|
|
|
|
|
2019-09-06 03:40:57 -05:00
|
|
|
fractionPart = modf( days, &integerPart );
|
2017-10-03 07:03:23 -05:00
|
|
|
|
2019-09-06 03:40:57 -05:00
|
|
|
QDateTime tmp = dt.addDays( integerPart );
|
|
|
|
tmp = tmp.addSecs( fractionPart * RiaQDateTimeTools::secondsInDay() );
|
2017-10-03 07:03:23 -05:00
|
|
|
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
///
|
2017-10-03 07:03:23 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
QDateTime RiaQDateTimeTools::addYears( const QDateTime& dt, double years )
|
2017-10-03 07:03:23 -05:00
|
|
|
{
|
2019-09-06 03:40:57 -05:00
|
|
|
double integerPart = 0.0;
|
2017-10-03 07:03:23 -05:00
|
|
|
double fractionPart = 0.0;
|
|
|
|
|
2019-09-06 03:40:57 -05:00
|
|
|
fractionPart = modf( years, &integerPart );
|
2017-10-03 07:03:23 -05:00
|
|
|
|
2019-09-06 03:40:57 -05:00
|
|
|
QDateTime tmp = dt.addYears( integerPart );
|
|
|
|
tmp = tmp.addSecs( fractionPart * RiaQDateTimeTools::secondsInYear() );
|
2017-10-03 07:03:23 -05:00
|
|
|
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2018-06-12 04:04:36 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
///
|
2018-06-12 04:04:36 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
QDateTime RiaQDateTimeTools::addSpan( const QDateTime& dt, DateTimeSpan span )
|
2018-06-12 04:04:36 -05:00
|
|
|
{
|
2019-09-06 03:40:57 -05:00
|
|
|
return createUtcDateTime( dt ).addYears( span.years() ).addMonths( span.months() ).addDays( span.days() );
|
2018-06-12 04:04:36 -05:00
|
|
|
}
|
|
|
|
|
2018-07-06 04:35:16 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
///
|
2018-07-06 04:35:16 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
QDateTime RiaQDateTimeTools::subtractSpan( const QDateTime& dt, DateTimeSpan span )
|
2018-07-06 04:35:16 -05:00
|
|
|
{
|
2019-09-06 03:40:57 -05:00
|
|
|
return createUtcDateTime( dt ).addYears( -span.years() ).addMonths( -span.months() ).addDays( -span.days() );
|
2018-07-06 04:35:16 -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
|
|
|
//--------------------------------------------------------------------------------------------------
|
2022-03-14 03:18:48 -05:00
|
|
|
QDateTime RiaQDateTimeTools::addPeriod( const QDateTime& dt, RiaDefines::DateTimePeriod period )
|
2018-06-12 04:04:36 -05:00
|
|
|
{
|
2019-09-06 03:40:57 -05:00
|
|
|
return addSpan( dt, timeSpan( period ) );
|
2018-06-12 04:04:36 -05:00
|
|
|
}
|
|
|
|
|
2018-07-06 04:35:16 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
///
|
2018-07-06 04:35:16 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2022-03-14 03:18:48 -05:00
|
|
|
QDateTime RiaQDateTimeTools::subtractPeriod( const QDateTime& dt, RiaDefines::DateTimePeriod period )
|
2018-07-06 04:35:16 -05:00
|
|
|
{
|
2019-09-06 03:40:57 -05:00
|
|
|
return subtractSpan( dt, timeSpan( period ) );
|
2018-07-06 04:35:16 -05:00
|
|
|
}
|
|
|
|
|
2022-03-11 06:24:01 -06:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
QDateTime RiaQDateTimeTools::createDateTime( const QDate& date )
|
|
|
|
{
|
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK( 5, 14, 0 )
|
|
|
|
return date.startOfDay();
|
|
|
|
#else
|
|
|
|
return QDateTime( date, QTime( 0, 0 ) );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-10-03 07:03:23 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
///
|
2017-10-03 07:03:23 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
QDateTime RiaQDateTimeTools::epoch()
|
|
|
|
{
|
|
|
|
// NB: Not able to use QDateTime::fromMSecsSinceEpoch as this was introduced in Qt 4.7
|
|
|
|
|
|
|
|
QDateTime dt;
|
2019-09-06 03:40:57 -05:00
|
|
|
dt.setDate( QDate( 1970, 1, 1 ) );
|
|
|
|
dt.setTimeSpec( currentTimeSpec() );
|
2017-10-03 07:03:23 -05:00
|
|
|
|
|
|
|
return dt;
|
|
|
|
}
|
2017-10-09 06:03:42 -05:00
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
///
|
2017-10-09 06:03:42 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2017-10-13 09:54:04 -05:00
|
|
|
QDateTime RiaQDateTimeTools::createUtcDateTime()
|
2017-10-09 06:03:42 -05:00
|
|
|
{
|
2017-10-09 06:33:56 -05:00
|
|
|
auto qdt = QDateTime();
|
2019-09-06 03:40:57 -05:00
|
|
|
qdt.setTimeSpec( currentTimeSpec() );
|
2017-10-09 06:03:42 -05:00
|
|
|
return qdt;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
///
|
2017-10-09 06:03:42 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
QDateTime RiaQDateTimeTools::createUtcDateTime( const QDate& date )
|
2017-10-09 06:03:42 -05:00
|
|
|
{
|
2022-03-11 06:24:01 -06:00
|
|
|
auto qdt = createDateTime( date );
|
2019-09-06 03:40:57 -05:00
|
|
|
qdt.setTimeSpec( currentTimeSpec() );
|
2017-10-09 06:03:42 -05:00
|
|
|
return qdt;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
///
|
2017-10-09 06:03:42 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
QDateTime RiaQDateTimeTools::createUtcDateTime( const QDate& date, const QTime& time )
|
2017-10-09 06:03:42 -05:00
|
|
|
{
|
2019-09-06 03:40:57 -05:00
|
|
|
auto qdt = QDateTime( date, time, currentTimeSpec() );
|
2017-10-09 06:03:42 -05:00
|
|
|
return qdt;
|
|
|
|
}
|
2018-06-12 04:04:36 -05:00
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
///
|
2018-06-12 04:04:36 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
QDateTime RiaQDateTimeTools::createUtcDateTime( const QDateTime& dt )
|
2018-06-12 04:04:36 -05:00
|
|
|
{
|
2019-09-06 03:40:57 -05:00
|
|
|
auto qdt = QDateTime( dt );
|
|
|
|
qdt.setTimeSpec( currentTimeSpec() );
|
2018-06-12 04:04:36 -05:00
|
|
|
return qdt;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
///
|
2018-06-12 04:04:36 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
bool RiaQDateTimeTools::lessThan( const QDateTime& dt1, const QDateTime& dt2 )
|
2018-06-12 04:04:36 -05:00
|
|
|
{
|
|
|
|
// dt1 < dt2
|
2019-09-06 03:40:57 -05:00
|
|
|
return dt1.secsTo( dt2 ) > 0;
|
2018-06-12 04:04:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
///
|
2018-06-12 04:04:36 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2022-03-14 03:18:48 -05:00
|
|
|
const DateTimeSpan RiaQDateTimeTools::timeSpan( RiaDefines::DateTimePeriod period )
|
2018-06-12 04:04:36 -05:00
|
|
|
{
|
2019-09-06 03:40:57 -05:00
|
|
|
switch ( period )
|
2018-06-12 04:04:36 -05:00
|
|
|
{
|
2022-03-14 03:18:48 -05:00
|
|
|
case RiaDefines::DateTimePeriod::DAY:
|
2019-09-06 03:40:57 -05:00
|
|
|
return TIMESPAN_DAY;
|
2022-03-14 03:18:48 -05:00
|
|
|
case RiaDefines::DateTimePeriod::WEEK:
|
2019-09-06 03:40:57 -05:00
|
|
|
return TIMESPAN_WEEK;
|
2022-03-14 03:18:48 -05:00
|
|
|
case RiaDefines::DateTimePeriod::MONTH:
|
2019-09-06 03:40:57 -05:00
|
|
|
return TIMESPAN_MONTH;
|
2022-03-14 03:18:48 -05:00
|
|
|
case RiaDefines::DateTimePeriod::QUARTER:
|
2019-09-06 03:40:57 -05:00
|
|
|
return TIMESPAN_QUARTER;
|
2022-03-14 03:18:48 -05:00
|
|
|
case RiaDefines::DateTimePeriod::HALFYEAR:
|
2019-09-06 03:40:57 -05:00
|
|
|
return TIMESPAN_HALFYEAR;
|
2022-03-14 03:18:48 -05:00
|
|
|
case RiaDefines::DateTimePeriod::YEAR:
|
2019-09-06 03:40:57 -05:00
|
|
|
return TIMESPAN_YEAR;
|
2022-03-14 03:18:48 -05:00
|
|
|
case RiaDefines::DateTimePeriod::DECADE:
|
2019-09-06 03:40:57 -05:00
|
|
|
return TIMESPAN_DECADE;
|
2018-06-12 04:04:36 -05:00
|
|
|
}
|
2019-09-06 03:40:57 -05:00
|
|
|
CVF_ASSERT( false );
|
2018-06-12 04:04:36 -05:00
|
|
|
return DateTimeSpan();
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
///
|
2018-06-12 04:04:36 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2022-03-14 03:18:48 -05:00
|
|
|
QDateTime RiaQDateTimeTools::truncateTime( const QDateTime& dt, RiaDefines::DateTimePeriod period )
|
2018-06-12 04:04:36 -05:00
|
|
|
{
|
2019-09-06 03:40:57 -05:00
|
|
|
int y = dt.date().year();
|
|
|
|
int m = dt.date().month();
|
|
|
|
int d = dt.date().day();
|
2018-06-18 07:10:11 -05:00
|
|
|
int dow = dt.date().dayOfWeek();
|
|
|
|
|
2019-09-06 03:40:57 -05:00
|
|
|
switch ( period )
|
2018-06-12 04:04:36 -05:00
|
|
|
{
|
2022-03-14 03:18:48 -05:00
|
|
|
case RiaDefines::DateTimePeriod::DAY:
|
2019-09-06 03:40:57 -05:00
|
|
|
return createUtcDateTime( QDate( y, m, d ) );
|
2022-03-14 03:18:48 -05:00
|
|
|
case RiaDefines::DateTimePeriod::WEEK:
|
2019-09-06 03:40:57 -05:00
|
|
|
return createUtcDateTime( QDate( y, m, d ).addDays( -dow + 1 ) );
|
2022-03-14 03:18:48 -05:00
|
|
|
case RiaDefines::DateTimePeriod::MONTH:
|
2019-09-06 03:40:57 -05:00
|
|
|
return createUtcDateTime( QDate( y, m, 1 ) );
|
2022-03-14 03:18:48 -05:00
|
|
|
case RiaDefines::DateTimePeriod::QUARTER:
|
2019-09-06 03:40:57 -05:00
|
|
|
return createUtcDateTime( QDate( y, ( ( m - 1 ) / 3 ) * 3 + 1, 1 ) );
|
2022-03-14 03:18:48 -05:00
|
|
|
case RiaDefines::DateTimePeriod::HALFYEAR:
|
2019-09-06 03:40:57 -05:00
|
|
|
return createUtcDateTime( QDate( y, ( ( m - 1 ) / 6 ) * 6 + 1, 1 ) );
|
2022-03-14 03:18:48 -05:00
|
|
|
case RiaDefines::DateTimePeriod::YEAR:
|
2019-09-06 03:40:57 -05:00
|
|
|
return createUtcDateTime( QDate( y, 1, 1 ) );
|
2022-03-14 03:18:48 -05:00
|
|
|
case RiaDefines::DateTimePeriod::DECADE:
|
2019-09-06 03:40:57 -05:00
|
|
|
return createUtcDateTime( QDate( ( y / 10 ) * 10, 1, 1 ) );
|
2018-06-12 04:04:36 -05:00
|
|
|
}
|
2019-09-06 03:40:57 -05:00
|
|
|
CVF_ASSERT( false );
|
2018-06-12 04:04:36 -05:00
|
|
|
return createUtcDateTime();
|
|
|
|
}
|
2018-06-20 02:41:01 -05:00
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
///
|
2018-06-20 02:41:01 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2022-03-14 03:18:48 -05:00
|
|
|
std::vector<RiaDefines::DateTimePeriod> RiaQDateTimeTools::dateTimePeriods()
|
2018-06-20 02:41:01 -05:00
|
|
|
{
|
2022-03-14 03:18:48 -05:00
|
|
|
std::vector<RiaDefines::DateTimePeriod> allPeriods;
|
2020-04-02 00:16:44 -05:00
|
|
|
|
2022-03-14 03:18:48 -05:00
|
|
|
for ( size_t i = 0; i < RiaDefines::DateTimePeriodEnum::size(); i++ )
|
2020-04-02 00:16:44 -05:00
|
|
|
{
|
2022-03-14 03:18:48 -05:00
|
|
|
allPeriods.push_back( RiaDefines::DateTimePeriodEnum::fromIndex( i ) );
|
2020-04-02 00:16:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return allPeriods;
|
2018-06-20 02:41:01 -05:00
|
|
|
}
|
2018-07-02 04:45:05 -05:00
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
///
|
2018-07-02 04:45:05 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2022-03-14 03:18:48 -05:00
|
|
|
QString RiaQDateTimeTools::dateTimePeriodName( RiaDefines::DateTimePeriod period )
|
2018-07-02 04:45:05 -05:00
|
|
|
{
|
2022-03-14 03:18:48 -05:00
|
|
|
return RiaDefines::DateTimePeriodEnum::uiText( period );
|
2018-07-02 04:45:05 -05:00
|
|
|
}
|
2018-10-01 03:19:59 -05:00
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
///
|
2018-10-01 03:19:59 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
QString RiaQDateTimeTools::toStringUsingApplicationLocale( const QDateTime& dt, const QString& format )
|
2018-10-01 03:19:59 -05:00
|
|
|
{
|
|
|
|
// Default application locale is set in RiaMain
|
|
|
|
// QLocale::setDefault(QLocale(QLocale::English, QLocale::UnitedStates));
|
|
|
|
//
|
|
|
|
// QDate/QDateTime use system locale for toString() functions
|
|
|
|
|
|
|
|
QLocale defaultApplicationLocale;
|
|
|
|
|
2019-09-06 03:40:57 -05:00
|
|
|
return defaultApplicationLocale.toString( dt, format );
|
2018-10-01 03:19:59 -05:00
|
|
|
}
|
2019-02-27 04:38:35 -06:00
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
QString RiaQDateTimeTools::createTimeFormatStringFromDates( const std::vector<QDateTime>& dates )
|
2019-02-27 04:38:35 -06:00
|
|
|
{
|
|
|
|
bool hasHoursAndMinutesInTimesteps = false;
|
|
|
|
bool hasSecondsInTimesteps = false;
|
|
|
|
bool hasMillisecondsInTimesteps = false;
|
|
|
|
|
2019-09-06 03:40:57 -05:00
|
|
|
for ( size_t i = 0; i < dates.size(); i++ )
|
2019-02-27 04:38:35 -06:00
|
|
|
{
|
2019-09-06 03:40:57 -05:00
|
|
|
if ( dates[i].time().msec() != 0.0 )
|
2019-02-27 04:38:35 -06:00
|
|
|
{
|
|
|
|
hasMillisecondsInTimesteps = true;
|
|
|
|
hasSecondsInTimesteps = true;
|
|
|
|
hasHoursAndMinutesInTimesteps = true;
|
|
|
|
break;
|
|
|
|
}
|
2019-09-06 03:40:57 -05:00
|
|
|
else if ( dates[i].time().second() != 0.0 )
|
2019-02-27 04:38:35 -06:00
|
|
|
{
|
|
|
|
hasHoursAndMinutesInTimesteps = true;
|
|
|
|
hasSecondsInTimesteps = true;
|
|
|
|
}
|
2019-09-06 03:40:57 -05:00
|
|
|
else if ( dates[i].time().hour() != 0.0 || dates[i].time().minute() != 0.0 )
|
2019-02-27 04:38:35 -06:00
|
|
|
{
|
|
|
|
hasHoursAndMinutesInTimesteps = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString formatString = dateFormatString();
|
2019-09-06 03:40:57 -05:00
|
|
|
if ( hasHoursAndMinutesInTimesteps )
|
2019-02-27 04:38:35 -06:00
|
|
|
{
|
|
|
|
formatString += " - hh:mm";
|
2019-09-06 03:40:57 -05:00
|
|
|
if ( hasSecondsInTimesteps )
|
2019-02-27 04:38:35 -06:00
|
|
|
{
|
|
|
|
formatString += ":ss";
|
2019-09-06 03:40:57 -05:00
|
|
|
if ( hasMillisecondsInTimesteps )
|
2019-02-27 04:38:35 -06:00
|
|
|
{
|
|
|
|
formatString += ".zzz";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return formatString;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
QString RiaQDateTimeTools::dateFormatString()
|
|
|
|
{
|
|
|
|
return "dd.MMM yyyy";
|
2019-08-19 02:37:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
std::vector<QString> RiaQDateTimeTools::supportedDateFormats()
|
|
|
|
{
|
|
|
|
std::vector<QString> dateFormats;
|
|
|
|
|
|
|
|
// See enum DateFormatComponents in header
|
|
|
|
// The semi-colon separated components are:
|
|
|
|
// DATE_FORMAT_YEAR, ..YEAR_MONTH, ..YEAR_MONTH_DAY
|
2019-09-06 03:40:57 -05:00
|
|
|
dateFormats.push_back( "yyyy;yyyy-MM;yyyy-MM-dd" );
|
|
|
|
dateFormats.push_back( "yyyy;MMM yyyy;dd. MMM yyyy" );
|
|
|
|
dateFormats.push_back( "yyyy;MMM yyyy;MMM dd. yyyy" );
|
|
|
|
dateFormats.push_back( "yyyy;MM/yyyy;dd/MM/yyyy" );
|
|
|
|
dateFormats.push_back( "yyyy;M/yyyy;d/M/yyyy" );
|
|
|
|
dateFormats.push_back( "yyyy;M/yyyy;M/d/yyyy" );
|
|
|
|
dateFormats.push_back( "yy;M/yy;d/M/yy" );
|
|
|
|
dateFormats.push_back( "yy;M/yy;M/d/yy" );
|
|
|
|
dateFormats.push_back( "yyyy;MM-yyyy;dd-MM-yyyy" );
|
|
|
|
dateFormats.push_back( "yyyy;MM.yyyy;dd.MM.yyyy" );
|
|
|
|
dateFormats.push_back( "yyyy;MM-yyyy;MM-dd-yyyy" );
|
|
|
|
dateFormats.push_back( "yyyy;MM.yyyy;MM.dd.yyyy" );
|
|
|
|
dateFormats.push_back( "yy;MM-yy;dd-MM-yy" );
|
|
|
|
dateFormats.push_back( "yy;MM-yy;MM-dd-yy" );
|
2019-08-19 02:37:42 -05:00
|
|
|
|
|
|
|
return dateFormats;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
std::vector<QString> RiaQDateTimeTools::supportedTimeFormats()
|
|
|
|
{
|
|
|
|
std::vector<QString> timeFormats;
|
|
|
|
|
|
|
|
// See enum TimeFormatComponents in header
|
|
|
|
// The semi-colon separated components are:
|
|
|
|
// TIME_FORMAT_HOUR, ..HOUR_MINUTE, ..HOUR_MINUTE_SECOND and ..HOUR_MINUTE_MILLISECOND
|
2019-09-06 03:40:57 -05:00
|
|
|
timeFormats.push_back( "HH;HH:mm;HH:mm:ss;HH:mm:ss.zzz" );
|
|
|
|
timeFormats.push_back( "H;H:mm;H:mm:ss;H:mm:ss.zzz" );
|
|
|
|
timeFormats.push_back( "hh AP;hh:mm AP;hh:mm:ss AP;hh:mm:ss.zzz AP" );
|
|
|
|
timeFormats.push_back( "h AP;h:mm AP;h:mm:ss AP;h:mm:ss.zzz AP" );
|
2019-08-19 02:37:42 -05:00
|
|
|
|
|
|
|
return timeFormats;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2022-03-14 03:18:48 -05:00
|
|
|
QString RiaQDateTimeTools::dateFormatString( const QString& fullDateFormat, RiaDefines::DateFormatComponents dateComponents )
|
2019-08-19 02:37:42 -05:00
|
|
|
{
|
2022-03-14 03:18:48 -05:00
|
|
|
if ( dateComponents == RiaDefines::DateFormatComponents::DATE_FORMAT_NONE ) return "";
|
|
|
|
|
|
|
|
auto enumValue = static_cast<std::underlying_type<RiaDefines::DateFormatComponents>::type>( dateComponents );
|
|
|
|
if ( enumValue < 0 ) return "";
|
2019-08-19 02:37:42 -05:00
|
|
|
|
2019-09-06 03:40:57 -05:00
|
|
|
QStringList allVariants = fullDateFormat.split( ";" );
|
2022-03-14 03:18:48 -05:00
|
|
|
if ( enumValue < allVariants.size() )
|
2019-08-19 02:37:42 -05:00
|
|
|
{
|
2022-03-14 03:18:48 -05:00
|
|
|
return allVariants[enumValue];
|
2019-08-19 02:37:42 -05:00
|
|
|
}
|
2019-09-06 03:40:57 -05:00
|
|
|
CVF_ASSERT( false && "Date format string is malformed" );
|
2019-08-19 02:37:42 -05:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2022-03-14 03:18:48 -05:00
|
|
|
QString RiaQDateTimeTools::timeFormatString( const QString& fullTimeFormat, RiaDefines::TimeFormatComponents timeComponents )
|
2019-08-19 02:37:42 -05:00
|
|
|
{
|
2022-03-14 03:18:48 -05:00
|
|
|
if ( timeComponents == RiaDefines::TimeFormatComponents::TIME_FORMAT_NONE ) return "";
|
2019-08-19 02:37:42 -05:00
|
|
|
|
2019-09-06 03:40:57 -05:00
|
|
|
QStringList allVariants = fullTimeFormat.split( ";" );
|
|
|
|
if ( static_cast<int>( timeComponents ) < allVariants.size() )
|
2019-08-19 02:37:42 -05:00
|
|
|
{
|
2020-04-24 01:10:48 -05:00
|
|
|
return allVariants[static_cast<int>( timeComponents )];
|
2019-08-19 02:37:42 -05:00
|
|
|
}
|
2019-09-06 03:40:57 -05:00
|
|
|
CVF_ASSERT( false && "Time format string is malformed" );
|
2019-08-19 02:37:42 -05:00
|
|
|
return "";
|
2020-03-29 06:49:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
QList<caf::PdmOptionItemInfo> RiaQDateTimeTools::createOptionItems( const std::vector<time_t>& timeSteps )
|
|
|
|
{
|
|
|
|
QList<caf::PdmOptionItemInfo> options;
|
|
|
|
|
|
|
|
std::vector<QDateTime> dateTimes;
|
|
|
|
for ( time_t timeT : timeSteps )
|
|
|
|
{
|
|
|
|
QDateTime dateTime = RiaQDateTimeTools::fromTime_t( timeT );
|
|
|
|
|
|
|
|
dateTimes.push_back( dateTime );
|
|
|
|
}
|
|
|
|
|
|
|
|
QString formatString = RiaQDateTimeTools::createTimeFormatStringFromDates( dateTimes );
|
|
|
|
|
|
|
|
for ( size_t i = 0; i < dateTimes.size(); i++ )
|
|
|
|
{
|
|
|
|
const auto& dt = dateTimes[i];
|
|
|
|
QString text = RiaQDateTimeTools::toStringUsingApplicationLocale( dt, formatString );
|
2020-11-06 03:46:38 -06:00
|
|
|
options.push_back( { text, static_cast<int>( i ) } );
|
2020-03-29 06:49:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return options;
|
|
|
|
}
|