QT之日期工具类——DateUtils

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;
}


  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值