#ifndef UTILITIES_H
#define UTILITIES_H

#include <QString>
#include <QDateTime>

inline bool parseDateTime(const QString& str, const QString& format, QDateTime& dateTime)
{
    dateTime = QDateTime::fromString(str.trimmed(), format);
    return dateTime.isValid();
}

inline bool parseTime(const QString& str, const QString& format, QTime& time)
{
    time = QTime::fromString(str.trimmed(), format);
    return time.isValid();
}

inline bool parseDate(const QString& str, const QString& format, QDate& date)
{
    date = QDate::fromString(str.trimmed(), format);
    return date.isValid();
}

inline bool parseDouble(const QString& str, double& value)
{
    bool ok;
    value = str.trimmed().toDouble(&ok);
    return ok;
}

inline bool parseInt(const QString& str, int& value)
{
    bool ok;
    value = str.trimmed().toInt(&ok);
    return ok;
}

inline qint64 M1_diff(const QDateTime& first, const QDateTime& current)
{
    return first.secsTo(current) / 60;
}

inline qint64 M5_diff(const QDateTime& first, const QDateTime& current)
{
    return M1_diff(first, current) / 5;
}

inline qint64 M15_diff(const QDateTime& first, const QDateTime& current)
{
    return M1_diff(first, current) / 15;
}

inline qint64 M30_diff(const QDateTime& first, const QDateTime& current)
{
    return M1_diff(first, current) / 30;
}

inline qint64 H1_diff(const QDateTime& first, const QDateTime& current)
{
    return M1_diff(first, current) / 60;
}

inline qint64 H4_diff(const QDateTime& first, const QDateTime& current)
{
    return H1_diff(first, current) / 4;
}

inline qint64 D1_diff(const QDateTime& first, const QDateTime& current)
{
    return H1_diff(first, current) / 24;
}

inline qint64 MN_diff(const QDateTime& first, const QDateTime& current)
{
    int yearDiff = current.date().year() - first.date().year();
    int monthDiff = current.date().month() - first.date().month();
    return yearDiff * 12 + monthDiff;
}

#endif // UTILITIES_H
