时间及时区函数

1.获取utc时间戳及当前时区差格林威治多少分钟
int gettimeofday(struct timeval*tv, struct timezone *tz);
多用于测试代码运行时长

2.获取utc时间
time_t time_stamp;
time_stamp = time(NULL);//utc时间戳
struct tm *sys_time;
sys_time = gmtime(&time_stamp);//格林威治时间,utc时间


3.获取本地时间
time_t time_stamp;
truct tm *local_time;
time_stamp = time(NULL);//utc时间戳
local_time = localtime(&time_stamp);


4.qt获取本地时间localtime
    QDateTime time = QDateTime::currentDateTime();//
    QString str = time.toString("yyyy-MM-dd hh:mm:ss");
    printf("now time is %s\n", str.toLatin1().data());
5.qt获取utc时间
QDateTime::currentDateTimeUtc()
	
	
	
	
	
#include 
#include 

int main(void) {
    time_t time_stamp;
    struct tm *sys_time;
    struct tm *local_time;

    time_stamp= time(NULL);
    printf("all sec is %ld\n", time_stamp);
    sys_time = gmtime(&time_stamp);
    printf("%04d-%02d-%02d %02d:%02d:%02d\n", sys_time->tm_year + 1900, sys_time->tm_mon + 1, sys_time->tm_mday, sys_time->tm_hour, sys_time->tm_min, sys_time->tm_sec);

    local_time = localtime(&time_stamp);
    printf("%04d-%02d-%02d %02d:%02d:%02d\n", local_time->tm_year + 1900, local_time->tm_mon + 1, local_time->tm_mday, local_time->tm_hour, local_time->tm_min, local_time->tm_sec);

    return 0;
}
	

时间戳与字符串格式相互转换

#include 
#include 
#include 

//将字符串转成时间戳
long metis_strptime(char *str_time)
{
  struct tm stm;  
  strptime(str_time, "%Y-%m-%d %H:%M:%S",&stm);
  long t = mktime(&stm);
  return t;
}

//将时间戳转成字符串
void metis_strftime(time_t t, char *pcTime)
{
  struct tm *tm_t;
  tm_t = localtime(&t);
  strftime(pcTime,128,"%F %T",tm_t);
}

int main(void)
{
	struct tm stm;
	long t;
	char str_time[]="2019-11-08 13:37:19";

	strptime(str_time, "%Y-%m-%d %H:%M:%S", &stm);
	t = metis_strptime(str_time);
	printf("%d, %s\n", t, str_time);

	bzero(str_time, sizeof(str_time));
	metis_strftime(t, str_time);
	printf("%d, %s\n", t, str_time);

	return 0;
}

 

你可能感兴趣的:(总结)