windows下时间转换和获取当前时间

time_t 转string  时间戳转标准时间

#include <time.h>

std::string time_to_string(time_t secs, const char *fmt)
{

secs = (secs >= 0 ? secs : 0);

 fmt = (fmt!=nullptr?fmt:"%Y-%m-%d %H:%M:%S");
 struct tm *ptr;
 char str[80] = {0};
 ptr=localtime(&secs);
 strftime(str, sizeof(str), fmt, ptr);
 return str;
}


string转time_t 标准时间转时间戳

time_t string_to_time_t(const std::string &time_string, const char *fmt)

 struct tm tm1; 
 time_t time1; 

fmt = (fmt != nullptr ? fmt : "%d-%d-%d %d:%d:%d");

 int i = sscanf(time_string.c_str(), fmt ,
  &(tm1.tm_year),  
  &(tm1.tm_mon),  
  &(tm1.tm_mday), 
  &(tm1.tm_hour), 
  &(tm1.tm_min), 
  &(tm1.tm_sec), 
  &(tm1.tm_wday), 
  &(tm1.tm_yday)); 

 tm1.tm_year -= 1900; 
 tm1.tm_mon --; 
 tm1.tm_isdst=-1; 
 time1 = mktime(&tm1); 

time1 = (time1 >= 0 ? time1 : 0);

 return time1; 
}


获取当前时间,单位秒

time_t current_time =  time(NULL);

获取当前时间,单位毫秒

time_t current_time = GetTickCount();

你可能感兴趣的:(时间转换,时间戳,格林尼治时间)