亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

linux下獲取當前時間的相關(guān)函數(shù)

 更新時間:2023年09月07日 09:01:59   作者:yun6853992  
這篇文章主要介紹了linux下獲取當前時間的相關(guān)函數(shù),具有很好的參考價值,希望對大家有所幫助,

1.可以獲取當前時間的s數(shù)

結(jié)構(gòu)體: time_t

函數(shù)

time_t time(time_t * timer)

2.可以獲取到當前時間的微秒數(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.可以獲取到當前時間的納秒數(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 *)獲取特定時鐘的時間:

4.對獲取到的時間做特定的格式化

結(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)測試代碼

 #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é)合可以獲得當前系統(tǒng)時間或是標準時間。
     //  struct tm* gmtime(const time_t *timep);    轉(zhuǎn)換為沒有經(jīng)過時區(qū)轉(zhuǎn)換的UTC時間,是一個struct tm結(jié)構(gòu)指針
     //  stuct tm* localtime(const time_t *timep);  gmtime類似,但是它是經(jīng)過時區(qū)轉(zhuǎn)換的時間。
     //  char *asctime(const struct tm* timeptr);   轉(zhuǎn)換為真實世界的時間,以字符串的形式顯示
     //  char *ctime(const time_t *timep);          轉(zhuǎn)換為真是世界的時間,以字符串顯示
 ?
     //  int gettimeofday(struct timeval *tv, struct timezone *tz);
     //          返回當前距離1970年的秒數(shù)和微妙數(shù),后面的tz是時區(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ù),對時間進行特定格式的字符串構(gòu)造,進行時間格式的設(shè)置
 //所以,獲取時間一般有兩個函數(shù)time() 和gettimeofday()
 ?
     //time()函數(shù)的測試 兩種使用方法
     time_t time0, time1;
     time0 = time(NULL);
     time(&time1); //獲取當前時間,秒數(shù)
     printf("time() test is %ld = %ld \n", time0, time1);
     printf("ctime() :%s", ctime(&time1)); //直接字符串打印 帶換行的
 ?
     //先用gmtime,沒有經(jīng)過時區(qū)轉(zhuǎn)換的時間 只有這里才不會有時區(qū)的轉(zhuǎn)換  差8小時
     struct tm* g_time = gmtime(&time0);
     printf("asctime(gmtime()) :%s", asctime(g_time)); //轉(zhuǎn)換為字符串進行打印
     //用localtime 轉(zhuǎn)換為經(jīng)過時區(qū)轉(zhuǎn)換的時間
     struct tm* l_time = localtime(&time0);
     printf("asctime(localtime()) :%s", asctime(l_time)); //轉(zhuǎn)換為字符串進行打印
 ?
     printf("mktime(g_time) to s :[%ld] \n", mktime(g_time)); //這里默認帶了時區(qū)的轉(zhuǎn)換
     printf("mktime(l_time) to s :[%ld] \n", mktime(l_time));
 ?
     char tmpbuf[128];
     struct tm* newtime=localtime(&time1);
     //測試相關(guān)的格式打印
     strftime( tmpbuf, 128, "Today is %A, day %d of %B in the year %Y.\n", newtime);
     printf("%s \n", tmpbuf);
 ?
     //其他兩個結(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 *)獲取特定時鐘的時間:
     CLOCK_REALTIME              統(tǒng)當前時間,從1970年1.1日算起
     CLOCK_MONOTONIC             系統(tǒng)的啟動時間,不能被設(shè)置
     CLOCK_PROCESS_CPUTIME_ID    本進程運行時間
     CLOCK_THREAD_CPUTIME_ID     本線程運行時間
 相關(guān)函數(shù)
     //把獲得的時間秒數(shù)轉(zhuǎn)換為struct tm 類型
     struct tm *localtime(const time_t *clock);  //線程不安全
     struct tm* localtime_r(const time_t* timer, struct tm* result );//線程安全
     //格式化時間字符串 
     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)的時間
 ******************/
 //1:clock_gettime 操作timespec結(jié)構(gòu),返回秒+納秒結(jié)構(gòu)的時間,可以定義時鐘類型
 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)時間 這里微妙和納秒可以對結(jié)構(gòu)體操作進行轉(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);
 }
 //獲取當前時間  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: 格式化的時間字符串。如:2018-11-28 10:13:40,表示為"Wed Nov 28 10:13:40 2018"。
 ?   %F: 日期格式為yyyy-mm-dd,與%Y:%m:%d作用相同。如:2018-11-28。
 ?   %X: 時間格式為hh:mm:ss。如:10:13:40。
 ?   %j: 一年中的第幾天,范圍:001-366.
 ?   %W: 一年中的第幾周,范圍:00-53.
 ?   %Y: 日期中的年,如:2018。
 ?   %m: 日期中的月,范圍:00-12。
 ?   %d: 日期中的天,范圍:01-31。
 ?   %H: 小時,范圍: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é)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論