QDateTime的毫秒计算

#include <iostream>
#include <stdint.h>
#include "QDateTime"
#include "QDebug"

using namespace std;


typedef struct
{
    char *name; //
    volatile int8_t hour; //
    volatile int8_t min; //
} UTCTime; // 每小时保存一次

typedef struct
{
    volatile int8_t hour; //
    volatile uint8_t min; //
    volatile uint8_t sec; //
    volatile uint16_t msec; //
} Time; // 每小时保存一次
typedef struct
{
    volatile uint16_t year;
    volatile uint8_t month;
    volatile uint8_t day;
} Date; // 每小时保存一次
typedef struct
{
    union {
        struct {
            volatile uint16_t year;
            volatile uint8_t month;
            volatile uint8_t day;
        };
        Date date;
    };

    union {
        struct {
            volatile int8_t hour; // 时
            volatile int8_t min; // 分
            volatile uint8_t sec; // 秒
            volatile uint16_t msec; // 毫秒
        };
        Time time;
    };

    volatile uint32_t days; // days of year
    volatile uint8_t  weeks; // week of year
    volatile uint8_t  week; // week of month

    volatile uint8_t event; // 按位关闭一个通道,bit=1则关闭;
} Calendar, TickTime; // 每小时保存一次


const UTCTime utc_time[] = {
    {"Dateline",-12	,0	},
    {"Alaskan",-9	,0	},
    {"US Mountain",-7	,0	},
    {"Central America",-6	,0	},
    {"Canada Central",-6	,0	},
    {"US Eastern",-5	,0	},
    {"Atlantic",-4	,0	},
    {"Pacific SA",-4	,0	},
    {"Argentina",-3	,0	},
    {"Montevideo",-3	,0	},
    {"Mid-Atlantic",-2	,0	},
    {"Morocco",0	,0	},
    {"Greenwich",0	,0	},
    {"Romance",  1	,0	},
    {"W. Central Africa",  1	,0	},
    {"Middle East",  2	,0	},
    {"E. Europe",  2	,0	},
    {"Turkey",  2	,0	},
    {"Arabic",  3	,0	},
    {"E. Africa",  3	,0	},
    {"Azerbaijan",  4	,0	},
    {"Georgian",  4	,0	},
    {"Pakistan",  5	,0	},
    {"Sri Lanka",  5	,30	},
    {"Bangladesh",  6	,0	},
    {"SE Asia",  7	,0	},
    {"North Asia",  8	,0	},
    {"Taipei",  8	,0	},
    {"Tokyo",  9	,0	},
    {"AUS Central",  9	,30	},
    {"West Pacific", 10	,0	},
    {"Central Pacific", 11	,0	},

    {"Pacific (Mexico)",-8	,0	},
    {"Mountain (Mexico)",-7	,0	},
    {"Central",-6	,0	},
    {"SA Pacific",-5	,0	},
    {"Venezuela",-4	,-30	},
    {"Central Brazilian",-4	,0	},
    {"Newfoundland",-3	,-30	},
    {"SA Eastern",-3	,0	},
    {"Bahia",-3	,0	},
    {"Azores",-1	,0	},
    {"Coordinated",0	,0	},
    {"W. Europe",  1	,0	},
    {"Central European",  1	,0	},
    {"Namibia",  1	,0	},
    {"Egypt",  2	,0	},
    {"South Africa",  2	,0	},
    {"Jerusalem",  2	,0	},
    {"Kaliningrad",  3	,0	},
    {"Iran",  3	,30	},
    {"Russian",  4	,0	},
    {"Caucasus",  4	,0	},
    {"West Asia",  5	,0	},
    {"Nepal",  5	,45	},
    {"Ekaterinburg",  6	,0	},
    {"N. Central Asia",  7	,0	},
    {"Malay Peninsula",  8	,0	},
    {"Ulaanbaatar",  8	,0	},
    {"Korea",  9	,0	},
    {"E. Australia", 10	,0	},
    {"Tasmania", 10	,0	},
    {"Vladivostok", 11	,0	},
    {"Fiji", 12	,0	},

    {"Hawaiian",-10	,0	},
    {"Pacific",-8	,0	},
    {"Mountain",-7	,0	},
    {"Central (Mexico)",-6	,0	},
    {"Eastern",-5	,0	},
    {"Paraguay",-4	,0	},
    {"SA Western",-4	,0	},
    {"E. South America",-3	,0	},
    {"Greenland",-3	,0	},
    {"Cape Verde",-1	,0	},
    {"GMT",0	,0	},
    {"Central Europe",  1	,0	},
    {"Libya",  1	,0	},
    {"GTB",  2	,0	},
    {"Syria",  2	,0	},
    {"FLE",  2	,0	},
    {"Jordan",  3	,0	},
    {"Arab",  3	,0	},
    {"Arabian",  4	,0	},
    {"Mauritius",  4	,0	},
    {"Afghanistan",  4	,30	},
    {"India",  5	,30	},
    {"Central Asia",  6	,0	},
    {"Myanmar",  6	,30	},
    {"China",  8	,0	},
    {"W. Australia",  8	,0	},
    {"North Asia East",  9	,0	},
    {"Cen. Australia",  9	,30	},
    {"AUS Eastern", 10	,0	},
    {"Yakutsk", 10	,0	},
    {"New Zealand", 12	,0	},
    {"Magadan", 12	,0	},
    {"\0", 0	,0	}
};


