a01设置时间

/**
*
* @Author:         Administrator
* @ProjectName:    PDL300G-A$
* @Package:        $PACKAGE_NAME$
* @ClassName:      TimeMethod$
* @Description:
* @CreateDate:     2022/1/13$ 14:15$
* @UpdateUser:
* @UpdateDate:     2022/1/13$ 14:15$
* @UpdateRemark:
* @Version:        1.0.0.0
*
**/
//
// Created by Administrator on 2022/1/13.
//

#include "TimeMethod.h"

/*****************************************宏定义******************************************/
/*****************************************************************************************/

/*****************************************变量申明*****************************************/
/*****************************************************************************************/

/*****************************************函数定义*****************************************/
/*****************************************************************************************/


/**
 * @date 2022年1月10日13:47:52
 * @def 格林威治时间转北京时间,传入data指针,转换时间后将时间放入data数组中
 * @param buf 转换之后的时间:年:月:日:时:分:秒:毫秒
 * @param len
 * @param data
 */
void GMTToUTCBuffer(void *buf,unsigned char len,void *data)
{
    int days = 0;
    if (len<16){return;}
    unsigned char *p=( unsigned char*)data;
    unsigned long long timestamp=*(unsigned long long *)buf;
    int timett=*(int *)(buf+8);

    time_t tmp;
    struct tm *timetmp;
    timestamp=timestamp/1000;
    tmp=(time_t)timestamp;
    timetmp=gmtime(&tmp);
    timetmp->tm_year-=100;
    timetmp->tm_mon+=1;
    if (timetmp->tm_mon == 1 || timetmp->tm_mon == 3 || timetmp->tm_mon == 5 || timetmp->tm_mon == 7 || timetmp->tm_mon == 8 || timetmp->tm_mon == 10 || timetmp->tm_mon == 12)
    {
        days = 31;
    }
    else if (timetmp->tm_mon == 4 || timetmp->tm_mon == 6 || timetmp->tm_mon == 9 || timetmp->tm_mon == 11)
    {
        days = 30;
    }
    else if (timetmp->tm_mon == 2)
    {
        if ((timetmp->tm_year % 400 == 0) || ((timetmp->tm_year % 4 == 0) && (timetmp->tm_year % 100 != 0))) /* 判断平年还是闰年 */
        {
            days = 29;
        }
        else
        {
            days = 28;
        }
    }
    timetmp->tm_min += (timett%60);
    if(timetmp->tm_min>=60){
        timetmp->tm_hour++;
        timetmp->tm_min -=60;
    }
    timetmp->tm_hour += (timett/60);                 // 格林威治时间+时区
    if (timetmp->tm_hour >= 24)            // 跨天
    {
        timetmp->tm_hour -= 24;
        timetmp->tm_mday++;
        if (timetmp->tm_mday > days)        // 跨月
        {
            timetmp->tm_mday = 1;
            timetmp->tm_mon++;
            if (timetmp->tm_mon > 12)    // 跨年
            {
                timetmp->tm_year++;
            }
        }
    }
    p[0]=timetmp->tm_year;
    p[1]=timetmp->tm_mon;
    p[2]=timetmp->tm_mday;
    p[3]=timetmp->tm_hour;
    p[4]=timetmp->tm_min;
    p[5]=timetmp->tm_sec;
    p[6]=(*(unsigned long long *)data)%1000;
    p[7]=(((*(unsigned long long *)data)%1000)>>8);
    //printf("-----------------> Year:%d Mon:%d Day:%d Hour:%d Min:%d Sec:%d MSec:%lld\n",p[0],p[1],p[2],p[3],p[4],p[5],((*(unsigned long long *)(data+12))%1000));
}

/**
 * @date 2022年1月10日13:47:52
 * @def 字符串转换timeval格式时间
 * @param buf 转换之后的时间:年:月:日:时:分:秒
 * @param len
 * @return
 */
struct timeval* BufferTOTimeval(void *buf,unsigned char len)
{
    static struct timeval tv={0};
    unsigned long long timestamp=*(unsigned long long *)buf;
    if (len<16){return NULL;}
    tv.tv_sec=timestamp/1000;
    tv.tv_usec=((*(unsigned long long *)buf)%1000)*1000;
    return &tv;
}

/**
 * @date 2022年2月7日11:41:47
 * @def 字符串转换timezone时区
 * @param buf
 * @param len
 * @return
 */
struct timezone* BufferTOTimezone(void *buf,unsigned char len)
{
    static struct timezone tz={0};
    if (len<16){return NULL;}
    tz.tz_minuteswest=*(int *)(buf+8);
    return &tz;
}
/**
 * @date 2022年1月10日19:12:04
 * @def 设置系统时间
 * @param time
 * @param tz
 */
void SetSystemTimeTimeval(struct timeval *time,struct timezone *tz)
{
    if (NULL!=time&&NULL!=tz)
    {
        //timezone不支持
        time->tv_sec+=tz->tz_minuteswest*60;
        settimeofday(time, NULL);
    }
}

/**
 * @date 2022年1月13日10:28:38
 * @def 设置系统时间
 * @param buf
 * @param len
 */
void SetSystemTimeBuffer(void * buf,unsigned char len)
{
    struct tm tptr={0};
    struct timeval tv={0};
    unsigned char *p=( unsigned char*)buf;
    if (len!=6){return;}
    tptr.tm_year =p[0]+100;     //传入值为2000年之后的年份,如2021年,传入值为21;struct tm时间结构体中/* 年份,其值等于实际年份减去1900 */
    if (p[1]>=0x01) {
        tptr.tm_mon = p[1] - 1;        /* 月份(从一月开始,0代表一月) - 取值区间为[0,11] */
    }
    tptr.tm_mday = p[2];
    tptr.tm_hour = p[3];
    tptr.tm_min = p[4];
    tptr.tm_sec = p[5];
    tv.tv_sec = mktime(&tptr);
    tv.tv_usec = 0;
    settimeofday(&tv, NULL);
}

/**
 * @brief  获取当前时间ms
 * @note   
 * @retval
 */
long long getCurrentTimeMs() {
    struct timeval tv = {0};
    long long time = 0;
    gettimeofday(&tv,NULL);
    time=(long long)tv.tv_sec * 1000 + (long long)tv.tv_usec / 1000;
    return time;
}

/**
 * @brief 获取当前时间us
 *
 * @return long long
 */
long long getCurrentTimeUs()
{
    struct timeval tv = {0};
    long long time = 0;
    gettimeofday(&tv,NULL);
    time=(long long)tv.tv_sec * 1000000 + (long long)tv.tv_usec;
    return time;
}


date --utc

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值