嵌入式开发记录-day10 linux时间
1、linux时间包括获取当前时间、延时、不同时间格式、地区时间转换等操作;
2、常见的时间格式:
UTC时间:全时间统一的时间;
UNIX纪元时间,1970年0时0分0秒;
GMT格林尼治标准时间太阳经过格林尼治时间,东八区时间;
机器日历时间:UNIX纪元时间;
3、经常可能会用到的函数
延时函数
系统编程常用函数:sleep()、usleep()
内核中常用到函数:ndelay()、udelay()、mdelay()
时间转换:秒精度 char* ctime(const time_t* timep); // 时间转为字符格式 struct tm* gmtime(const time_t* timep); // 将时间转为格林威只时间 char* asctime(const struct tm* tm);// 将时间转为字符格式 精确到秒 struct tm* localtime(const time_t* clock); // 将时间转为本地时间#include <sys/time.h>
struct tm {
int tm_sec; //* Seconds (0-60)
int tm_min; //* Minutes (0-59)
int tm_hour; //* Hours (0-23)
int tm_mday; //* Day of the month (1-31)
int tm_mon; //* Month (0-11)
int tm_year; //* Year - 1900
int tm_wday; //* Day of the week (0-6, Sunday = 0)
int tm_yday; //* Day in the year (0-365, 1 Jan = 0)
int tm_isdst; //* Daylight saving time
};
// 设置时间和获取时间 精度比较高 精确到微秒us 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
};
#include <time.h>
// 时间转换相关函数
char *asctime(const struct tm *tm);
char *asctime_r(const struct tm *tm, char *buf);
char *ctime(const time_t *timep);
char *ctime_r(const time_t *timep, char *buf);
struct tm *gmtime(const time_t *timep);
struct tm *gmtime_r(const time_t *timep, struct tm *result);
struct tm *localtime(const time_t *timep);
struct tm *localtime_r(const time_t *timep, struct tm *result);
time_t mktime(struct tm *tm);
4、相关函数测试
/******************* #include <unistd.h> unsigned int sleep(unsigned int seconds); int usleep(useconds_t usec); */ #include <stdio.h> #include <unistd.h> #include <time.h> #include <sys/time.h> void main(void) { int i=10; while(i--){ sleep(2); // 延时2s printf("sleep 2 second\n"); usleep(100000); // 延时100ms printf("usleep 100000\n"); } }
/**************************** #include <time.h> time_t time(time_t *t); /// returns the time as the number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC). */ #include <stdio.h> #include <unistd.h> #include <time.h> #include <sys/time.h> void main(void) { time_t t1,t2; time(&t1); printf("UTC time:0x%08x\n",t1); t1 = time(NULL); // 获取1970年以来的秒数 printf("UTC time:0x%08x\n",t1); // 时间转换: struct tm* tb; time(&t2); printf("ctime is :%s\n",ctime(&t2)); // 转成字符格式 printf("asctime is %s\n",asctime(gmtime(&t2))); // 将时间转为字符格式 // 转化为本地时间 tb = localtime(&t2); // 转化为本地时间 printf("localtime is %s\n",asctime(tb)); // 转换为年月日时分秒的形式 printf("localtime is %s\n",ctime(&t2)); }
/*************************** #include <sys/time.h> // 获取时间精度比较高,可以获取运行时间 精确到ms int gettimeofday(struct timeval *tv, struct timezone *tz); int settimeofday(const struct timeval *tv, const struct timezone *tz); */ #include <stdio.h> #include <unistd.h> #include <time.h> #include <sys/time.h> void main(void) { int i=0; float timeuse; // 求代码运行时间 struct timeval ts,te; gettimeofday(&ts,NULL); for(i =0;i<10000000;i++){} gettimeofday(&te,NULL); timeuse = 1000*(te.tv_sec - ts.tv_sec); printf("use time :%f\n",timeuse); } // 输出结果,小数点后好多位 [root@iTOP-4412]# ./mnt/disk/delay use time :0.000000 [root@iTOP-4412]#
5、linux下的四种时间类型:
1 time_t:长整型,保存1970年以来的秒数 time_t time = time(NULL); 2 struct timeb:int ftime(struct timeb *tp) // 精确到毫秒 3 struct timeb { 4 time_t time; 5 unsigned short millitm; 6 short timezone; 7 short dstflag; 8 }; 9 10 struct timeval: // 精确到微秒 11 struct timespec:int clock_gettime(clockid_t clk_id, struct timespec *tp); 精确到ns纳秒 12 struct timespec { 13 time_t tv_sec; /* seconds */ 14 long tv_nsec; /* nanoseconds */ 15 }; 16 17 clock_t:clock_t clock(void); 表示进程占用CPU时间 精确到微秒us,与进程相关 18 19 struct tm:最直观的时间,属于生活中经常出现
6、msleep()与mdelay()异同点
1、对于模块来说 mdelay()是忙等待函数,延迟过程中无法运行其他任务,延时时间准确;一般使用定时器延时,此时等待定时器溢出结束 msleep()是休眠休眠函数,任务休眠、任务挂起等,因此延时会比设定的时间长些 2、对于系统: mdelay()此时会占用CPU资源,定时器运行,线程运行,此时其他模块无法使用CPU资源 msleep() 进入休眠态的线程会挂起,因此不会占用CPU资源,需要等待信号唤醒线程;

浙公网安备 33010602011771号