mktime有时会返回-1使用boost库没有问题

linux获得时间戳

#include <iostream>
#include <boost/date_time/posix_time/posix_time.hpp>
long long utc8_to_stamp(int date, float time)
{
    struct tm stm;
    int itime = (time);
    int iY= date/10000,iM=(date-iY*10000)/100,iD=date%100,iH=itime/10000,iMin=(itime-iH*10000)/100,iS=itime%100;
  
    printf("%d , %d , %d, %d, %d,%d\n", iY, iM,iD,iH,iMin,iS);
    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("%lld\n",(mktime(&stm)*1ll) );
    return (long long)(mktime(&stm));
}
void getDate(long long ts, char* str)// time to local time
{
    time_t t;
    struct tm *p;
    char s[100];
 
    t = ts ;//+ 28800;
    uint8_t  nn,yy,rr,ss,ff,mm;
    p = localtime( &t );
 
    strftime(s, sizeof(s), "%Y%m%d,%H%M%S", p);
    strcpy(str, s);
}
//1691188920
//1691102744
long long utc8_str_to_stamp(char *date, char* time)
{
    struct tm stm;
    int iY,iM,iD,iH,iMin,iS;
    memset(&stm,0,sizeof(stm));

    char buf[5];

    strncpy(buf, date, 4);
    buf[4] = '\0';
    iY = atoi(buf);

    strncpy(buf, date + 4, 2);
    buf[2] = '\0';
    iM = atoi(buf);

    strncpy(buf, date + 6, 2);

    iD = atoi(buf);

    buf[0]=time[0];
    buf[1]=time[1];
    iH = atoi(buf);
    buf[0]=time[2];
    buf[1]=time[3];
    iMin = atoi(buf);
    buf[0]=time[4];
    buf[1]=time[5];
    iS = atoi(buf);
   // printf("%d , %d , %d, %d, %d,%d\n", iY, iM,iD,iH,iMin,iS);
    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("%lld\n",(mktime(&stm)*1ll) );
    return (long long)(mktime(&stm));
}
int main() {

    int year = 2023;
    int month = 8;
    int day = 17;
    int hour = 12;
    int minute = 30;
    int second = 0;

    boost::posix_time::ptime datetime(boost::gregorian::date(year, month, day),
                                      boost::posix_time::time_duration(hour, minute, second));


    boost::posix_time::ptime epoch(boost::gregorian::date(1970, 1, 1));
    boost::posix_time::time_duration timeSinceEpoch = datetime - epoch;
    long timestamp = timeSinceEpoch.total_seconds();

    // ŽòӡʱŒäŽÁ
    std::cout << "boost stamp: " << timestamp << std::endl;


long long llt = utc8_str_to_stamp("20160114","071903.00");
llt = utc8_to_stamp(20021105,122144.00);
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值