c++中时间戳与标准时间间的相互转换

第一部分  源自:https://blog.csdn.net/wangqing_12345/article/details/52092728

1,标准时间准换成时间戳

int standard_to_stamp(char *str_time) 

        struct tm stm; 
        int iY, iM, iD, iH, iMin, iS; 

        memset(&stm,0,sizeof(stm)); 
        iY = atoi(str_time); 
        iM = atoi(str_time+5); 
        iD = atoi(str_time+8); 
        iH = atoi(str_time+11); 
        iMin = atoi(str_time+14); 
        iS = atoi(str_time+17); 

        stm.tm_year=iY-1900; 
        stm.tm_mon=iM-1; 
        stm.tm_mday=iD; 
        stm.tm_hour=iH; 
        stm.tm_min=iMin; 
        stm.tm_sec=iS; 

        /*printf("%d-%0d-%0d %0d:%0d:%0d\n", iY, iM, iD, iH, iMin, iS);*/   //标准时间格式例如:2016:08:02 12:12:30
        return (int)mktime(&stm); 


2,时间戳转换成标准时间

typedef struct times
{
        int Year;
        int Mon;
        int Day;
        int Hour;
        int Min;
        int Second;
}Times;

Times stamp_to_standard(int stampTime)
{
        time_t tick = (time_t)stampTime;
        struct tm tm;
        char s[100];
        Times standard;

        //tick = time(NULL);
        tm = *localtime(&tick);
        strftime(s, sizeof(s), "%Y-%m-%d %H:%M:%S", &tm);
        printf("%d: %s\n", (int)tick, s);


        standard.Year = atoi(s);
        standard.Mon = atoi(s+5);
        standard.Day = atoi(s+8);
        standard.Hour = atoi(s+11);
        standard.Min = atoi(s+14);
        standard.Second = atoi(s+17);
    
        return standard;
}

3,如何获取系统标准时间

time_t rawtime ;

struct tm * timeinfo;

time ( &rawtime );

timeinfo = localtime ( &rawtime );

其中:

int Year = timeinfo->tm_year+1900;

int Mon = timeinfo->tm_mon+1;

其余日,时,分,秒都不变。

 

4,如何获取系统当前时间戳

 time_t now;                                                                                                                     
 int unixTime = (int)time(&now);

 

第二部分、个人应用

#include<iostream>
#include<string>
#include<vector>
#include<time.h>
/*
    功能:时间比较
    描述:比较时间戳
    返回:bool true,false
*/
inline bool CompareStampTime(long stampTimeStart, long stampTimeEnd)
{
    if (stampTimeStart > stampTimeEnd)
        return false;
    return true;
};


/*
    功能:时间累计器
    描述:size,时间间隔  type:时间类型0:分钟级别,1:小时级别,2:天级别
    返回:long,累加后的时候戳
*/
inline long addTime(long  strTime, int size, int type)
{
    switch (type)
    {
    case 0://分钟
        strTime += size * 60;
        break;
    case 1://小时
        strTime += size * 60 * 60;
        break;
    case 2://天
        strTime += size * 24 * 60 * 60;
        break;
    default:
        break;
    }
    return  strTime;
};

/*
    功能:标准时间转时间戳
    描述:传参为标准时间,如:2021-02-03 10:10:00
    返回:int类型的时候戳
*/
int standard_to_stamp(const char *str_time)
{
    struct tm stm;
    int iY, iM, iD, iH, iMin, iS;

    memset(&stm, 0, sizeof(stm));
    iY = atoi(str_time);
    iM = atoi(str_time + 5);
    iD = atoi(str_time + 8);
    iH = atoi(str_time + 11);
    iMin = atoi(str_time + 14);
    iS = atoi(str_time + 17);

    stm.tm_year = iY - 1900;
    stm.tm_mon = iM - 1;
    stm.tm_mday = iD;
    stm.tm_hour = iH;
    stm.tm_min = iMin;
    stm.tm_sec = iS;

    return (int)mktime(&stm);
}


/*
    功能:时间戳转换成标准时间
    描述:
*/
std::string stamp_to_standard(int stampTime)
{
    time_t tick = (time_t)stampTime;
    struct tm tm;
    char s[100];
    tm = *localtime(&tick);
    strftime(s, sizeof(s), "%Y-%m-%d %H:%M:%S", &tm);
    return  s;
};
 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值