struct transaction * mergeSortedArrays(struct transaction *A, int ALen, struct transaction *B, int BLen)
{
	int index1 = 0, index2 = 0, resindex = 0, compare;
	date d1, d2;
	if (A == NULL || B == NULL)
		return NULL;
	struct transaction *result = (struct transaction *)malloc(sizeof(struct transaction)*(ALen + BLen));
	while ((index1 < ALen) && (index2 < BLen))
	{
		getDayMonthYear(A[index1].date, &d1);
		getDayMonthYear(B[index2].date, &d2);
		compare = compareDates(d1, d2);
		if (compare == 1)
			result[resindex++] = B[index2++];
		else if (compare == -1)
			result[resindex++] = A[index1++];
		else
		{
			result[resindex++] = A[index1++];
			result[resindex++] = B[index2++];
		}
	}
	while (index1 < ALen)
		result[resindex++] = A[index1++];
	while (index2 < BLen)
		result[resindex++] = B[index2++];
	return result;
}
//--------------------------------------------------------------------------------------------------
/// Get list of time step texts (dates)
//--------------------------------------------------------------------------------------------------
void RifEclipseOutputFileTools::timeSteps(ecl_file_type* ecl_file, std::vector<QDateTime>* timeSteps, std::vector<double>* daysSinceSimulationStart)
{
    if (!ecl_file) return;

    CVF_ASSERT(timeSteps && daysSinceSimulationStart);

    timeSteps->clear();
    daysSinceSimulationStart->clear();

    // Get the number of occurrences of the INTEHEAD keyword
    int numINTEHEAD = ecl_file_get_num_named_kw(ecl_file, INTEHEAD_KW);
    
    // Get the number of occurrences of the DOUBHEAD keyword
    int numDOUBHEAD = ecl_file_get_num_named_kw(ecl_file, DOUBHEAD_KW);

    std::vector<double> dayValues(numINTEHEAD, 0.0); // Init fraction to zero

    // Read out fraction of day if number of keywords are identical
    if (numINTEHEAD == numDOUBHEAD)
    {
        for (int i = 0; i < numDOUBHEAD; i++)
        {
            ecl_kw_type* kwDOUBHEAD = ecl_file_iget_named_kw(ecl_file, DOUBHEAD_KW, i);
            if (kwDOUBHEAD)
            {
                dayValues[i] = ecl_kw_iget_double(kwDOUBHEAD, DOUBHEAD_DAYS_INDEX);
            }
        }
    }

    std::set<QDateTime> existingTimesteps;

    for (int i = 0; i < numINTEHEAD; i++)
    {
        ecl_kw_type* kwINTEHEAD = ecl_file_iget_named_kw(ecl_file, INTEHEAD_KW, i);
        CVF_ASSERT(kwINTEHEAD);
        int day = 0;
        int month = 0;
        int year = 0;
        getDayMonthYear(kwINTEHEAD, &day, &month, &year);

        QDateTime reportDateTime = RiaQDateTimeTools::createUtcDateTime(QDate(year, month, day));
        CVF_ASSERT(reportDateTime.isValid());

        double dayValue = dayValues[i];
        double dayFraction = dayValue - cvf::Math::floor(dayValue);
        double milliseconds = dayFraction * 24.0 * 60.0 * 60.0 * 1000.0;

        reportDateTime = reportDateTime.addMSecs(milliseconds);
        if (existingTimesteps.insert(reportDateTime).second)
        {
            timeSteps->push_back(reportDateTime);
            daysSinceSimulationStart->push_back(dayValue);
        }
    }
}
//--------------------------------------------------------------------------------------------------
/// Get list of time step texts (dates)
//--------------------------------------------------------------------------------------------------
void RifEclipseOutputFileTools::timeSteps(ecl_file_type* ecl_file, std::vector<QDateTime>* timeSteps, bool* detectedFractionOfDay )
{
    CVF_ASSERT(timeSteps);
    CVF_ASSERT(ecl_file);

    // Get the number of occurrences of the INTEHEAD keyword
    int numINTEHEAD = ecl_file_get_num_named_kw(ecl_file, INTEHEAD_KW);

    // Get the number of occurrences of the DOUBHEAD keyword
    int numDOUBHEAD = ecl_file_get_num_named_kw(ecl_file, DOUBHEAD_KW);

    CVF_ASSERT(numINTEHEAD == numDOUBHEAD);

    bool hasFractionOfDay = false;
    bool foundAllDayValues = false;
    const double delta = 0.001;

    // Find all days, and stop when the double value is lower than the previous
    QList<double> days;
    for (int i = 0; i < numDOUBHEAD; i++)
    {
        if (foundAllDayValues) continue;;

        ecl_kw_type* kwDOUBHEAD = ecl_file_iget_named_kw(ecl_file, DOUBHEAD_KW, i);
        if (kwDOUBHEAD)
        {
            double dayValue = ecl_kw_iget_double(kwDOUBHEAD, DOUBHEAD_DAYS_INDEX);
            double floorDayValue = cvf::Math::floor(dayValue);

            if (dayValue - floorDayValue > delta)
            {
                hasFractionOfDay = true;
            }

            days.push_back(dayValue);
        }
    }

    std::vector<QDateTime> timeStepsFound;

    if (hasFractionOfDay)
    {
        ecl_kw_type* kwINTEHEAD = ecl_file_iget_named_kw(ecl_file, INTEHEAD_KW, 0);
        if (kwINTEHEAD)
        {
            int day = 0;
            int month = 0;
            int year = 0;
            getDayMonthYear(kwINTEHEAD, &day, &month, &year);

            QDateTime simulationStart(QDate(year, month, day));
            for (int i = 0; i < days.size(); i++)
            {
                QDateTime reportDateTime(simulationStart);
                CVF_ASSERT(reportDateTime.isValid());

                double dayValue = days[i];
                double floorDayValue = cvf::Math::floor(dayValue);
                reportDateTime = reportDateTime.addDays(static_cast<int>(floorDayValue));

                double dayFraction = dayValue - floorDayValue;
                int seconds = static_cast<int>(dayFraction * 24.0 * 60.0 * 60.0);
                QTime time(0, 0);
                time = time.addSecs(seconds);

                reportDateTime.setTime(time);

                if (std::find(timeStepsFound.begin(), timeStepsFound.end(), reportDateTime) ==  timeStepsFound.end())
                {
                    timeStepsFound.push_back(reportDateTime);
                }
            }
        }
    }
    else
    {
        for (int i = 0; i < numINTEHEAD; i++)
        {
            ecl_kw_type* kwINTEHEAD = ecl_file_iget_named_kw(ecl_file, INTEHEAD_KW, i);
            if (kwINTEHEAD)
            {
                int day = 0;
                int month = 0;
                int year = 0;
                getDayMonthYear(kwINTEHEAD, &day, &month, &year);

                QDateTime reportDateTime(QDate(year, month, day));
                QTime time(0, 0);
                reportDateTime.setTime(time);

                CVF_ASSERT(reportDateTime.isValid());

                if (std::find(timeStepsFound.begin(), timeStepsFound.end(), reportDateTime) ==  timeStepsFound.end())
                {
                    timeStepsFound.push_back(reportDateTime);
                }
            }
        }
    }

    // Return time step info to caller
    *timeSteps = timeStepsFound;

    if (detectedFractionOfDay)
    {
        *detectedFractionOfDay = hasFractionOfDay;
    }
}