localtime、localtime_s、localtime_r的使用
(1)、localtime用来获取系统时间,精度为秒
#include <stdio.h>
#include <time.h>int main()
{time_t time_seconds = time(0);struct tm* now_time = localtime(&time_seconds);printf("%d-%d-%d %d:%d:%d\n", now_time->tm_year + 1900, now_time->tm_mon + 1,now_time->tm_mday, now_time->tm_hour, now_time->tm_min,now_time->tm_sec);
}
函数原型为struct tm *localtime(const time_t * timep)
需要包含头文件:#include <time.h>
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日,1代表1月2日,以此类推 */
int tm_isdst; /* 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的进候,tm_isdst为0;不了解情况时,tm_isdst()为负。*/
(2)localtime_r也是用来获取系统时间,运行于linux平台下
函数原型为struct tm *localtime_r(const time_t *timep, struct tm *result);
#include <stdio.h>
#include <time.h>int main()
{time_t time_seconds = time(0);struct tm now_time;localtime_r(&time_seconds, &now_time);printf("%d-%d-%d %d:%d:%d\n", now_time.tm_year + 1900, now_time.tm_mon + 1,now_time.tm_mday, now_time.tm_hour, now_time.tm_min, now_time.tm_sec);
}
(3)localtime_s也是用来获取系统时间,运行于windows平台下,与localtime_r只有参数顺序不一样
#include <iostream>
#include <time.h>int main()
{time_t time_seconds = time(0);struct tm now_time;localtime_s(&now_time,&time_seconds);printf("%d-%d-%d %d:%d:%d\n", now_time.tm_year + 1900, now_time.tm_mon + 1,now_time.tm_mday, now_time.tm_hour, now_time.tm_min, now_time.tm_sec);
}
会什么有了localtime还要有其他两个函数呢,因为localtime并不是线程安全的,观察localtime和localtime_r的调用发现,localtime在使用时,我们只需定义一个指针,并不需要为指针申请空间,而指针必须要指向内存空间才可以使用,其实申请空间的动作由函数自己完成,这样在多线程的情况下,如果有另一个线程调用了这个函数,那么指针指向的struct tm结构体的数据就会改变。
在localtime_s与localtime_r调用时,定义的是struct tm的结构体,获取到的时间已经保存在struct tm中,并不会受其他线程的影响。
localtime、localtime_s、localtime_r的使用相关推荐
- localtime和localtime_r
C:时间函数 localtime localtime_r localtime 和 localtime_r 的函数功能: converts the calendar time timep to brok ...
- localtime与localtime_r
在写代码的时候,经常会用到读取系统时间的函数.很多人都会调用localtime函数来将时间转换本地时间,但是大家往往会忽略了一点,localtime函数不是线程安全的.如果在多线程里调用localti ...
- localtime 和 localtime_r
上程序: #include <cstdlib> #include <iostream> #include <time.h> #include <stdio.h ...
- 二十 关于gmtime、gmtime_r、localtime、localtime_r
测试环境:vmware 7 + Redhat5.5,系统时间使用UTC,时区为上海. 1.函数功能介绍 使用man gmtime或man localtime都可以的得到这几个函数的介绍.原型如下: s ...
- Linux下gmtime、gmtime_r、localtime、localtime_r函数详解
函数功能介绍 使用man gmtime或man localtime都可以的得到这几个函数的介绍.原型如下: struct tm *gmtime(const time_t *timep); struct ...
- Linux时间函数time()、ctime()、ctime_r()、localtime()、localtime_r()、asctime()、strftime()的转换关系
上面这个图就是它们所有函数的关系转换,比较清晰,不需要太多文字描述. 下面是它们的代码实现过程: #include<stdio.h> #include<unistd.h> #i ...
- Linux gmtime、gmtime_r、localtime、localtime_r介绍
测试环境:vmware 7 + Redhat5.5,系统时间使用UTC,时区为上海. 1.函数功能介绍 使用man gmtime或man localtime都可以的得到这几个函数的介绍.原型如下: s ...
- C:时间函数 localtime localtime_r
C:时间函数 localtime localtime_r localtime 和 localtime_r 的函数功能: converts the calendar time timep to brok ...
- localtime死锁——多线程下fork子进程
最近测试我们自己改进的redis,发现在做rdb时,子进程会一直hang住,gdb attach上,堆栈如下: (gdb) bt #0 0x0000003f6d4f805e in __lll_lock ...
最新文章
- 浅析大规模DDOS防御架构:应对T级攻防
- Linux makefile中的= := ?=操作符
- vuex的使用和封装
- 数据结构笔记(二十八)-- 图的广度优先遍历
- android realm jar包,android-Realm支持maven或拥有jar的最新资源
- Spring-自定义BeanName生成规则
- 数值方法求积分 详解+模板代码
- 伺服电机算功率基本公式_伺服电机选型通用计算公式
- 安卓屏幕朗读app_8个非常好用的黑科技App
- EXCEL中数据分析涉及的一些操作
- ARCH模型的R语言实现
- q_i7p_co/index_php rmvb_某返利网站admin目录index.php文件混淆加密算法分析
- 【Simulink专题】Simulink模型设置(三):代码生成
- 爱心代码表白(可直接复制运行)
- 【教程】Ubuntu安装、使用gephi
- 微信小程序的安全登录
- 苹果亮度突然变暗_安卓手机体验真的超越苹果了吗?至少这一点上,iPhone还是领先...
- DS18b20温度值换算
- 贝叶斯统计 韦来生 课后题答案 第二章
- TCL爱奇艺再次联姻 共同打造国际微电影节
热门文章
- 微软surface屏幕测试软件,Soomal作品 - Microsoft 微软 Surface Go平板电脑屏幕测评报告 [Soomal]...
- android rom包修改工具,自己修改安卓的ROM包(非官方) | 寒山烟雨
- 模电学习第一天--PN结梳理
- P3224 [HNOI2012]永无乡 FHQ-Treap 启发式合并
- 华为芯片与器件设计工程师_华为芯片与器件设计工程师(实习)一、二面
- vhost-user
- 关于跨境支付,你了解多少
- 使用ESP8266数据上onenet云平台
- Linux管道到底能有多快?
- 清华计算机408考研真题资料经验分享