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;
}