Linux應用程式設計 時間程式設計

2021-10-10 17:07:02 字數 4405 閱讀 8016

相關標頭檔案

time.h 是iso c99 標準日期標頭檔案。

sys/time.h 是linux系統的日期標頭檔案。

注: sys/time.h 通常會包含include 「time.h」

/* structure crudely representing a timezone.

this is obsolete and should never be used. */

struct timezone

;

上面的過時了不應該使用,而且在這個標頭檔案中也找不到timeval的定義了

新的定義在下面的標頭檔案中,不過使用的時候一般呼叫sys/time.h就可以了

很多的介面還在這個標頭檔案中的,

///注意這個不是核心的標頭檔案,是在usr/include目錄下的,核心空間驅動的標頭檔案在kernel目錄下的。

2023年1月1日起經過的秒數和us

struct timespec

;#endif

struct timeval

;struct timezone ;/*

* the ids of the various system clocks (for posix.1b interval timers):

*/#define clock_realtime 0

//2023年1.1日算起

#define clock_monotonic 1

//系統的啟動時間不能被設定

#define clock_process_cputime_id 2

//本程序執行時間

#define clock_thread_cputime_id 3

//本執行緒執行時間

#define clock_monotonic_raw 4

#define clock_realtime_coarse 5

#define clock_monotonic_coarse 6

#define clock_boottime 7

#define clock_realtime_alarm 8

#define clock_boottime_alarm 9

timespec和timeval的精度不一樣攜帶的乙個是ns和us

ids 定義的巨集一般用於clock_gettime介面用來獲取特定的時鐘時間帶出timespec 描述

/* get the current time of day and timezone information,

putting it into *tv and *tz. if tz is null, *tz is not filled.

returns 0 on success, -1 on errors.

note: this form of timezone information is obsolete.

use the functions and variables declared in instead. */

extern

int gettimeofday (

struct timeval *__restrict __tv,

__timezone_ptr_t __tz) __throw __nonnull ((1

));# define timeval_to_timespec(tv, ts)

# define timespec_to_timeval(tv, ts)

下面幾個巨集介面依賴timevals的

convenience macros for operations on timevals.

note: `timercmp' does not work for

>= or <=

timerisset

(tvp)

->

>

((tvp)

->tv_sec ||

(tvp)

->tv_usec)

//判斷是否有值

timerclear

(tvp)

->

>

((tvp)

->tv_sec =

(tvp)

->tv_usec =0)

//清空屬性

timercmp

(a, b, cmp)

//cmp是比較符 > < = 返回真假

timeradd

(a, b, result)

//a+b >> result

timersub

(a, b, result)

//a-b >>result

void

timeradd

(struct timeval *a,

struct timeval *b,

struct timeval *res)

;void

timersub

(struct timeval *a,

struct timeval *b,

struct timeval *res)

;void

timerclear

(struct timeval *tvp)

;int

timerisset

(struct timeval *tvp)

;int

timercmp

(struct timeval *a,

struct timeval *b, cmp)

;

獲取今天的實時時間,通過tv和tz帶出來,tz空則不賦值

通過巨集可以將乙個tv值轉化成ts的值

常用**

#include

struct timeval cur_tv =

;struct timeval start_tv =

;struct timeval cost_time_tv =

;//all time cost

struct timeval sub_tv =

;//one time cost

gettimeofday

(&start_tv,

null);

sleep(1

);//***x

gettimeofday

(&cur_tv,

null);

timersub

(&cur_tv,

&start_tv,

&sub_tv)

;timeradd

(&cost_time_tv,

&sub_tv,

&cost_time_tv)

;printf

(" cost time [%d s]\n"

, sub_tv.tv_sec, sub_tv.tv_usec)

;

time_t msctime --

----

2023年到現今的秒數

/* used by other time functions. */

struct tm

/* return the current time and put it in *timer if timer is not null. */

extern time_t time (time_t *__timer) __throw;

/* return the `struct tm' representation

of *timer in the local timezone. */

extern

struct tm *localtime (__const time_t *__timer) __throw;

/* return a string of the form "day mon dd hh:mm:ss yyyy\n"

that is the representation of tp in this format. */

extern

char

*asctime (__const struct tm *__tp) __throw;

通過time(&msctime ),來獲取當前秒數

localtime 相當於格式化一下time那個大數

struct tm *tm= localtime(&msctime );

asctime 格式化字串的形式,這樣就可以直接printf打出來了

Linux應用程式設計之時間程式設計

一 時間的型別 1 格林威治標準時間 coordinated universal time utc 是世界標準時間,即常說的格林威治標準時間 greenwich mean time,gmt 注 格林威治時間和本地時間不同 2 日曆時間 日曆時間 calendar time 是用 乙個標準時間點 如 ...

Linux應用程式設計之程序程式設計

程序同步 一組併發的程序按照一定的順序執行的過程稱為程序間的同步。獲取id include pid t getpid void 獲取本程序id pid t getppid void 在子程序中獲取父程序id 程序建立 include pid t fork void fork 的奇妙之處在於它被呼叫一...

Linux應用程式設計之程序程式設計

程序同步 一組併發的程序按照一定的順序執行的過程稱為程序間的同步。獲取id include pid t getpid void 獲取本程序id pid t getppid void 在子程序中獲取父程序id 程序建立 include pid t fork void fork 的奇妙之處在於它被呼叫一...