Files
ResInsight/ApplicationLibCode/UnitTests/RiaQDateTimeTools-Test.cpp
Magne Sjaastad 9b268e6d98 #14543 Use time from simulation start in Show Plot Data
When the summary plot time axis is configured to show time from simulation start, the text produced by Show Plot Data and the ASCII export now reports the same values instead of date and time. The time column header becomes "Time [<unit>]", and each row reports the elapsed time relative to the first time step of the first curve, scaled to the display unit selected on the time axis. Plots using the date based time axis are unchanged.

Time in months and years is computed using calendar arithmetic instead of a fixed number of seconds per unit, so a time step exactly N calendar months or years after the simulation start reports exactly N. This makes resampled data report whole numbers also when a leap year is part of the interval. The calendar arithmetic is available as RiaQDateTimeTools::calendarYearsBetween() and calendarMonthsBetween(), and is used both when plotting the curves and when reporting the time column.
2026-08-20 11:26:56 +02:00

60 lines
2.2 KiB
C++

#include "gtest/gtest.h"
#include "RiaQDateTimeTools.h"
#include <QDate>
namespace
{
QDateTime utcTime( int year, int month, int day )
{
return RiaQDateTimeTools::createUtcDateTime( QDate( year, month, day ) );
}
} // namespace
//--------------------------------------------------------------------------------------------------
/// Time steps landing on a calendar year boundary must report a whole number of years, also when a
/// leap year is part of the interval.
//--------------------------------------------------------------------------------------------------
TEST( RiaQDateTimeToolsTest, CalendarYearsBetween )
{
const QDateTime start = utcTime( 2000, 1, 1 );
for ( int i = 0; i <= 10; i++ )
{
EXPECT_NEAR( static_cast<double>( i ), RiaQDateTimeTools::calendarYearsBetween( start, utcTime( 2000 + i, 1, 1 ) ), 1e-9 );
}
// Half way into a non-leap year
EXPECT_NEAR( 1.5, RiaQDateTimeTools::calendarYearsBetween( start, utcTime( 2001, 7, 2 ) ), 1e-2 );
// Start time that is not at a year boundary
EXPECT_NEAR( 3.0, RiaQDateTimeTools::calendarYearsBetween( utcTime( 1997, 11, 6 ), utcTime( 2000, 11, 6 ) ), 1e-9 );
// Time steps before the start time are reported as negative
EXPECT_NEAR( -2.0, RiaQDateTimeTools::calendarYearsBetween( start, utcTime( 1998, 1, 1 ) ), 1e-9 );
EXPECT_NEAR( 0.0, RiaQDateTimeTools::calendarYearsBetween( start, start ), 1e-9 );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
TEST( RiaQDateTimeToolsTest, CalendarMonthsBetween )
{
const QDateTime start = utcTime( 2000, 1, 1 );
for ( int i = 0; i <= 36; i++ )
{
int year = 2000 + i / 12;
int month = 1 + i % 12;
EXPECT_NEAR( static_cast<double>( i ), RiaQDateTimeTools::calendarMonthsBetween( start, utcTime( year, month, 1 ) ), 1e-9 );
}
// 16 days into a 31 day month
EXPECT_NEAR( 16.0 / 31.0, RiaQDateTimeTools::calendarMonthsBetween( start, utcTime( 2000, 1, 17 ) ), 1e-9 );
EXPECT_NEAR( -1.0, RiaQDateTimeTools::calendarMonthsBetween( start, utcTime( 1999, 12, 1 ) ), 1e-9 );
}