第1关:Linux获取时间
/************************************************************************* > File Name: GetTimeTest.c > Author: ma6174 > Mail: ma6174@163.com > Created Time: Tue 17 Apr 2018 08:42:30 PM CST ************************************************************************/ #include <stdio.h> #include <unistd.h> #include <time.h> typedef struct _localtimestruct { int year; int month; int day; int hour; int minute; int second; }localtimestruct; time_t gettimesecond (void) { /*************Begin***********/ time_t seconds = time (NULL); return seconds; /**************End************/ } void getlocaltv (localtimestruct *ltinfo) { /*************Begin***********/ time_t current = time(NULL); struct tm *tv = localtime(¤t); ltinfo->year = tv->tm_year + 1900; ltinfo->month = tv->tm_mon + 1; ltinfo->day = tv->tm_mday; ltinfo->hour = tv->tm_hour; ltinfo->minute = tv->tm_min; ltinfo->second = tv->tm_sec; /**************End************/ }
第2关:Linux时间相互转换
/************************************************************************* > File Name: TimeTransTest.c > Author: ma6174 > Mail: ma6174@163.com > Created Time: Tue 17 Apr 2018 10:13:59 PM CST ************************************************************************/ #include <stdio.h> #include <unistd.h> #include <string.h> #include <time.h> #include <sys/time.h> typedef struct _localtimestruct { int year; int month; int day; int hour; int minute; int second; }localtimestruct; void func (void); void timetrans (time_t current, char *time_str, localtimestruct *ltinfo, struct tm *tm_time) { /*************Begin***********/ if (NULL != time_str) { memcpy (time_str, ctime(¤t), strlen(ctime(¤t))); } if(NULL != tm_time) { memcpy(tm_time, gmtime(¤t), sizeof(struct tm)); if (NULL != ltinfo) { ltinfo->year = tm_time->tm_year + 1900; ltinfo->month = tm_time->tm_mon + 1; ltinfo->day = tm_time->tm_mday; ltinfo->hour = tm_time->tm_hour; ltinfo->minute = tm_time->tm_min; ltinfo->second = tm_time->tm_sec; } } /**************End************/ } long getdifftimeval (void) { /*************Begin***********/ struct timeval start; struct timeval end; gettimeofday(&start, NULL); func(); gettimeofday(&end, NULL); long timeval_s = (end.tv_sec * 1000 * 1000 + end.tv_usec) - (start.tv_sec * 1000 * 1000 + start.tv_usec); return timeval_s; /**************End************/ }
第3关:Linux 时间定时器
/************************************************************************* > File Name: TimerTest.c > Author: ma6174 > Mail: ma6174@163.com > Created Time: Thu 19 Apr 2018 02:58:54 PM CST ************************************************************************/ #include <stdio.h> #include <unistd.h> #include <string.h> #include <signal.h> #include <time.h> #include <sys/time.h> void func (void); static int count = 0;//the number of calling loopevent void loopevent (int signo) { /************Begin************/ if(SIGPROF == signo) { func (); if(4 > count) { count++; } else { struct itimerval new_it; new_it.it_interval.tv_sec = 0; new_it.it_interval.tv_usec = 0; new_it.it_value.tv_sec = 0; new_it.it_value.tv_usec = 0; setitimer (ITIMER_PROF, &new_it, NULL); } } /*************End*************/ } void setlocaltimer (void) { /************Begin************/ struct itimerval new_it; struct itimerval old_it; struct sigaction act; /*register signal function*/ act.sa_handler = loopevent; act.sa_flags = 0; sigemptyset (&act.sa_mask); sigaction (SIGPROF, &act, NULL); new_it.it_interval.tv_sec = 1; new_it.it_interval.tv_usec = 0; new_it.it_value.tv_sec = 3; new_it.it_value.tv_usec = 0; setitimer (ITIMER_PROF, &new_it, &old_it); /*************End*************/ }