タイムタイムゾーン関数
2144 ワード
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
", 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
", time_stamp);
sys_time = gmtime(&time_stamp);
printf("%04d-%02d-%02d %02d:%02d:%02d
", 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
", 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
", t, str_time);
bzero(str_time, sizeof(str_time));
metis_strftime(t, str_time);
printf("%d, %s
", t, str_time);
return 0;
}