2017年3月14日 星期二

linux- time setting ( gettimeofday()、localtime() )



gettimeofday()

Introduction
  • C語言中可以使用 gettimeofday() 來得到時間,精準度可達到微秒。
Function Prototype

  • #include<sys/time.h>
  • int gettimeofday(struct  timeval*tv,struct  timezone *tz )

Description
  • 目前的時間用tv 指標回傳,目前的時區則存放在tz 所指的結構中。
Data Structure

struct  timeval{
       long  tv_sec;/*秒*/
       long  tv_usec;/*微妙*/
};

struct  timezone{
        int tz_minuteswest;/*和greenwich 時間差多少*/
        int tz_dsttime;/*type of DST correction*/
}

在gettimeofday() 中參數 tv tz 皆可為NULL,設定為NULL 則不回傳其結構。

函數執行成功後返回0,失敗後回傳-1。

Example-1

#include<stdio.h>
#include<sys/time.h>
#include<unistd.h>

int main()
{
        struct  timeval    tv;
        struct  timezone   tz;
        gettimeofday(&tv,&tz);

        printf("tv_sec:%d\n",tv.tv_sec);
        printf("tv_usec:%d\n",tv.tv_usec);
        printf("tz_minuteswest:%d\n",tz.tz_minuteswest);
        printf("tz_dsttime:%d\n",tz.tz_dsttime);
}

說明:在使用gettimeofday()函數時,第二個參數一般都為空,一般都只是為了取得當前時間,而不用取得timezone的數值。


Example-2

#include<stdio.h>
#include<sys/time.h>
#include<unistd.h>
  
int delay(int time)
{
    int i,j;
    
    for(i =0;i<time;i++)
        for(j=0;j<5000;j++)
            ;
}
  
int main()
{
        struct  timeval start;
        struct  timeval end;
        
        unsigned  long diff;
        gettimeofday(&start,NULL);
        delay(10);
        gettimeofday(&end,NULL);
        diff = 1000000 * (end.tv_sec-start.tv_sec)+ end.tv_usec-start.tv_usec;
        printf("thedifference is %ld\n",diff);
        return 0;
        
}

測試呼叫delay()函數所需執行的時間(單位為微妙)




localtime()

Introduction
  • localtime() 可以轉換 time()轉換成 year, month, day, haurs, secends等單位。
  • 有效範為為自Jan 1 1970 00:00:00 至 Jan 19 2038 03:14:07
Function Prototype

#include<sys/time.h>
struct tm *localtime(const time_t *timer)

Description
  • localtime() 以指向日曆時間的指標當作參數,將此日曆時間轉換成結構 tm 的表示方法,並回傳表示此結構 tm 。
Data Structure
struct tm {
 int tm_sec; /* Seconds */
 int tm_min; /* Minutes */
 int tm_hour; /* Hour (0 - 23) */
 int tm_mday; /* Day of month (1 - 31) */
 int tm_mon; /* Month (0 - 11) 0:元月*/
 int tm_year; /* Year (減去1900後之值) */
 int tm_wday; /* Weekday (0 - 6;Sunday is 0) */
 int tm_yday; /* Day of year (0 -365) */
 int tm_isdst; /* Nonzero if daylight saving time is in effect. */
}
Example-1

#include <stdio.h>
#include <time.h>
int main(void)
{
    time_t t1 = time(NULL);    // 自 1970 年 1 月 1 日後經過了多少秒可看Example-2 範例
    struct tm *nPtr = localtime(&t1);   
    int year = nPtr->tm_year + 1900;
    int month = nPtr->tm_mon + 1;
    int mday = nPtr->tm_mday;
    int wday = nPtr->tm_wday;

    printf("今天是 %u 年 %u 月 %u 號星期 %u\n", year, month, mday, wday);
     
    return 0;
}

Example-2

#include <stdio.h>
#include <time.h>
int main(void)
{
    time_t t1 = time(NULL); 
    printf("自 1970 年 1 月 1 日後經過了 %d 秒....\n", t1);
     
    return 0;

沒有留言:

張貼留言

linux - IPC (inter-processes communication) Shered mempry(共用記憶體)

IPC (inter-processes communication) 顧名思義,processes 之間溝通的管道&機制。 Shared memory   程式間可以共享memory Message Queue  程式間傳送資訊最簡單的方法 Semaphore...