当前位置: 首页 > news >正文

国内装饰行业网站制作个人做哪方面网站

国内装饰行业网站制作,个人做哪方面网站,wordpress浮动导航菜单,建设银行河北分行招聘网站1.前言 对于linux下的编程#xff0c;无论是用户态还是内核态#xff0c;时间获取都是经常需要使用到的。以下分别从用户态和内核态整理了几个常用的时间获取接口#xff0c;供编写代码时快速查阅。 2.用户态获取时间 2.1 clock_gettime() #include time.hint c…1.前言 对于linux下的编程无论是用户态还是内核态时间获取都是经常需要使用到的。以下分别从用户态和内核态整理了几个常用的时间获取接口供编写代码时快速查阅。 2.用户态获取时间 2.1 clock_gettime() #include time.hint clock_gettime (clockid_t __clock_id, struct timespec *__tp);作用根据系统时钟的类型获取当前时间__clock_id系统时钟的类型。常用取值 CLOCK_REALTIME: 从1970年1月1日到目前的时间CLOCK_MONOTONIC: 系统启动时间CLOCK_PROCESS_CPUTIME_ID: 本进程运行时间CLOCK_THREAD_CPUTIME_ID: 本线程运行的时间 __tp: 存放当前的时间。返回值成功则返回0失败则返回-1 timespec结构体 struct timespec {__time_t tv_sec; /* Seconds. 秒 */__syscall_slong_t tv_nsec; /* Nanoseconds. 纳秒*/ };示例 #include stdio.h #include string.h #include time.hlong long get_clock_sys_time_ns(void) {struct timespec tp;long long time_ns 0;clock_gettime(CLOCK_MONOTONIC, tp);time_ns (long long)tp.tv_sec * 1000000000 tp.tv_nsec;return time_ns; }int main(void) {struct timespec tp;/// 获取从1970年1月1日到目前的时间memset(tp, 0, sizeof(struct timespec));clock_gettime(CLOCK_REALTIME, tp);printf(clock_id CLOCK_REALTIME, sec %ld, nsec %ld\n, tp.tv_sec, tp.tv_nsec);/// 获取系统启动时间memset(tp, 0, sizeof(struct timespec));clock_gettime(CLOCK_MONOTONIC, tp);printf(clock_id CLOCK_MONOTONIC, sec %ld, nsec %ld, sys_time %lld ns\n, tp.tv_sec, tp.tv_nsec, get_clock_sys_time_ns());/// 获取本进程运行时间memset(tp, 0, sizeof(struct timespec));clock_gettime(CLOCK_PROCESS_CPUTIME_ID, tp);printf(clock_id CLOCK_PROCESS_CPUTIME_ID, sec %ld, nsec %ld\n, tp.tv_sec, tp.tv_nsec);/// 获取本线程运行时间memset(tp, 0, sizeof(struct timespec));clock_gettime(CLOCK_THREAD_CPUTIME_ID, tp);printf(clock_id CLOCK_THREAD_CPUTIME_ID, sec %ld, nsec %ld\n, tp.tv_sec, tp.tv_nsec);return 0; }2.2.gettimeofday() #include sys/time.hint gettimeofday(struct timeval *tv, struct timezone *tz);作用获取当前时间从1970年1月1日到目前的时间tv当前UTC时间tz当前时区信息返回值成功则返回0失败则返回-1 timeval结构体 struct timeval {__time_t tv_sec; /* Seconds. 秒*/__suseconds_t tv_usec; /* Microseconds. 微秒*/ };timezone结构体 struct timezone{int tz_minuteswest; /* Minutes west of GMT. 和Greenwich时间差了多少分钟 */int tz_dsttime; /* Nonzero if DST is ever in effect. 日光节约时间的状态 */};示例 #include stdio.h #include string.h #include sys/time.hlong long get_sys_time_ms(void) {long long time_ms 0;struct timeval tv;gettimeofday(tv, NULL);time_ms ((long long)tv.tv_sec*1000000 tv.tv_usec) / 1000;return time_ms; }int main(void) {/// 获取系统时间printf(sys_time %lld ms\n, get_sys_time_ms());return 0; }2.3.time() #include time.htime_t time(time_t *tloc);作用获取1970-01-01 00:00:00 0000至今的秒数UTCtloc返回的秒存储指针返回值成功则返回秒数失败则返回-1错误原因存在errno中。 time_t的类型 typedef long time_t;示例 #include stdio.h #include time.htime_t get_utc_time(void) {return time(NULL); }int main(int argc, char **argv) {time_t utc_time get_utc_time();printf(utc_time %ld s\n, utc_time);return 0; }2.4.localtime() #include time.hstruct tm *localtime(const time_t *timep);作用将time_t类型的时间转换为struct tm类型的时间timep当前UTC秒数返回值返回当地时间 tm结构体 struct tm {int tm_sec; /* Seconds. [0-60] (1 leap second) */int tm_min; /* Minutes. [0-59] */int tm_hour; /* Hours. [0-23] */int tm_mday; /* Day. [1-31] */int tm_mon; /* Month. [0-11] 注意0代表1月以此类推*/int tm_year; /* Year - 1900. 该值为实际年份减去1900*/int tm_wday; /* Day of week. [0-6] 注意0代表星期一以此类推*/int tm_yday; /* Days in year.[0-365] 从每年的1月1日开始的天数其中0代表1月1日以此类推*/int tm_isdst; /* DST. [-1/0/1] 夏玲时标识符*/ };示例 #include stdio.h #include time.htime_t get_utc_time(void) {return time(NULL); }int main(int argc, char **argv) {time_t utc_time get_utc_time();printf(utc_time %ld s\n, utc_time);struct tm *local_tm localtime(utc_time); printf(local time %.4d-%.2d-%.2d %.2d:%.2d:%.2d\n, local_tm-tm_year 1900,local_tm-tm_mon 1,local_tm-tm_mday,local_tm-tm_hour,local_tm-tm_min,local_tm-tm_sec);return 0; }2.5.localtime_r() #include time.hstruct tm *localtime_r(const time_t *timep, struct tm *result);作用将time_t类型的时间转换为struct tm类型的时间timep当前UTC秒数result当地时间返回值返回当地时间 注 localtime不是一个线程安全的函数。对于实时性要求较高的系统多个线程同时调用localtime可能会造成数据被覆盖。使用localtime_r来替代。 示例 #include stdio.h #include time.htime_t get_utc_time(void) {return time(NULL); }int main(int argc, char **argv) {time_t utc_time get_utc_time();printf(utc_time %ld s\n, utc_time);struct tm result;struct tm *local_tm localtime_r(utc_time, result); printf(local time %.4d-%.2d-%.2d %.2d:%.2d:%.2d\n, local_tm-tm_year 1900,local_tm-tm_mon 1,local_tm-tm_mday,local_tm-tm_hour,local_tm-tm_min,local_tm-tm_sec);printf(result time %.4d-%.2d-%.2d %.2d:%.2d:%.2d\n, result.tm_year 1900,result.tm_mon 1,result.tm_mday,result.tm_hour,result.tm_min,result.tm_sec);return 0; }2.6.gmtime() #include time.hstruct tm *gmtime(const time_t *timep);作用返回tm结构的GMT时间UTC时间timep当前UTC秒数返回值返回当地时间 例子 #include stdio.h #include time.htime_t get_utc_time(void) {return time(NULL); }int main(int argc, char **argv) {time_t utc_time get_utc_time();printf(utc_time %ld s\n, utc_time);struct tm *gmt_tm gmtime(utc_time); printf(gmt time %.4d-%.2d-%.2d %.2d:%.2d:%.2d\n, gmt_tm-tm_year 1900,gmt_tm-tm_mon 1,gmt_tm-tm_mday,gmt_tm-tm_hour,gmt_tm-tm_min,gmt_tm-tm_sec);return 0; }localtime和gmtime的区别: localtime和gmtime都是C语言中的函数用于将time_t类型的时间转换为struct tm类型的时间。它们的区别在于gmtime将time_t转换为UTC时间即世界标准时间而localtime将time_t转换为本地时间。 例子使用gmtime与localtime接口返回的小时数来计算当地时区 #include stdio.h #include time.htime_t get_utc_time(void) {return time(NULL); }int main(int argc, char **argv) {time_t utc_time get_utc_time();printf(utc_time %ld s\n, utc_time);struct tm *gmt_tm gmtime(utc_time); printf(gmt time %.4d-%.2d-%.2d %.2d:%.2d:%.2d\n, gmt_tm-tm_year 1900,gmt_tm-tm_mon 1,gmt_tm-tm_mday,gmt_tm-tm_hour,gmt_tm-tm_min,gmt_tm-tm_sec);int gmt_hour gmt_tm-tm_hour;struct tm *local_tm localtime(utc_time); printf(local time %.4d-%.2d-%.2d %.2d:%.2d:%.2d\n, local_tm-tm_year 1900,local_tm-tm_mon 1,local_tm-tm_mday,local_tm-tm_hour,local_tm-tm_min,local_tm-tm_sec);int local_hour local_tm-tm_hour;int local_time_zone local_hour - gmt_hour;if (local_time_zone -12) {local_time_zone 24; } else if (local_time_zone 12) {local_time_zone - 24;}else{}printf(local_time_zone %d\n, local_time_zone);return 0; }3.内核态获取时间 3.1.do_gettimeofday()比较老的函数新内核可能不存在了 #include linux/time.h void do_gettimeofday(struct timeval *tv);作用与C标准库中gettimeofday()用法相同tv当前UTC时间 timeval结构体 struct timeval {__time_t tv_sec; /* Seconds. 秒*/__suseconds_t tv_usec; /* Microseconds. 微秒*/ };示例 #include linux/module.h #includelinux/time.h MODULE_LICENSE(GPL);int __init do_gettimeofday_init(void) {printk(do_gettimeofday test begin.\n);struct timeval now{.tv_sec0,.tv_usec0}; //声明一个变量do_gettimeofday(now); //调用函数获取时间此时间是距离1970-01-01 00:00:00的时间/*显示当前时间差*/printk(the seconds of the day is: %ld\n, now.tv_sec); //秒数printk(the microseconds of the day is: %ld\n, now.tv_usec); //微秒数printk(do_gettimeofday test over.\n);return 0; }void __exit do_gettimeofday_exit(void) {printk(Goodbye do_gettimeofday test\n); }module_init(do_gettimeofday_init); module_exit(do_gettimeofday_exit);3.2.基于ktime_t格式的时间 参考linux kernel时钟获取 ktime_get() #include linux/ktime.hktime_t ktime_get(void);作用获取的是CLOCK_MONOTONIC时间。通过ktime_get获取的时间是不统计设备休眠时间的并且这个时间统计的起始点则是设备启动后。 返回值返回ktime_t格式的数据类型单位为纳秒。 ktime_t的定义 typedef s64 ktime_t;示例 time_test_drv_init ktime_t curTime 0; curTime ktime_get(); TIME_TEST_INFO(ktime_get:%lld ns, curTime);ktime_get_ts64() #include linux/time64.hvoid ktime_get_ts64(struct timespec64 *ts)作用和ktime_get的功能是完全一样的区别在于对时间的表示数据类型由ktime_t变成了timespec64。 timespec64的定义如下: struct timespec64 {time64_t tv_sec; /* seconds */long tv_nsec; /* nanoseconds */ };timespec64中包含了秒和纳秒相对ktime_t纳秒这个时间表示更加适合人类查看. 示例 static void show_time_ts64(const char* caller, const int line, const struct timespec64 *curTimeTs) {pr_info(%s,%d:%lld s %ld ns\n, caller, __LINE__, curTimeTs-tv_sec, curTimeTs-tv_nsec); }time_test_drv_init struct timespec64 curTimeTs; ktime_get_boottime_ts64(curTimeTs); show_time_ts64(__func__, __LINE__, curTimeTs);3.ktime_get_boottime() static inline ktime_t ktime_get_boottime(void);作用:ktime_get_boottime获取的时间和ktime_get最大的不同是其包含了设备进入休眠的时间其这个时间统计的起始点也是设备启动后。 返回值返回值类型为ktime_t单位为纳秒。 示例 time_test_drv_init ktime_t curTime 0; curTime ktime_get_boottime(); TIME_TEST_INFO(ktime_get_boottime:%lld ns, curTime);4.ktime_get_boottime_ts() void ktime_get_boottime_ts64(struct timespec64 *)作用ktime_get_boottime_ts相对于ktime_get_boottime的功能是完全一样的区别在于对时间的表示数据类型由ktime_t变成了timespec64。 5.ktime_get_real() ktime_t ktime_get_real(void);作用ktime_get_real获取的时间的起点不是设备的启动时间点了而是相对UTC的即从1970开始。 示例 time_test_drv_init ktime_t curTime 0; curTime ktime_get_real(); TIME_TEST_INFO(ktime_get_real:%lld ns, curTime);6.ktime_get_real_ts() void ktime_get_real_ts(struct timespec64 *)作用ktime_get_real_ts相对于ktime_get_real的功能是完全一样的区别在于对时间的表示数据类型由ktime_t变成了timespec64。 示例 time_test_drv_init struct timespec64 curTimeTs; ktime_get_real_ts64(curTimeTs);4.延时函数的实现 void delay_us(uint32_t nus) {volatile uint32_t startts, endts, ts;ts nus;startts get_time_us();endts startts ts;if (endts startts){while (get_time_us() endts);}else{while (get_time_us() endts);while (get_time_us() endts);} }
http://www.w-s-a.com/news/753675/

