自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

linux下獲取系統(tǒng)時(shí)間的方法

運(yùn)維 系統(tǒng)運(yùn)維

可以用 localtime 函數(shù)分別獲取年月日時(shí)分秒的數(shù)值。

Linux下獲得系統(tǒng)時(shí)間的C語(yǔ)言的實(shí)現(xiàn)方法:

1. 可以用 localtime 函數(shù)分別獲取年月日時(shí)分秒的數(shù)值。

#include<time.h>     //C語(yǔ)言的頭文件

#include<stdio.h>     //C語(yǔ)言的I/O

void   main()

{

time_t   now;         //實(shí)例化time_t結(jié)構(gòu)

struct   tm     *timenow;         //實(shí)例化tm結(jié)構(gòu)指針

time(&now);

//time函數(shù)讀取現(xiàn)在的時(shí)間(國(guó)際標(biāo)準(zhǔn)時(shí)間非北京時(shí)間),然后傳值給now

timenow   =   localtime(&now);

//localtime函數(shù)把從time取得的時(shí)間now換算成你電腦中的時(shí)間(就是你設(shè)置的地區(qū))

printf("Local   time   is   %s\n",asctime(timenow));

//上句中asctime函數(shù)把時(shí)間轉(zhuǎn)換成字符,通過(guò)printf()函數(shù)輸出

}

注釋:time_t是一個(gè)在time.h中定義好的結(jié)構(gòu)體。而tm結(jié)構(gòu)體的原形如下:

struct   tm

{

int   tm_sec;//seconds   0-61

int   tm_min;//minutes   1-59

int   tm_hour;//hours   0-23

int   tm_mday;//day   of   the   month   1-31

int   tm_mon;//months   since   jan   0-11

int   tm_year;//years   from   1900

int   tm_wday;//days   since   Sunday,   0-6

int   tm_yday;//days   since   Jan   1,   0-365

int   tm_isdst;//Daylight   Saving   time   indicator

};

2. 對(duì)某些需要較高精準(zhǔn)度的需求,Linux提供了gettimeofday()。

#include   <stdio.h>

#include   <stdlib.h>

#include   <sys/time.h>

int  main(int argc,   char **argv)

{

struct   tim   start,stop,diff;

gettimeofday(&start,0);

//做你要做的事...

gettimeofday(&stop,0);

tim_subtract(&diff,&start,&stop);

printf("總計(jì)用時(shí):%d毫秒\n",diff.tv_usec);

}

int tim_subtract(struct tim *result, struct tim *x, struct tim *y)

{

int nsec;

if ( x->tv_sec > y->tv_sec )

return   -1;

if ((x->tv_sec==y->tv_sec) && (x->tv_usec>y->tv_usec))

return   -1;

result->tv_sec = ( y->tv_sec-x->tv_sec );

result->tv_usec = ( y->tv_usec-x->tv_usec );

if (result->tv_usec<0)

{

result->tv_sec--;

result->tv_usec+=1000000;

}

return   0;

}

【編輯推薦】

  1. 如何在Linux命令行模式下修改系統(tǒng)時(shí)間
  2. Debian下系統(tǒng)時(shí)間比正常時(shí)間快8小時(shí)的問(wèn)題A>
  3. Linux內(nèi)核中的DeviceMapper機(jī)制
責(zé)任編輯:趙寧寧 來(lái)源: chinaitlab
相關(guān)推薦

2014-09-16 16:28:33

Linux

2020-11-04 11:25:33

Linux目錄命令

2011-08-22 15:39:38

Linux

2011-04-21 09:54:14

Linuxiptables

2011-08-16 14:18:09

UbuntuLinuxLinuxwindows

2009-09-07 09:56:02

Linux系統(tǒng)LVM擴(kuò)充Linux

2011-01-07 17:08:22

linux系統(tǒng)時(shí)間

2011-03-09 13:02:15

LinuxLAMP安裝

2009-07-01 09:13:10

Linux

2009-08-06 19:37:17

2009-07-27 10:27:34

Javascript

2009-06-25 08:53:03

Linux操作系統(tǒng)

2009-03-30 14:32:15

LinuxNetware服務(wù)器

2009-06-30 10:35:36

Linux

2009-06-29 09:21:41

Linux

2012-02-06 09:40:25

2009-04-28 19:05:28

Linux系統(tǒng)服務(wù)器

2009-06-25 08:58:03

Linux

2011-03-03 15:21:11

2011-03-21 15:42:14

LinuxNagios
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)