最近搞視頻檢索,涉及到很多時間的計算。順便記錄下一些基本用法。
一、gmtime用法
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define PNULL NULL
int p_time(time_t t)
{
struct tm *timenow;
if (t == -1) {
printf("p_time input error. time = -1\n");
return;
}
timenow = gmtime(&(t));
if (timenow == PNULL) {
return;
}
printf("%4d.%02d.%02d-%02d:%02d:%02d\n", \
timenow->tm_year+1900,timenow->tm_mon+1,timenow->tm_mday,\
timenow->tm_hour,timenow->tm_min,timenow->tm_sec);
}
void main()
{
time_t test_time;
time(&test_time);
test_time = test_time + (8*60*60);
printf("time = %lu\n", test_time);
p_time(test_time);
}
運(yùn)行結(jié)果:
root@chenwr-pc:/home/workspace/test/tmp/FAT# date
Thu Mar 28 14:52:03 CST 2019
root@chenwr-pc:/home/workspace/test/tmp/FAT# gcc time.c -o run && ./run
time = 1553784724
2019.03.28-14:52:04
作用打印出當(dāng)前時間。
- gmtime()將參數(shù)test_time所指的time_t 結(jié)構(gòu)中的信息轉(zhuǎn)換成真實(shí)世界所使用的時間日期表示方法,然后將結(jié)果由結(jié)構(gòu)tm 返回。
- Coordinated Universal Time(UTC):協(xié)調(diào)世界時,又稱為世界標(biāo)準(zhǔn)時間,也就是大家所熟知的格林威治標(biāo)準(zhǔn)時間(Greenwich Mean Time,GMT)。比如,中國內(nèi)地的時間與UTC的時差為+8,也就是UTC+8。美國是UTC-5。
- time函數(shù)用來獲取機(jī)器時間(UTC)單位為秒數(shù),所有和北京時間差8個時區(qū),因此要加86060。最后通過gmtime來轉(zhuǎn)換。
二、time函數(shù)用法
time()函數(shù)的用途是返回一個值,即**格林尼治時間1970年1月1日00:00:00到當(dāng)前時刻的時長**,時長單位是秒。
#include <stdio.h>
#include <stdlib.h>
void main()
{
time_t t;
time(&t);
printf("time = %lu\n", t);
}
運(yùn)行結(jié)果:
time = 1555576546
tm結(jié)構(gòu)體
struct tm{
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
};
三、ctime用法
函數(shù)原型: char *ctime(long time)
函數(shù)功能: 得到日歷時間
函數(shù)返回: 返回字符串格式:星期,月,日,小時:分:秒,年
參數(shù)說明: time-該參數(shù)應(yīng)由函數(shù)time獲得
所屬文件: <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void main()
{
time_t t;
time(&t);
printf("date and time: %s\n", ctime(&t));
}
運(yùn)行結(jié)果:
date and time: Thu Apr 18 16:43:32 2019
四、mktime用法
將時間轉(zhuǎn)換為自1970年1月1日以來逝去時間的秒數(shù),發(fā)生錯誤時返回-1
函數(shù)原型:time_t mktime(struct tm * timeptr);
所屬文件 #include <time.h>
日期如何轉(zhuǎn)成秒數(shù)實(shí)例
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define PNULL NULL
typedef signed long INT32S;
#include <math.h>
#include <string.h>
int p_time(time_t t)
{
struct tm *timenow;
if (t == -1) {
printf("p_time input error. time = -1\n");
return;
}
timenow = gmtime(&(t));
if (timenow == PNULL) {
return;
}
printf("%4d.%02d.%02d-%02d:%02d:%02d\n", \
timenow->tm_year+1900,timenow->tm_mon+1,timenow->tm_mday,\
timenow->tm_hour,timenow->tm_min,timenow->tm_sec);
}
time_t date_to_sec(int year, int mon, int day, int hour, int min, int sec)
{
struct tm date ;
int time_zone = 8;
memset(&date, 0, sizeof(date));
date.tm_year = year-1900;
date.tm_mon = mon-1;
date.tm_mday = day;
date.tm_hour = hour+time_zone;
date.tm_min = min;
date.tm_sec = sec;
return mktime(&date);
}
void main()
{
int my_time;
my_time = date_to_sec(2019, 3, 20, 18, 10, 6);
printf("date sec; %d\n", my_time);
p_time(my_ime);
}
運(yùn)行結(jié)果:
date sec; 1553105406
2019.03.20-18:10:06
五、參考資料
這篇把常見的關(guān)于time的函數(shù)用法整理的很清晰
c語言中time函數(shù)的用法 - wangluojisuan的專欄 - CSDN博客 https://blog.csdn.net/wangluojisuan/article/details/7045592/
struct tm 和 time_t 時間和日期的使用方法(轉(zhuǎn)) - 教學(xué)博客 - 博客園 http://www.cnblogs.com/hhpjxbk/archive/2009/10/06/1578385.html