QT之日期工具类——DateUtils
前言
借鉴了java的相关类的思想,可以实现日期相关的计算方法。
源码
头文件
#ifndef DATEUTILS_H
#define DATEUTILS_H
#include <QString>
#include <QDateTime>
/*!
* \brief DateUtils 是一个用来操作时间日期的工具类
*/
class DateUtils
{
public:
DateUtils();
/*!
* \brief 获取当前时间字符串(hh:mm:ss.zzz)
* \return 时间字符串
*/
static QString getCurrTimeStr();
/*!
* \brief 获取当前日期字符串(yyyy-MM-dd hh:mm:ss)
* \return 日期字符串
*/
static QString getCurrDateTimeStr();
/*!
* \brief 按格式返回当前日期字符串(默认:yyyy-MM-dd hh:mm:ss)
* \param formatStr 格式字符串
* \return 日期字符串
*/
static QString getCurrDateTimeStr(QString formatStr = "yyyy-MM-dd hh:mm:ss");
/*!
* \brief 计算开始日期到现在过去的天数
* \param dateTime 开始日期
* \return 过去的天数
*/
static long pastDays(QDateTime dateTime);
/*!
* \brief 计算开始日期到现在过去的小时数
* \param dateTime 开始日期
* \return 过去的小时数
*/
static long pastHours(QDateTime dateTime);
/*!
* \brief 计算开始日期到现在过去的分钟数
* \param 开始日期
* \return 过去的分钟数
*/
static long pastMinutes(QDateTime dateTime);
/*!
* \brief 计算两个日期之间过去的天数
* \param before 前面的日期
* \param after 后面的日期
* \return 过去的天数
*/
static long twoDatePastDays(QDateTime before, QDateTime after);
/*!
* \brief 获取两个日期段的小时数交集(带小数)
* \param stime1 开始日期1
* \param etime1 结束日期1
* \param stime2 开始日期2
* \param etime2 结束日期2
* \return 小时数交集(带小数)
*/
static float getTwoDateRangeIntersection2Hours(QDateTime stime1, QDateTime etime1, QDateTime stime2, QDateTime etime2);
};
#endif // DATEUTILS_H
源文件
#include "dateutils.h"
#include <QDateTime>
#include <QList>
DateUtils::DateUtils()
{
}
QString DateUtils::getCurrTimeStr()
{
return QTime::currentTime().toString("hh:mm:ss.zzz");
}
QString DateUtils::getCurrDateTimeStr()
{
return QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
}
QString DateUtils::getCurrDateTimeStr(QString formatStr)
{
return QDateTime::currentDateTime().toString(formatStr);
}
long DateUtils::pastDays(QDateTime dateTime)
{
QDateTime now = QDateTime::currentDateTime();
long t = now.toMSecsSinceEpoch() - dateTime.toMSecsSinceEpoch();
return t / (24 * 60 * 60 * 1000);
}
long DateUtils::pastHours(QDateTime dateTime)
{
QDateTime now = QDateTime::currentDateTime();
long t = now.toMSecsSinceEpoch() - dateTime.toMSecsSinceEpoch();
return t / (60 * 60 * 1000);
}
long DateUtils::pastMinutes(QDateTime dateTime)
{
QDateTime now = QDateTime::currentDateTime();
long t = now.toMSecsSinceEpoch() - dateTime.toMSecsSinceEpoch();
return t / (60 * 1000);
}
long DateUtils::twoDatePastDays(QDateTime before, QDateTime after)
{
long t = after.toMSecsSinceEpoch() - before.toMSecsSinceEpoch();
return t / (1000 * 60 * 60 * 24);
}
float DateUtils::getTwoDateRangeIntersection2Hours(QDateTime stime1, QDateTime etime1, QDateTime stime2, QDateTime etime2)
{
float result = 0.0f;
long btlong = qMin(stime1.toMSecsSinceEpoch(), etime1.toMSecsSinceEpoch());// 开始时间
long otlong = qMax(stime1.toMSecsSinceEpoch(), etime1.toMSecsSinceEpoch());// 结束时间
long stlong = qMin(stime2.toMSecsSinceEpoch(), etime2.toMSecsSinceEpoch());// 开始时间
long edlong = qMax(stime2.toMSecsSinceEpoch(), etime2.toMSecsSinceEpoch());// 结束时间
if(btlong <= edlong && otlong >= stlong)
{
QList<long> list;
list << btlong << otlong << stlong << edlong;
qSort(list.begin(), list.end()); //从小到大排序,取第二、第三计算
float f = list.at(2) - list.at(1);
return f / (1000.0f * 60 * 60);
}
return result;
}