//月份数据表
const uint8_t _week_table[12]={0,3,3,6,1,4,6,2,5,0,3,5}; //月修正数据表
const uint8_t _month_table[]={ 31,28,31,30,31,30,31,31,30,31,30,31 };
inline int isLeapYear(int year)
{
    return (( (year%4==0) && (year%100!=0) ) || (year%400==0) )? 1:0;
}

inline uint16_t RTC_GetDayOfYear( uint16_t year, uint8_t month, uint8_t day )
{
    uint64_t msecsSinceEpoch=0;
    int m;
    //int UTC = 8; // UTC 时差 8
    msecsSinceEpoch = 0;
    for( m=1; m<month; m++ )
    {
        msecsSinceEpoch += _month_table[ m-1 ];
        if( m == 2 && isLeapYear( year ) )
            msecsSinceEpoch += 1;
    }
    msecsSinceEpoch += day;
    return msecsSinceEpoch;

}

inline uint64_t GetMSecsSinceEpoch( uint16_t year, uint8_t month, uint8_t day,
                            uint8_t hour, uint8_t min, uint8_t sec, uint16_t msec,
                            Time utc )
{
    uint64_t msecsSinceEpoch=0;
    if( year<1970 || year>2199 )
        return 0;

    {
        int m;
        int y;
        float vv = 0;
        //int UTC = 8; // UTC 时差 8
        msecsSinceEpoch = 0;
        for( y=1970; y<year; y++ )
        {
            //            if( isLeapYear( y ) )
            //                cmm += 366;
            //            else
            //                cmm += 365;
            vv += 365.2422;
        }
        msecsSinceEpoch = vv;
        for( m=1; m<month; m++ )
        {
            msecsSinceEpoch += _month_table[ m-1 ];
            if( m == 2 && isLeapYear( year ) )
                msecsSinceEpoch += 1;
        }
        msecsSinceEpoch += day-1;
        msecsSinceEpoch = msecsSinceEpoch * 24 + hour - utc.hour;
        msecsSinceEpoch = msecsSinceEpoch * 60 + min - utc.min;
        msecsSinceEpoch = msecsSinceEpoch * 60 + sec ;
        msecsSinceEpoch = msecsSinceEpoch * 1000 + msec ;
    }

    return msecsSinceEpoch;
}

//获得现在是星期几
//功能描述:输入公历日期得到星期(只允许1901-2099年)
//输入参数:公历年月日
//返回值:星期号
inline uint8_t RTC_GetDayOfWeek( uint16_t year, uint8_t month, uint8_t day )
{
    uint16_t temp2;
    uint8_t yearH, yearL;

    yearH = year / 100;
    yearL = year % 100;

    // 如果为21世纪,年份数加100
    if ( yearH>19 )
        yearL += 100;

    // 所过闰年数只算1900年之后的
    temp2 = yearL + (yearL/4);
    temp2 = temp2 % 7;
    temp2 = temp2 + day + _week_table[ month-1 ];

    if ( yearL%4==0 && month<3 )
        temp2--;

    return (temp2%7);
}

inline uint8_t RTC_GetWeekOfYear( uint16_t year, uint8_t month, uint8_t day )
{
    return (RTC_GetDayOfYear( year, month, day )/7);
}

inline uint8_t RTC_GetWeekNumber( uint16_t year, uint8_t month, uint8_t day )
{
    uint16_t temp2;
    uint8_t yearH, yearL;

    yearH = year / 100;
    yearL = year % 100;

    // 如果为21世纪,年份数加100
    if ( yearH>19 )
        yearL += 100;

    // 所过闰年数只算1900年之后的
    temp2 = yearL + (yearL/4);
    temp2 = temp2 % 7;
    temp2 = temp2 + day + _week_table[ month-1 ];

    if ( yearL%4==0 && month<3 )
        temp2--;

    return (temp2%7);
}