相关文章:

  • 建一个团购网站WordPress文章字号设置
  • 做漂亮的网站东营网站seo
  • 网站开发摊销做设计哪个网站可以接单
  • 惠州h5网站建设建设公司网站报价
  • 做网站多少钱 优帮云嘉兴五县两区网站建设
  • 三亚旅游网站策划书企业网站建设的定位
  • 网站建设工作台账网站建设的实验结论
  • 商业网站建设平台制作软件的软件
  • 本地网站开发wordpress页面关键词和描述
  • 微网站 合同软件开发培训方案
  • 怎么做淘宝客网站备案广告公司图片大全
  • 微信小程序展示网站建设多少钱做网站用什么软件初二
  • 瀑布流资源网站模板打码网站建设
  • wordpress 支付宝打赏网站视觉优化的意义
  • 建设网站需要几个文件夹永久免费内存大服务器
  • 可信赖的手机网站建设wordpress 显示摘要
  • 谁给我一个企业邮箱认证wordpress优化攻略
  • 建站软件免费版下载涿州做网站的
  • html5网站开发西安哪家网站建设公司好
  • 怎么做网站赚广州番禺区是富人区吗
  • 服装网站推广方案戴尔网站建设成功
  • 手机网站布局国外可以用什么网站做问卷
  • 手机建网站网店logo设计图片免费
  • 装修网站有哪些wordpress外网访问错误
  • 个人做电影网站服务器放国外安全吗建设通app
  • 西安公司网站开发快站官网平台
  • 北京网站设计公司哪个好网站开发属于哪个部门
  • 现在海外做的比较好一点的网站网站报价书
  • 做整站优化漳州建网站
  • jsp网站建设期末作业搜索引擎优化的定义是什么