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

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

1
2
3
4
5
6
7
8
9
10
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语言的时间函数,他可以用于计算精度和纳秒。

语法:

1
2
3
#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花费的时间
1
2
3
4
5
6
7
8
9
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:

1
2
3
4
5
6
7
8
9
10
11
12
#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。

1
2
3
4
5
6
7
8
9
10
11
#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():

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#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())

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#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

1
2
3
4
5
6
7
8
9
10
11
struct tm {
    int tm_sec;  /* 秒 – 取值区间为[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; /* 星期 – 取值区间为[0,6],其中0代表星期天,1代表星期一 */
    int tm_yday; /* 从每年1月1日开始的天数– 取值区间[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

例:

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

Linux下用C获取当前时间的更多相关文章

  1. linux 下的clock_gettime() 获取精确时间函数

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

  2. perl在linux下通过date获取当前时间

    perl处理文件的时候最好添加上 处理的时间戳,获取系统的时间又多种方法,但是反引号是最原始的,不需要其他外界条件和lib的支持. my $now = `date "+%F %T" ...

  3. Linux下文件的三个时间意义及用法

    Linux下文件的三个时间参数: (1)modification time(mtime):内容修改时间        这里的修改时间指的是文件的内容发生变化,而更新的时间. (2)change tim ...

  4. 【Linux基础】linux下修改ls显示的时间格式

    1.修改ls显示格式 ls -l --time-style '+%Y/%m/%d %H:%M:%S' drwxr-x--- edwetl edwetl // :: arc_test ls -l --t ...

  5. linux下dmidecode命令获取硬件信息

    linux下dmidecode命令获取硬件信息 2 A+ 所属分类:Linux 运维工具 dmidecode在 Linux 系统下获取有关硬件方面的信息.dmidecode 遵循 SMBIOS/DMI ...

  6. Linux下文件的三种时间标记(atime ctime mtime)

    在windows下,一个文件有:创建时间.修改时间.访问时间. 在Linux下,一个文件有:状态改动时间.修改时间.访问时间. 1)查看文件(或文件夹)的三种时间标记 (stat 命令) Access ...

  7. Linux下onvif客户端获取ipc摄像头 GetServices:获取媒体地址(有的h265摄像头必须要这个接口)

    GetServices:获取媒体地址(有些h265的摄像头必须用到这个接口,得到获取能力时没获取到的另一个媒体地址) 鉴权:但是在使用这个接口之前是需要鉴权的.ONVIF协议规定,部分接口需要鉴权,部 ...

  8. Linux下onvif客户端获取ipc摄像头 GetStreamUri:rtsp地址(h264、h265)

    GetStreamUri:rtsp地址 鉴权:但是在使用这个接口之前是需要鉴权的.ONVIF协议规定,部分接口需要鉴权,部分接口不需要鉴权,在调用需要鉴权的接口时不使用鉴权,会导致接口调用失败.实现鉴 ...

  9. Linux下onvif客户端获取ipc摄像头 获取能力:GetCapabilities

    GetCapabilities:获取能力,主要目的获取设备能力信息(获取媒体服务地址) 鉴权:但是在调用获取设备能力之前是需要鉴权的.ONVIF协议规定,部分接口需要鉴权,部分接口不需要鉴权,在调用需 ...

随机推荐

  1. 【转】【Html】Vuejs2.0学习之二(Render函数,createElement,vm.$slots,函数化组件,模板编译,JSX)

    1.Render函数 所以直接来到Render,本来也想跳过,发现后面的路由貌似跟它还有点关联.先来看看Render 1.1 官网一开始就看的挺懵的,不知道讲的是啥,动手试了一下,一开头讲的是Rend ...

  2. Aizu_Insertion Sort

    原题链接:https://vjudge.net/problem/Aizu-ALDS1_1_A 题目描述 Write a program of the Insertion Sort algorithm ...

  3. Eclipse初次java开发问题总结-4-Maven使用问题汇总

    Non-resolvable parent POM [INFO] Scanning for projects... [ERROR] The build could not read 1 project ...

  4. C# ?? 运算符是什么?

    ?? 运算符定义在将可空类型分配给非可空类型时返回的默认值. int? c = null; //若 c 为 null,则 d 为 -1,否则把 c 值赋予 dint d = c ?? -1;

  5. Spring Boot 8080端口被占用抛出异常

    问题: SpringBoot------8080端口被占用抛出异常 解决: 进到项目下这两个文件,添加“server.port=8888”即可

  6. 使用jenkins配置.net mvc网站进行持续集成

    最近好久没有更新文章了,因为好久没有写代码了,以至于我不知道同大家分享些什么,刚好,今天突然叫我学习下jenkins每日构建,我就把今天的学习笔记记录下来,这其中很多东西都是公司同事之前调研总结的,我 ...

  7. 模式识别之检索---Bag of visual word(词袋模型)

    visual words 视觉单词 http://blog.csdn.net/v_july_v/article/details/8203674 http://blog.csdn.net/pi9nc/a ...

  8. 如何使用Matrix对bitmap的旋转与镜像水平垂直翻转

    Bitmap convert(Bitmap a, int width, int height){int w = a.getWidth();int h = a.getHeight();Bitmap ne ...

  9. WEB网页监控系统的设计框架思路具体解释

    提示:也能够直接在LCD上显示摄像头数据.这个參考luvcview源代码.设计思路思将YUV或者MJPEG格式的数据转换成RGB格式的数据,然后实现图片的缩放,图像缩放算法:点击这里!,然后写到fra ...

  10. interproscan 软件对序列进行GO 注释

    interproscan 软件实际上将对输入的查询序列和interpro 数据库中的序列去比对,将比对上的序列对应的GO信息作为查询序列的GO注释 在interpro 数据库中,每条蛋白质序列有一个唯 ...