侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

Linux下用C获取当前时间

linux  /  管理员 发布于 7年前   169

Linux下用C获取当前时间,具体如下:

代码(可以把clock_gettime换成time(NULL))

void getNowTime(){ timespec time; clock_gettime(CLOCK_REALTIME, &time); //获取相对于1970到现在的秒数 tm nowTime; localtime_r(&time.tv_sec, &nowtime); char current[1024]; sprintf(current, "%04d%02d%02d%02d:%02d:%02d", nowTime.tm_year + 1900, nowTime.tm_mon, nowTime.tm_mday,   nowTime.tm_hour, nowTime.tm_min, nowTime.tm_sec);}

分析:

clock_gettime()

 函数"clock_gettime"是基于Linux C语言的时间函数,他可以用于计算精度和纳秒。

语法:

#include<time.h>int clock_gettime(clockid_t clk_id,struct timespec *tp);

参数:

clk_id : 检索和设置的clk_id指定的时钟时间。

CLOCK_REALTIME:系统实时时间,随系统实时时间改变而改变,即从UTC1970-1-1 0:0:0开始计时,中间时刻如果系统时间被用户改成其他,则对应的时间相应改变

  •   CLOCK_MONOTONIC:从系统启动这一刻起开始计时,不受系统时间被用户改变的影响
  •   CLOCK_PROCESS_CPUTIME_ID:本进程到当前代码系统CPU花费的时间
  •   CLOCK_THREAD_CPUTIME_ID:本线程到当前代码系统CPU花费的时间
struct timespec{time_t tv_sec; /* 秒*/long tv_nsec; /* 纳秒*/};

localtime()

localtime是 把从1970-1-1零点零分到当前时间系统所偏移的秒数时间转换为本地时间.

语法

说明:此函数获得的tm结构体的时间是日历时间。

用 法: struct tm *localtime(const time_t *clock);

返回值:返回指向tm 结构体的指针.tm结构体是time.h中定义的用于分别存储时间的各个量(年月日等)的结构体.

例1:

#include <stdio.h>#include <stddef.h>#include <time.h>int main(void){ time_t timer;//time_t就是long int 类型 struct tm *tblock; timer = time(NULL); tblock = localtime(&timer); printf("Local time is: %s\n", asctime(tblock)); return 0;}

执行结果:

Local time is: Mon Feb 16 11:29:26 2009

例2:

上面的例子用了asctime函数,下面这个例子不使用这个函数一样能获取系统当前时间。需要注意的是年份加上1900,月份加上1。

#include<time.h>#include<stdio.h>int main(){ struct tm *t; time_t tt; time(&tt); t = localtime(&tt); printf("%4d年%02d月%02d日 %02d:%02d:%02d\n", t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec); return 0;}

localtime()与localtime_r()的区别

localtime():

#include <cstdlib> #include <iostream> #include <time.h> #include <stdio.h>  using namespace std;  int main(int argc, char *argv[]) {  time_t tNow =time(NULL);  time_t tEnd = tNow + 1800;  //注意下面两行的区别  struct tm* ptm = localtime(&tNow);  struct tm* ptmEnd = localtime(&tEnd);   char szTmp[50] = {0};  strftime(szTmp,50,"%H:%M:%S",ptm);  char szEnd[50] = {0};  strftime(szEnd,50,"%H:%M:%S",ptmEnd);     printf("%s /n",szTmp);  printf("%s /n",szEnd);     system("PAUSE");  return EXIT_SUCCESS; }

最后出来的结果是:

21:18:39

21:18:39

和最初想法不一致。

查阅localtime的文档,发现这段话:

This structure is statically allocated and shared by the functions gmtime and localtime. Each time either one of these functions is called the content of this structure is overwritten.

也就是说每次只能同时使用localtime()函数一次,要不就会被重写!

The localtime() function need not be reentrant. A function that is not required to be reentrant is not required to be thread-safe.

因此localtime()不是可重入的。同时libc里提供了一个可重入版的函数localtime_r();

Unlike localtime(), the reentrant version is not required to set tzname。

修改程序:(localtime_r())

#include <cstdlib> #include <iostream> #include <time.h> #include <stdio.h>  using namespace std;  int main(int argc, char *argv[]) {  time_t tNow =time(NULL);  time_t tEnd = tNow + 1800;   //在这里修改程序  //struct tm* ptm = localtime(&tNow);  //struct tm* ptmEnd = localtime(&tEnd);  struct tm ptm = { 0 };  struct tm ptmEnd = { 0 };  localtime_r(&tNow, &ptm);  localtime_r(&tEnd, &ptmEnd);    char szTmp[50] = {0};  strftime(szTmp,50,"%H:%M:%S",&ptm);  char szEnd[50] = {0};  strftime(szEnd,50,"%H:%M:%S",&ptmEnd);  printf("%s /n",szTmp);  printf("%s /n",szEnd);     system("PAUSE");  return EXIT_SUCCESS; }

最后出来的结果是:

10:29:06
10:59:06

tm

 struct tm {     int tm_sec;  /* 秒 C 取值区间为[0,59] */      int tm_min;  /* 分 - 取值区间为[0,59] */      int tm_hour;  /* 时 - 取值区间为[0,23] */      int tm_mday;  /* 一个月中的日期 - 取值区间为[1,31] */      int tm_mon;  /* 月份(从一月开始,0代表一月) - 取值区间为[0,11] */      int tm_year;  /* 年份,其值等于实际年份减去1900 */      int tm_wday; /* 星期 C 取值区间为[0,6],其中0代表星期天,1代表星期一 */      int tm_yday; /* 从每年1月1日开始的天数C 取值区间[0,365],其中0代表1月1日 */      int tm_isdst; /* 夏令时标识符,夏令时tm_isdst为正;不实行夏令时tm_isdst为0 */    };

time 函数

返回:1970-1-1, 00:00:00以来经过的秒数

原型: time_t time(time_t *calptr) 

结果可以通过返回值,也可以通过参数得到,见实例

头文件 <time.h>

返回值: 

成功:秒数,从1970-1-1,00:00:00 可以当成整型输出或用于其它函数

失败:-1

例:

 time_t now; time(&now);// 等同于now = time(NULL) printf("now time is %d\n", now);

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


  • 上一条:
    VMware下CentOS6.4网卡设置为桥接模式静态IP配置方法详解
    下一条:
    VMware 10 中为CentOS 7添加多网卡并重命名
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在Linux系统中使用Iptables实现流量转发功能流程步骤(0个评论)
    • vim学习笔记-入门级需要了解的一些快捷键(0个评论)
    • 在centos7系统中实现分区并格式化挂载一块硬盘到/data目录流程步骤(0个评论)
    • 在Linux系统种查看某一个进程所占用的内存命令(0个评论)
    • Linux中grep命令中的10种高级用法浅析(0个评论)
    • 近期文章
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-11
    • 2017-07
    • 2017-10
    • 2017-11
    • 2018-01
    • 2018-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-03
    • 2022-04
    • 2022-08
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-06
    • 2023-07
    • 2023-10
    • 2023-12
    • 2024-01
    • 2024-04
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客