linux下獲取當(dāng)前時(shí)間的相關(guān)函數(shù)
1.可以獲取當(dāng)前時(shí)間的s數(shù)
結(jié)構(gòu)體: time_t
函數(shù)
time_t time(time_t * timer)
2.可以獲取到當(dāng)前時(shí)間的微秒數(shù)
結(jié)構(gòu)體:struct timeval
struct timeval {
time_t tv_sec; // seconds
long tv_usec; // microseconds
};函數(shù):
int gettimeofday(struct timeval *tv, struct timezone *tz);
3.可以獲取到當(dāng)前時(shí)間的納秒數(shù)
結(jié)構(gòu)體:struct timespec
struct timespec {
time_t tv_sec; // seconds
long tv_nsec; // and nanoseconds
};函數(shù):
int clock_gettime(clockid_t, struct timespec *)獲取特定時(shí)鐘的時(shí)間:
4.對(duì)獲取到的時(shí)間做特定的格式化
結(jié)構(gòu)體:
struct tm {
int tm_sec; // Seconds (0-60)
int tm_min; // Minutes (0-59)
int tm_hour; // Hours (0-23)
int tm_mday; // Day of the month (1-31)
int tm_mon; // Month (0-11)
int tm_year; // Year - 1900;從1900年算起,至今的年份
int tm_wday; // Day of the week (0-6, Sunday = 0)
int tm_yday; // Day in the year (0-365, 1 Jan = 0)
int tm_isdst; // Daylight saving time;干啥用的???
};轉(zhuǎn)換函數(shù):
struct tm* localtime_r(const time_t* timer, struct tm* result );//線程安全
格式化函數(shù):
size_t strftime (char* ptr, size_t maxsize, const char* format, const struct tm* timeptr );
5.相關(guān)測(cè)試代碼
#include <stdio.h>
#include <stdlib.h>
#include <time.h> //time()
#include <string.h>
#include <unistd.h>
#include <stdint.h> //uint64_t
?
#include <sys/time.h> //gettimeofday
void get_time();
void get_time1();
static uint64_t getNowTime();
int main()
{
?
// time_t time(time_t * timer) 返回TC1970-1-1 0:0:0開始到現(xiàn)在的秒數(shù)
// 與相關(guān)函數(shù):localtime、gmtime、asctime、ctime,結(jié)合可以獲得當(dāng)前系統(tǒng)時(shí)間或是標(biāo)準(zhǔn)時(shí)間。
// struct tm* gmtime(const time_t *timep); 轉(zhuǎn)換為沒有經(jīng)過時(shí)區(qū)轉(zhuǎn)換的UTC時(shí)間,是一個(gè)struct tm結(jié)構(gòu)指針
// stuct tm* localtime(const time_t *timep); gmtime類似,但是它是經(jīng)過時(shí)區(qū)轉(zhuǎn)換的時(shí)間。
// char *asctime(const struct tm* timeptr); 轉(zhuǎn)換為真實(shí)世界的時(shí)間,以字符串的形式顯示
// char *ctime(const time_t *timep); 轉(zhuǎn)換為真是世界的時(shí)間,以字符串顯示
?
// int gettimeofday(struct timeval *tv, struct timezone *tz);
// 返回當(dāng)前距離1970年的秒數(shù)和微妙數(shù),后面的tz是時(shí)區(qū),一般不用
// time_t mktime(struct tm* timeptr); 轉(zhuǎn)換為從1970年至今的秒數(shù)
?
//size_t strftime (char* ptr, size_t maxsize, const char* format, const struct tm* timeptr );
// 可以通過該函數(shù),對(duì)時(shí)間進(jìn)行特定格式的字符串構(gòu)造,進(jìn)行時(shí)間格式的設(shè)置
//所以,獲取時(shí)間一般有兩個(gè)函數(shù)time() 和gettimeofday()
?
//time()函數(shù)的測(cè)試 兩種使用方法
time_t time0, time1;
time0 = time(NULL);
time(&time1); //獲取當(dāng)前時(shí)間,秒數(shù)
printf("time() test is %ld = %ld \n", time0, time1);
printf("ctime() :%s", ctime(&time1)); //直接字符串打印 帶換行的
?
//先用gmtime,沒有經(jīng)過時(shí)區(qū)轉(zhuǎn)換的時(shí)間 只有這里才不會(huì)有時(shí)區(qū)的轉(zhuǎn)換 差8小時(shí)
struct tm* g_time = gmtime(&time0);
printf("asctime(gmtime()) :%s", asctime(g_time)); //轉(zhuǎn)換為字符串進(jìn)行打印
//用localtime 轉(zhuǎn)換為經(jīng)過時(shí)區(qū)轉(zhuǎn)換的時(shí)間
struct tm* l_time = localtime(&time0);
printf("asctime(localtime()) :%s", asctime(l_time)); //轉(zhuǎn)換為字符串進(jìn)行打印
?
printf("mktime(g_time) to s :[%ld] \n", mktime(g_time)); //這里默認(rèn)帶了時(shí)區(qū)的轉(zhuǎn)換
printf("mktime(l_time) to s :[%ld] \n", mktime(l_time));
?
char tmpbuf[128];
struct tm* newtime=localtime(&time1);
//測(cè)試相關(guān)的格式打印
strftime( tmpbuf, 128, "Today is %A, day %d of %B in the year %Y.\n", newtime);
printf("%s \n", tmpbuf);
?
//其他兩個(gè)結(jié)構(gòu)體
get_time();
get_time1();
printf("get ms time: %lu \n", getNowTime());
return 0;
}
?
/******************
秒和納秒
struct timespec {
time_t tv_sec; // seconds
long tv_nsec; // and nanoseconds
};
int clock_gettime(clockid_t, struct timespec *)獲取特定時(shí)鐘的時(shí)間:
CLOCK_REALTIME 統(tǒng)當(dāng)前時(shí)間,從1970年1.1日算起
CLOCK_MONOTONIC 系統(tǒng)的啟動(dòng)時(shí)間,不能被設(shè)置
CLOCK_PROCESS_CPUTIME_ID 本進(jìn)程運(yùn)行時(shí)間
CLOCK_THREAD_CPUTIME_ID 本線程運(yùn)行時(shí)間
相關(guān)函數(shù)
//把獲得的時(shí)間秒數(shù)轉(zhuǎn)換為struct tm 類型
struct tm *localtime(const time_t *clock); //線程不安全
struct tm* localtime_r(const time_t* timer, struct tm* result );//線程安全
//格式化時(shí)間字符串
size_t strftime (char* ptr, size_t maxsize, const char* format, const struct tm* timeptr );
?
秒和微妙
struct timeval {
time_t tv_sec; // seconds
long tv_usec; // microseconds
};
int gettimeofday(struct timeval *tv, struct timezone *tz)獲取系統(tǒng)的時(shí)間
******************/
//1:clock_gettime 操作timespec結(jié)構(gòu),返回秒+納秒結(jié)構(gòu)的時(shí)間,可以定義時(shí)鐘類型
void get_time()
{
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
printf("clock_gettime : tv_sec=%ld, tv_nsec=%ld\n",ts.tv_sec, ts.tv_nsec);
struct tm t;
char date_time[64];
strftime(date_time, sizeof(date_time), "%Y-%m-%d %H:%M:%S", localtime_r(&ts.tv_sec, &t));//只到秒上
printf("clock_gettime :date_time=%s, tv_nsec=%ld\n", date_time, ts.tv_nsec);
}
// 2:gettimeofday 獲取系統(tǒng)時(shí)間 這里微妙和納秒可以對(duì)結(jié)構(gòu)體操作進(jìn)行轉(zhuǎn)換
// struct timeval now;
// struct timespec outtime;
// gettimeofday(&now, NULL);
// outtime.tv_sec = now.tv_sec; //s
// outtime.tv_nsec = now.tv_usec * 1000; //微妙轉(zhuǎn)為納秒
void get_time1()
{
struct timeval us;
gettimeofday(&us,NULL);
printf("gettimeofday: tv_sec=%ld, tv_usec=%ld\n", us.tv_sec, us.tv_usec);
struct tm t;
char date_time[64];
strftime(date_time, sizeof(date_time), "%Y-%m-%d %H:%M:%S", localtime_r(&us.tv_sec, &t));
printf("gettimeofday: date_time=%s, tv_usec=%ld\n", date_time, us.tv_usec);
}
//獲取當(dāng)前時(shí)間 ms單位
static uint64_t getNowTime()
{
struct timeval tval;
uint64_t nowTime;
?
gettimeofday(&tval, NULL);
?
nowTime = tval.tv_sec * 1000L + tval.tv_usec / 1000L;
return nowTime;
}
/******************************************
struct tm {
int tm_sec; // Seconds (0-60)
int tm_min; // Minutes (0-59)
int tm_hour; // Hours (0-23)
int tm_mday; // Day of the month (1-31)
int tm_mon; // Month (0-11)
int tm_year; // Year - 1900;從1900年算起,至今的年份
int tm_wday; // Day of the week (0-6, Sunday = 0)
int tm_yday; // Day in the year (0-365, 1 Jan = 0)
int tm_isdst; // Daylight saving time;干啥用的???
};
?
%a: 英文單詞中星期幾的縮寫版。如:星期三,表示為"Wed"。
? %A: 英文單詞中星期幾的完整版。如:星期三,表示為"Wednesday"。
? %b: 英文單詞中月份的縮寫版。如:11月,表示為"Nov"。
? %B: 英文單詞中月份的縮寫版。如:11月,表示為"November"。
? %c: 格式化的時(shí)間字符串。如:2018-11-28 10:13:40,表示為"Wed Nov 28 10:13:40 2018"。
? %F: 日期格式為yyyy-mm-dd,與%Y:%m:%d作用相同。如:2018-11-28。
? %X: 時(shí)間格式為hh:mm:ss。如:10:13:40。
? %j: 一年中的第幾天,范圍:001-366.
? %W: 一年中的第幾周,范圍:00-53.
? %Y: 日期中的年,如:2018。
? %m: 日期中的月,范圍:00-12。
? %d: 日期中的天,范圍:01-31。
? %H: 小時(shí),范圍:00-24。
? %M: 分鐘,范圍:00-59。
? %S: 秒,范圍:00-60。
******************************************/執(zhí)行結(jié)果:
time() test is 1623892563 = 1623892563
ctime() :Thu Jun 17 09:16:03 2021
asctime(gmtime()) :Thu Jun 17 01:16:03 2021
asctime(localtime()) :Thu Jun 17 09:16:03 2021
mktime(g_time) to s :[1623892563]
mktime(l_time) to s :[1623892563]
Today is Thursday, day 17 of June in the year 2021.
clock_gettime : tv_sec=1623892563, tv_nsec=702520168
clock_gettime :date_time=2021-06-17 09:16:03, tv_nsec=702520168
gettimeofday: tv_sec=1623892563, tv_usec=702558
gettimeofday: date_time=2021-06-17 09:16:03, tv_usec=702558
get ms time: 1623892563702
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
linux下統(tǒng)計(jì)appche站點(diǎn)IP訪問量的shell腳本
這篇文章主要介紹了linux下統(tǒng)計(jì)appche站點(diǎn)IP訪問量的幾種shell腳本以及執(zhí)行結(jié)果2014-06-06
apache tomcat 一個(gè)網(wǎng)站多域名的實(shí)現(xiàn)方法
因此處是進(jìn)行多域名設(shè)置,所以 Apache 與 tomcat的結(jié)合沒有詳述,此處只是設(shè)置多域名的方法2009-02-02
apache啟用gzip壓縮的實(shí)現(xiàn)方法
對(duì)于部署在Linux服務(wù)器上的PHP程序,在服務(wù)器支持的情況下,我們建議你開啟使用Gzip Web壓縮,以前腳本之家介紹了iis中的開啟方法,這篇文章主要介紹了linux中apache的開啟方法2013-06-06
配置Linux服務(wù)器SSH 安全訪問的四個(gè)小技巧
越來越多的站長(zhǎng),開始使用獨(dú)立主機(jī)(Dedicated Host)和 VPS。而為了節(jié)省成本或提高性能,不少人的獨(dú)機(jī)和 VPS,都是基于 unmanaged 的裸機(jī),一切都要自己 DIY。這時(shí)候,安全策略的實(shí)施,就猶為重要。2010-12-12
CentOS MySQL 5.7編譯安裝步驟詳細(xì)說明
這篇文章主要介紹了CentOS MySQL 5.7編譯安裝詳細(xì)介紹的相關(guān)資料,這里對(duì)安裝步驟進(jìn)行了詳細(xì)介紹,需要的朋友可以參考下2016-12-12

