Search Suggest

Time and Date

1. time
Unix/Linux cung cấp thư viện time.h bao gồm các cấu trúc và hàm để xác định thời gian. Chúng thường được dùng để làm bộ timer, đo tốc độ của một đoạn xử lý logic, ...Thời gian trong Unix/Linux được lấy mốc là 00:00:00 (hh:mm:ss) 01-01-1970 UTC.

 #include <time.h>  
time_t time(time_t *tloc);

Hàm time lấy thời gian hiện tại.
+ tloc
Thời gian hiện tại trả về được lưu vào tloc nếu tloc không NULL.
+ return
Trả về thời gian hiện tại lưu trong time_t tính bằng giây tính từ thời điểm bắt đầu 00:00:00 (hh:mm:ss) 01-01-1970 UTC.

Ex:
envtime.c
 #include <time.h>  
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

int main()
{
int i;
time_t the_time, timmer;
for(i = 1; i <= 5; i++) {
the_time = time(&timmer);
printf("The time is %ld\n", the_time); // the_time == timmer
sleep(1);
}
exit(0);
}


Execute:
 $ gcc envtime.c   
$ ./a.out
The time is 1438358154
The time is 1438358155
The time is 1438358156
The time is 1438358157
The time is 1438358158


2. gmtime
 #include <time.h>  
struct tm *gmtime(const time_t timeval);

struct tm:

Ex:
gmtime.c
 #include <time.h>  
#include <stdio.h>
#include <stdlib.h>
int main()
{
struct tm *tm_ptr;
time_t the_time;
(void) time(&the_time);

tm_ptr = gmtime(&the_time);
printf("Raw time is %ld\n", the_time);
printf("gmtime gives:\n");
printf("date: %02d/%02d/%02d\n",
tm_ptr->tm_year, tm_ptr->tm_mon+1, tm_ptr->tm_mday);
printf("time: %02d:%02d:%02d\n",
tm_ptr->tm_hour, tm_ptr->tm_min, tm_ptr->tm_sec);
exit(0);
}


Execute:
 $ gcc gmtime.c   
$ ./a.out
Raw time is 1438358295
gmtime gives:
date: 115/07/31
time: 15:58:15


3. ctime
 #include <time.h>  
char *asctime(const struct tm *timeptr);
char *ctime(const time_t *timeval);

Ex:
ctime.c
 #include <time.h>  
#include <stdio.h>
#include <stdlib.h>
int main()
{
time_t timeval;
(void)time(&timeval);
printf("The date is: %s", ctime(&timeval));
exit(0);
}


 $ gcc ctime.c   
$ ./a.out
The date is: Fri Jul 31 23:00:27 2015

4. localtime
 #include <time.h>  
struct tm *localtime(const time_t *timeval);

Ex:
localtime.c
 /* localtime example */  
#include <stdio.h> /* puts, printf */
#include <time.h> /* time_t, struct tm, time, localtime */

int main ()
{
time_t rawtime;
struct tm * timeinfo;

time (&rawtime);
timeinfo = localtime (&rawtime);
printf ("Current local time and date: %s", asctime(timeinfo));

return 0;
}

Execute:
 $ gcc localtime.c  
$ ./a.out
Current local time and date: Fri Jul 31 16:11:47 2015

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

 struct timeval {  
time_t tv_sec; /* seconds */
suseconds_t tv_usec; /* microseconds */
};

struct timezone {
int tz_minuteswest; /* minutes west of Greenwich */
int tz_dsttime; /* type of DST correction */
};

Ex:
gettimeofday.c
 #include <stdio.h>  
#include <stdlib.h>
#include <sys/time.h>

int main()
{
long int cycle = 2000000;//us ~ 2s
struct timeval tv, tv2;

gettimeofday(&tv,NULL);
while(1){
gettimeofday(&tv2,NULL);
if(tv2.tv_sec * 1000000 + tv2.tv_usec >= tv.tv_sec * 1000000 + tv.tv_usec + cycle){
printf("Timer is starting ... \n");
break;
}
}

return 0;
}


Đăng nhận xét