//得到当前的时间
Calendar RTC_GetCalendar(uint64_t timecount, Time utc )
{
    uint16_t daycnt=0;
    uint32_t temp=0;
    uint16_t temp1=0;
    Calendar calendar;
    temp = timecount/86400000;   //得到天数(秒钟数对应的)
    if( daycnt != temp )//超过一天了
    {
        daycnt = temp;
        temp1 = 1970;	//从1970年开始
        while( temp>=365 )
        {
            if( isLeapYear(temp1) )//是闰年
            {
                if( temp >= 366 )
                    temp -= 366;//闰年的秒钟数
                else
                {
                    temp1++;
                    break;
                }
            }
            else
                temp-=365;	  //平年
            temp1++;
        }

        calendar.year = temp1;//得到年份
        temp1 = 0;
        while( temp >= 28 )//超过了一个月
        {
            if( temp1 == 1 && isLeapYear(calendar.year) )//当年是不是闰年/2月份
            {
                if( temp >= 29 )
                    temp -= 29;//闰年的秒钟数
                else
                    break;
            }
            else
            {
                if( temp >= _month_table[temp1] )
                    temp -= _month_table[temp1];//平年
                else
                    break;
            }
            temp1++;
        }
        calendar.month = temp1+1;	//得到月份
        calendar.day = temp+1;  	//得到日期
    }

    calendar.msec=timecount%1000; 	//毫秒
    timecount = timecount/1000  + ((utc.hour*60+utc.min)*60);
    temp=(timecount)%86400;    //得到秒钟数
    calendar.hour=temp/3600 ;     	//小时
    calendar.min=(temp%3600)/60 ; 	//分钟
    calendar.sec=(temp%3600)%60 ; 	//秒钟
    calendar.days = RTC_GetDayOfYear( calendar.year, calendar.month, calendar.day );//获取星期
    calendar.week = RTC_GetDayOfWeek( calendar.year, calendar.month, calendar.day );//获取星期
    calendar.weeks = calendar.days/7+1;//获取一年中第几周
    return calendar;
}


int main()
{
    cout << "Hello World!" << endl;

    //1970-01-01 00:00:00.000 UTC 0
    QDateTime dt( QDateTime::currentDateTime() );

    uint64_t msecsSinceEpoch=0;
    uint64_t cm = dt.toMSecsSinceEpoch();

    TickTime tickTime;
    tickTime.year = dt.date().year();
    tickTime.month = dt.date().month();
    tickTime.day = dt.date().day();
    tickTime.hour = dt.time().hour();
    tickTime.min = dt.time().minute();
    tickTime.sec = dt.time().second();
    tickTime.msec = dt.time().msec();

    tickTime.days = dt.date().dayOfYear();
    tickTime.week = dt.date().dayOfWeek();
    tickTime.weeks = dt.date().weekNumber();

    qDebug()<<"+++++++++++++++++++++++++";
    qDebug()<<dt.toMSecsSinceEpoch();
    qDebug()<<tickTime.year<<tickTime.month<<tickTime.day<<tickTime.weeks<<tickTime.week<<tickTime.days;
    qDebug()<<tickTime.hour<<tickTime.min<<tickTime.sec<<tickTime.msec;
    qDebug()<<"-------------------------";

    {
        Time utc = { 8, 0 };
        Calendar calendar = RTC_GetCalendar( cm, utc );
        msecsSinceEpoch = GetMSecsSinceEpoch( calendar.year, calendar.month, calendar.day,
                                             calendar.hour, calendar.min, calendar.sec, calendar.msec,
                                             utc );
        qDebug()<<msecsSinceEpoch;
        qDebug()<<calendar.year<<calendar.month<<calendar.day<<calendar.weeks<<calendar.week<<calendar.days;
        qDebug()<<calendar.hour<<calendar.min<<calendar.sec<<calendar.msec;
    }
    qDebug()<<"-------------------------";
    qDebug()<<cm<<msecsSinceEpoch<<(cm-msecsSinceEpoch);

    return 0;
}
代码中所计算出的毫秒主要是为了与 QDateTime::currentMSecsSinceEpoch() 保持一致。

Hello World!
+++++++++++++++++++++++++
1723444856359
2024 8 12 33 1 225
14 40 56 359
-------------------------
1723444856359
2024 8 12 33 1 225
14 40 56 359
-------------------------
1723444856359 1723444856359 0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值