结合QDateTime,跨平台下日期和时间的获取

本文介绍如何使用QDateTime类在不同平台上获取当前的日期和时间,聚焦于儒略日的计算和应用。

儒略日:

static inline int64_t floordiv(int64_t a, int b)
{
    return (a - (a < 0 ? b - 1 : 0)) / b;
}

static inline int floordiv(int a, int b)
{
    return (a - (a < 0 ? b - 1 : 0)) / b;
}

// 当前的日期对应的儒略日
static inline int64_t julianDayFromDate(int year, int month, int day)
{
    // Adjust for no year 0
    if (year < 0)
    {
        ++year;
    }

    /*
     * Math from The Calendar FAQ at http://www.tondering.dk/claus/cal/julperiod.php
     * This formula is correct for all julian days, when using mathematical integer
     * division (round to negative infinity), not c++11 integer division (round to zero)
     */
    int    a = floordiv(14 - month, 12);
    int64_t y = (int64_t)year + 4800 - a;
    int    m = month + 12 * a - 3;
    return day + floordiv(153 * m + 2, 5) + 365 * y + floordiv(y, 4) - floordiv(y, 100) + floordiv(y, 400) - 32045;
}

注:Qt使用qint64,C++使用int64_t
  • 获取当前的日期和时间
enum {
    SECS_PER_DAY = 86400,		// 一天的秒数
    MSECS_PER_DAY = 86400000,	// 一天的毫秒数
    SECS_PER_HOUR = 3600,		// 一小时的秒数
    MSECS_PER_HOUR = 3600000,	// 一小时的毫秒数
    SECS_PER_MIN = 60,			// 一分钟的秒数
    MSECS_PER_MIN = 60000,		// 一分钟的毫秒数
    TIME_T_MAX = 2145916799,  	// int maximum 2037-12-31T23:59:59 UTC
    JULIAN_DAY_FOR_EPOCH = 2440588 // result of julianDayFromDate(1970, 1, 1)
};

// 当前的时间对应的毫秒数
static inline unsigned int msecsFromDecomposed(int hour, int minute, int sec, int msec = 0)
{
    return MSECS_PER_HOUR * hour + MSECS_PER_MIN * minute + 1000 * sec + msec;
}

Q_OS_WIN:
#include <Windows.h>

// 获取日期、时间
SYSTEMTIME st;
memset(&st, 0, sizeof(SYSTEMTIME));
GetLocalTime(&st);

typedef struct _SYSTEMTIME {
    WORD wYear;
    WORD wMonth;
    WORD wDayOfWeek;
    WORD wDay;
    WORD wHour;
    WORD wMinute;
    WORD wSecond;
    WORD wMilliseconds;
} SYSTEMTIME, *PSYSTEMTIME, *LPSYSTEMTIME;

GetLocalTime:获取本地时间
GetSystemTime:获取通用协调时(UTC, Universal Time Coordinated)

// 当前UTC时间距离1970.1.1过去的毫秒数
int64_t currentMSecsSinceEpoch()
{
    SYSTEMTIME st;
    memset(&st, 0, sizeof(SYSTEMTIME));
    GetSystemTime(&st);

    return msecsFromDecomposed(st.wHour, st.wMinute, st.wSecond, st.wMilliseconds) +
        int64_t(julianDayFromDate(st.wYear, st.wMonth, st.wDay)
            - julianDayFromDate(1970, 1, 1)) * int64_t(86400000);
}

// 当前UTC时间距离1970.1.1过去的秒数
int64_t currentSecsSinceEpoch()
{
    SYSTEMTIME st;
    memset(&st, 0, sizeof(SYSTEMTIME));
    GetSystemTime(&st);

    return st.wHour * SECS_PER_HOUR + st.wMinute * SECS_PER_MIN + st.wSecond +
        int64_t(julianDayFromDate(st.wYear, st.wMonth, st.wDay)
            - julianDayFromDate(1970, 1, 1)) * int64_t(86400);
}

Q_OS_UNIX:
// 当前UTC时间距离1970.1.1过去的毫秒数
int64_t currentMSecsSinceEpoch()
{
    // posix compliant system
    // we have milliseconds
    struct timeval tv;
    gettimeofday(&tv, 0);
    return int64_t(tv.tv_sec) * int64_t(1000) + tv.tv_usec / 1000;
}

// 当前UTC时间距离1970.1.1过去的秒数
int64_t currentSecsSinceEpoch()
{
    struct timeval tv;
    gettimeofday(&tv, 0);
    return int64_t(tv.tv_sec);
}

struct timeval {
	_kernel_time_t 		tv_sec;	 /* seconds */
	_kernel_suseconds_t tv_usec; /* microseconds */
};

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值