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

 void getNowTime()
{
timespec time;
clock_gettime(CLOCK_REALTIME, &time); //获取相对于1970到现在的秒数
tm nowTime;
localtime_r(&time.tv_sec, &nowtime);
char current[];
sprintf(current, "%04d%02d%02d%02d:%02d:%02d", nowTime.tm_year + , nowTime.tm_mon+1, 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 ;
}
执行结果:
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 + , t->tm_mon + , t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
return ;
}

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 + ;
//注意下面两行的区别
struct tm* ptm = localtime(&tNow);
struct tm* ptmEnd = localtime(&tEnd); char szTmp[] = {};
strftime(szTmp,,"%H:%M:%S",ptm);
char szEnd[] = {};
strftime(szEnd,,"%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 + ; //在这里修改程序
//struct tm* ptm = localtime(&tNow);
//struct tm* ptmEnd = localtime(&tEnd);
struct tm ptm = { };
struct tm ptmEnd = { };
localtime_r(&tNow, &ptm);
localtime_r(&tEnd, &ptmEnd); char szTmp[] = {};
strftime(szTmp,,"%H:%M:%S",&ptm);
char szEnd[] = {};
strftime(szEnd,,"%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; /* 秒 – 取值区间为[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

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);

转载请注明出处:http://www.cnblogs.com/fnlingnzb-learner/p/5985822.html

 

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

  1. Mac/IOS/linux获取当前时间包含微秒毫秒的代码

    #include <sys/time.h> 1 struct UnityLocalTimeStat { int Year; int Month; int DayOfWeek; int Da ...

  2. django 获取系统当前时间 和linux 系统当前时间不一致 问题处理。

    问题场景: 在django admin models 实体对象添加一个属性最后修改时间,用户在添加.修改是系统自动修改操作时间. UpdateTime自动获取系统时间.并且自动修改. 代码设置如下. ...

  3. Linux C 语言 获取系统时间信息

    比如获取当前年份:        /* 获取当前系统时间 暂时不使用        int iyear = 0;        int sysyear = 0;        time_t now;  ...

  4. [置顶] 获取系统时间的方法--linux

    一. localtime 函数获取(年/月/日/时/分/秒)数值. 1.感性认识 #include<time.h>     //C语言的头文件 #include<stdio.h> ...

  5. Linux下用C获取当前时间

    Linux下用C获取当前时间,具体如下: 代码(可以把clock_gettime换成time(NULL)) ? 1 2 3 4 5 6 7 8 9 10 void getNowTime() {  ti ...

  6. Linux C 获取系统时间信息

    比如获取当前年份:               /* 获取当前系统时间 暂时不使用 ; ; time_t now; struct tm *timenow; time(&now); timeno ...

  7. Linux驱动中获取系统时间

    最近在做VoIP方面的驱动,总共有16个FXS口和FXO口依次初始化,耗用的时间较多.准备将其改为多线程,首先需要确定哪个环节消耗的时间多,这就需要获取系统时间. #include <linux ...

  8. Linux设备驱动程序 之 获取当前时间

    墙上时间 内核一般通过jiffies来获取当前时间,该数值表示的是最近一次系统启动到当前的时间间隔,它和设备驱动程序无关,因为它的声明期只限于系统的运行期:但是驱动程序可以用jiffies来计算不同事 ...

  9. linux c获取本地时间

    在标准C/C++中,我们可通过tm结构来获得日期和时间,tm结构在time.h中的定义如下: #ifndef _TM_DEFINED struct tm { int tm_sec; /* 秒–取值区间 ...

随机推荐

  1. ubuntu16.04 更换源

    1.备份 sudo cp /etc/apt/source.list /etc/apt/source.list.bak 2.打开/etc/apt/source.list,并删除所有内容 sudo ged ...

  2. R语言实战(十)处理缺失数据的高级方法

    本文对应<R语言实战>第15章:处理缺失数据的高级方法 本文仅在书的基础上进行简单阐述,更加详细的缺失数据问题研究将会单独写一篇文章. 处理缺失值的一般步骤: 识别缺失数据: 检查导致数据 ...

  3. [CodeForces 893D] Credit Card 贪心

    题意: Recenlty Luba有一张信用卡,一开始金额为0,每天早上可以充值任意数量的钱,但有限制,卡里的钱不能超过D.到了晚上,银行会对信用卡进行一次操作,操作有三种: 1.a[i]>0, ...

  4. JAVAEE——ssm综合练习:CRM系统(包含ssm整合)

    1 CRM项目外观   1. 开发环境 IDE: Eclipse Mars2 Jdk: 1.7 数据库: MySQL 2. 创建数据库 数据库sql文件位置如下图: 创建crm数据库,执行sql 效果 ...

  5. load Properties

    /* */ public static final Properties loadProperties(String propertyFileRelativePath) /* */ { /* 67 * ...

  6. ICMP隧道工具ptunnel

    ICMP隧道工具ptunnel   在一些网络环境中,如果不经过认证,TCP和UDP数据包都会被拦截.如果用户可以ping通远程计算机,就可以尝试建立ICMP隧道,将TCP数据通过该隧道发送,实现不受 ...

  7. springBoot 自动配置原理

    在之前文章中说过,springBoot会根据jar包去添加许多的自动配置,本文就来说说为什么会自动配置,自动配置的原理时什么? springBoot在运行SpringApplication对象实例化时 ...

  8. BZOJ.4919.[Lydsy1706月赛]大根堆(线段树合并/启发式合并)

    题目链接 考虑树退化为链的情况,就是求一个最长(严格)上升子序列. 对于树,不同子树间是互不影响的.仿照序列上的LIS,对每个点x维护一个状态集合,即合并其子节点后的集合,然后用val[x]替换掉第一 ...

  9. Educational Codeforces Round 46 (Div 2) (A~G)

    目录 Codeforces 1000 A.Codehorses T-shirts B.Light It Up C.Covered Points Count(差分) D.Yet Another Prob ...

  10. [Agc002E]Candy Piles

    [Agc002E]Candy Piles 题目大意 有\(n\)个数,两人轮流操作,可以做以下操作之一: 删掉一个最大的数 将所有数-1 最后取没的人输,问先手是否必胜? 试题分析 直接决策不知道选哪 ...