雷火电竞-中国电竞赛事及体育赛事平台

歡迎來(lái)到入門(mén)教程網(wǎng)!

C語(yǔ)言

當(dāng)前位置:主頁(yè) > 軟件編程 > C語(yǔ)言 >

C語(yǔ)言獲取Linux系統(tǒng)精確時(shí)間的方法

來(lái)源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C語(yǔ)言|點(diǎn)擊:

gettimeofday()函數(shù)的使用方法

1.函數(shù)原型

#include <sys/time.h>

int gettimeofday(struct timeval *tv, struct timezone *tz);

2.說(shuō)明

gettimeofday()會(huì)把目前的時(shí)間用tv 結(jié)構(gòu)體返回,當(dāng)?shù)貢r(shí)區(qū)的信息則放到tz所指的結(jié)構(gòu)中

3.結(jié)構(gòu)體

struct timeval{

 

    long tv_sec;/*秒*/

    long tv_usec;/*微妙*/

};

struct timezone{

    int tz_minuteswest; /*和greenwich 時(shí)間差了多少分鐘*/

    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);
  /* 時(shí)間最大長(zhǎng)度為: 年 4位、 月 2位 、日 2位 、時(shí) 2位 、分 2位 、秒 2位 毫秒 6位 = 20位 */ 
 /* 對(duì)不夠長(zhǎng)度的末尾補(bǔ)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語(yǔ)言獲取Linux系統(tǒng)精確時(shí)間的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持我們。

上一篇:C++中繼承與多態(tài)的基礎(chǔ)虛函數(shù)類(lèi)詳解

欄    目:C語(yǔ)言

下一篇:C++ 中類(lèi)對(duì)象類(lèi)型的轉(zhuǎn)化的實(shí)例詳解

本文標(biāo)題:C語(yǔ)言獲取Linux系統(tǒng)精確時(shí)間的方法

本文地址:http://www.jygsgssxh.com/a1/Cyuyan/1206.html

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有