Ejemplo n.º 1
0
	QDateTime Exif::dateTime() const {
		QRegExp date_regex("(\\d{4}):(\\d{2}):(\\d{2})\\s+(\\d{2}):(\\d{2}):(\\d{2})");

// 		std::vector<QString> match;
		std::string datetime_value = value("DateTime");

		int year, month, day, hour, minute, second;

		// Parse date time string
		int result = date_regex.indexIn(QString(datetime_value.c_str()));
		if (result == -1)
			throw new DateTimeParseFailure;

		// Must have 6 fields for date/time (year, month, day, hour, minute, second)
		// match object will have 7 fields. The first one is the entire Datetime string, The other 6 are the various parts of the date time.
		if (date_regex.numCaptures() < 6)
			throw new DateTimeParseFailure;

		year = QVariant(date_regex.cap(1)).toInt();
		month = QVariant(date_regex.cap(2)).toInt();
		day = QVariant(date_regex.cap(3)).toInt();
		hour = QVariant(date_regex.cap(4)).toInt();
		minute = QVariant(date_regex.cap(5)).toInt();
		second = QVariant(date_regex.cap(6)).toInt();

		return QDateTime(QDate(year, month, day), QTime(hour, minute, second));
	}
Ejemplo n.º 2
0
/**
* @brief Creates a new file with selected data
* @param asset The asset created
* @param file The file where are located the values.
* @throw ImportException The data is not valid
*/
void ImportNewData::import(const Asset &asset, const QString& file) const {
	QString data;
	QFile importedCSV(file);
	QStringList rowOfData;
	QStringList rowData;
	data.clear();
	rowOfData.clear();
	rowData.clear();
	QRegExp date_regex("^(20|19)[0-9]{2}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$");
	QRegExp value_regex("^([0-9]+)([.])([0-9][0-9])$");
	QDate previousDate = QDate::fromString("2999-01-01", "yyyy-MM-dd");
	int data_index;
	if (asset.getOrigin() == "ProjectVaR") {
		data_index = 1;
	} else {
		data_index = 6;
	}

	if (importedCSV.open(QFile::ReadOnly)) {
		data = importedCSV.readAll();
		rowOfData = data.split(QRegExp("[\r\n]"), QString::SkipEmptyParts);
		importedCSV.close();
	}

	//FILE CREATION OF IMPORTED DATA
	// Do unique names
	QFile fileCreated(asset.getFile());
	// The file is open in write-only mode and we check the opening
	if (!fileCreated.open(QIODevice::WriteOnly | QIODevice::Text)) {
		return;
	}
	QTextStream flux(&fileCreated);
	flux.setCodec("UTF-8");
	QDate endDate = asset.getEndDate();
	QDate startDate = asset.getStartDate();
	rowData = rowOfData.at(0).split(",");
	if (!(rowData.count() < data_index)) {
		if (!((QString) rowData[0]).isEmpty() && !((QString)rowData[data_index]).isEmpty()) {
			flux << rowData[0] << "," << rowData[data_index] << "\n";
			// x = 1 to avoid the first line with labels
			for (int x =1; x < rowOfData.size()-1; x++) {
				rowData = rowOfData.at(x).split(",");
				//Check dates and values are correct
				if(date_regex.exactMatch(rowData[0]) && value_regex.exactMatch(rowData[data_index])) {
					QDate currentDate = QDate::fromString(rowData[0], "yyyy-MM-dd");
					//checks the order of dates
					if(previousDate > currentDate) {
						previousDate = currentDate;
						//checks if we are on still in the range of dates
						if ((endDate >= currentDate)) {
							if(startDate > currentDate) {
								break;
							}
							flux << rowData[0] << "," << rowData[data_index] << "\n";
						}
					} else {
						throw CreateAssetException("Dates are not sorted");
						return;
					}

				} else {
					throw CreateAssetException("The data is invalid");
					return;
				}
			}
		} else {
			throw CreateAssetException("Header is missing");
			return;
		}
	} else {
		throw CreateAssetException("Wrong file type");
		return;
	}
	fileCreated.close();
}