src/lib/src/functions.cpp
/**
 * Convert a danbooru-like date (Sat May 14 17:38:04 -0400 2011) to a valid QDateTime.
 * @param	str				The date string.
 * @return	The converted date as a QDateTime.
 */
Convert a danbooru-like date (Sat May 14 17:38:04 -0400 2011) to a valid QDateTime.
@param       str                             The date string.
@return      The converted date as a QDateTime.
Convert a danbooru - like date ( Sat May 14 17 : 38 : 04 - 0400 2011 ) to a valid QDateTime . @param str The date string . @return The converted date as a QDateTime .
QDateTime qDateTimeFromString(const QString &str)
{
	QDateTime date;
	const uint toInt = str.toUInt();
	if (toInt != 0) {
		date = QDateTime::fromSecsSinceEpoch(toInt, Qt::UTC);
	} else if (str.length() == 19) {
		date = QDateTime::fromString(str, QStringLiteral("yyyy/MM/dd HH:mm:ss"));
		if (!date.isValid()) {
			date = QDateTime::fromString(str, QStringLiteral("yyyy-MM-dd HH:mm:ss"));
		}
		date.setTimeSpec(Qt::UTC);
	} else if (str.length() == 16) {
		date = QDateTime::fromString(str, QStringLiteral("yyyy/MM/dd HH:mm"));
		if (!date.isValid()) {
			date = QDateTime::fromString(str, QStringLiteral("yyyy-MM-dd HH:mm"));
		}
		date.setTimeSpec(Qt::UTC);
	} else if (str[0].isDigit()) {
		qreal decay = 0;
		date = QDateTime::fromString(str.left(19), QStringLiteral("yyyy-MM-dd'T'HH:mm:ss"));
		if (!date.isValid()) {
			date = QDateTime::fromString(str.left(19), QStringLiteral("yyyy/MM/dd HH:mm:ss"));
		} else {
			decay = str.right(6).remove(':').toDouble() / 100;
		}
		date.setOffsetFromUtc(qFloor(3600 * decay));
	} else {
		QLocale myLoc(QLocale::English);
		date = myLoc.toDateTime(str, QStringLiteral("ddd MMM dd HH:mm:ss yyyy"));
		if (!date.isValid()) {
			date = myLoc.toDateTime(str, QStringLiteral("ddd MMM  d HH:mm:ss yyyy"));
		}
		if (date.isValid()) {
			date.setTimeSpec(Qt::UTC);
			return date;
		}
		const QStringList months { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
		const int year = str.midRef(26, 4).toInt();
		const int month = months.indexOf(str.mid(4, 3)) + 1;
		const int day = str.midRef(8, 2).toInt();
		const qreal decay = str.midRef(20, 5).toDouble() / 100;
		const QTime time = QTime::fromString(str.mid(11, 8), QStringLiteral("HH:mm:ss"));
		date.setDate(QDate(year, month, day));
		date.setTime(time);
		date.setOffsetFromUtc(qFloor(3600 * decay));
	}
	return date;
}
QDateTime qDateTimeFromString ( const QString & str ) { QDateTime date ; const uint toInt = str . toUInt ( ) ; if ( toInt != 0 ) { date = QDateTime :: fromSecsSinceEpoch ( toInt , Qt :: UTC ) ; } else if ( str . length ( ) == 19 ) { date = QDateTime :: fromString ( str , QStringLiteral ( " " ) ) ; if ( ! date . isValid ( ) ) { date = QDateTime :: fromString ( str , QStringLiteral ( " " ) ) ; } date . setTimeSpec ( Qt :: UTC ) ; } else if ( str . length ( ) == 16 ) { date = QDateTime :: fromString ( str , QStringLiteral ( " " ) ) ; if ( ! date . isValid ( ) ) { date = QDateTime :: fromString ( str , QStringLiteral ( " " ) ) ; } date . setTimeSpec ( Qt :: UTC ) ; } else if ( str [ 0 ] . isDigit ( ) ) { qreal decay = 0 ; date = QDateTime :: fromString ( str . left ( 19 ) , QStringLiteral ( " " ) ) ; if ( ! date . isValid ( ) ) { date = QDateTime :: fromString ( str . left ( 19 ) , QStringLiteral ( " " ) ) ; } else { decay = str . right ( 6 ) . remove ( ' ' ) . toDouble ( ) / 100 ; } date . setOffsetFromUtc ( qFloor ( 3600 * decay ) ) ; } else { QLocale myLoc ( QLocale :: English ) ; date = myLoc . toDateTime ( str , QStringLiteral ( " " ) ) ; if ( ! date . isValid ( ) ) { date = myLoc . toDateTime ( str , QStringLiteral ( " " ) ) ; } if ( date . isValid ( ) ) { date . setTimeSpec ( Qt :: UTC ) ; return date ; } const QStringList months { " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " } ; const int year = str . midRef ( 26 , 4 ) . toInt ( ) ; const int month = months . indexOf ( str . mid ( 4 , 3 ) ) + 1 ; const int day = str . midRef ( 8 , 2 ) . toInt ( ) ; const qreal decay = str . midRef ( 20 , 5 ) . toDouble ( ) / 100 ; const QTime time = QTime :: fromString ( str . mid ( 11 , 8 ) , QStringLiteral ( " " ) ) ; date . setDate ( QDate ( year , month , day ) ) ; date . setTime ( time ) ; date . setOffsetFromUtc ( qFloor ( 3600 * decay ) ) ; } return date ; }
Convert a danbooru-like date (Sat May 14 17:38:04 -0400 2011) to a valid QDateTime.
Convert a danbooru - like date ( Sat May 14 17 : 38 : 04 - 0400 2011 ) to a valid QDateTime .