C語言獲取Linux系統(tǒng)精確時間的方法
來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點擊: 次
gettimeofday()函數的使用方法
1.函數原型
#include <sys/time.h> int gettimeofday(struct timeval *tv, struct timezone *tz);
2.說明
gettimeofday()會把目前的時間用tv 結構體返回,當地時區(qū)的信息則放到tz所指的結構中
3.結構體
struct timeval{ long tv_sec;/*秒*/ long tv_usec;/*微妙*/ }; struct timezone{ int tz_minuteswest; /*和greenwich 時間差了多少分鐘*/ int tz_dsttime; /*DST的校正*/ }
#include <stdio.h> #include <time.h> #include <sys/time.h> #include <string.h> #define SIZE_OF_DATETIME 20 void sysUsecTime(char *pTime) { struct timeval tv; struct timezone tz; int i=0; struct tm *p; char sys_time[SIZE_OF_DATETIME+1]=""; gettimeofday(&tv, &tz); p = localtime(&tv.tv_sec); sprintf(sys_time,"%d%d%d%d%d%d%ld",1900+p->tm_year, 1+p->tm_mon, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec, tv.tv_usec); printf("strlen(sys_time)=[%d]\n",strlen(sys_time)); printf("sys_time=[%s]\n",sys_time); /* 時間最大長度為: 年 4位、 月 2位 、日 2位 、時 2位 、分 2位 、秒 2位 毫秒 6位 = 20位 */ /* 對不夠長度的末尾補0 */ for ( i=strlen(sys_time);i<SIZE_OF_DATETIME;i++) { sys_time[i]='0'; } sys_time[SIZE_OF_DATETIME]='\0'; strcpy(pTime,sys_time); } int main(void) { char strusecTime[SIZE_OF_DATETIME+1]; sysUsecTime(strusecTime); printf("%s\n",strusecTime); return 0; }
以上這篇C語言獲取Linux系統(tǒng)精確時間的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持我們。
您可能感興趣的文章
- 04-02c語言函數調用后清空內存 c語言調用函數刪除字符
- 04-02c語言的正則匹配函數 c語言正則表達式函數庫
- 04-02func函數+在C語言 func函數在c語言中
- 04-02c語言中對數函數的表達式 c語言中對數怎么表達
- 04-02c語言用函數寫分段 用c語言表示分段函數
- 04-02c語言編寫函數冒泡排序 c語言冒泡排序法函數
- 04-02c語言沒有round函數 round c語言
- 04-02c語言分段函數怎么求 用c語言求分段函數
- 04-02C語言中怎么打出三角函數 c語言中怎么打出三角函數的值
- 04-02c語言調用函數求fibo C語言調用函數求階乘


閱讀排行
本欄相關
- 04-02c語言函數調用后清空內存 c語言調用
- 04-02func函數+在C語言 func函數在c語言中
- 04-02c語言的正則匹配函數 c語言正則表達
- 04-02c語言用函數寫分段 用c語言表示分段
- 04-02c語言中對數函數的表達式 c語言中對
- 04-02c語言編寫函數冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數 round c語言
- 04-02c語言分段函數怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數 c語言中怎
- 04-02c語言調用函數求fibo C語言調用函數求
隨機閱讀
- 01-11ajax實現頁面的局部加載
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05dedecms(織夢)副欄目數量限制代碼修改
- 01-10C#中split用法實例總結
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
- 01-10delphi制作wav文件的方法
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 04-02jquery與jsp,用jquery