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
上程序: #include <cstdlib> #include <iostream> #include <time.h> #include <stdio.h ...
- libc中的标准函数 localtime和localtime_r 的用法
http://baike.baidu.com/view/1080853.htm 随便一查,就可以查到基本用法,但是... http://blog.csdn.net/maocl1983/article/ ...
- localtime 和 localtime_r
#include <cstdlib> #include <iostream> #include <time.h> #include <stdio.h> ...
- localtime 和 localtime_r 的区别
转自:http://blog.csdn.net/maocl1983/article/details/6221810 #include <cstdlib> #include <iost ...
- localtime死锁——多线程下fork子进程
近期測试我们自己改进的redis,发如今做rdb时,子进程会一直hang住.gdb attach上.堆栈例如以下: (gdb) bt #0 0x0000003f6d4f805e in __lll_lo ...
- Visual studio 2015程序转Eclipse gun编译出现的问题总结
Visual studio 2015程序转Eclipse gun编译出现的问题总结 1.C++11支持 1)Project settings project右键-> c/c++ build -& ...
- C++中的时间函数
C++获取时间函数众多,何时该用什么函数,拿到的是什么时间?该怎么用?很多人都会混淆. 本文是本人经历了几款游戏客户端和服务器开发后,对游戏中时间获取的一点总结. 最早学习游戏客户端时,为了获取最精确 ...
- [转贴]实践:C++平台迁移以及如何用C#做C++包装层
终于有个C++ 如何调用C#类库的文章,收藏之 在前面,我们看过OpenTK与MOgre,这二个项目都是C#项目,但是他的实现都是C++.他们简单来说就是一个包装层.常见的包装方式有二种,一 种就是我 ...
- C++ —— 时间与日期
导读 在平时编程中有时需要获取当前的时间或者日期,然而不同的平台不同的场景下,有时使用的API也不尽相同.一般来说,C/C++中关于时间的标准库函数在不同的平台的都可以使用,可一些与平台相关的函数就只 ...
随机推荐
- imp与impdp比较
impdp和expdp是oracle 10g及以上版本才带的命令,目的是替换imp和exp命令,但为了向后兼容,故后面命令在高版本中依然可以使用. 但imp和exp在处理跨版本的导入导出时很麻烦,而i ...
- ValueError: Expecting property name: line 1 column 1 (char 1)
# -*- coding: cp936 -*- #xiaodeng #python 2.7.10 import weibo s='{"name":"xiaodeng&qu ...
- java 生成可执行jar包
jar -cvfm my.jar [配置主函数入口文件] [包] Main-Class: 包名.类名 注意“:”后边有一个空格,类名后边要有回车换行
- 【mysql+RBAC】RBAC权限处理(转载:http://www.cnblogs.com/xiaoxi/p/5889486.html 平凡希)
1.这里我只讲核心,mysql查询语句:FIND_IN_SET(str,strlist) 2.具体教程可以参考[童攀老师的RBAC],很清晰,赞一个. 3.详解:mysql的find_in_set 首 ...
- Java中网络相关API的应用——InetAddress&URL
一.InetAddress类 标识网络上的硬件资源 package com.homework; import java.net.InetAddress; import java.net.Unknown ...
- 戴尔大力宣传Ubuntu 对比与Windows的差异
2010-06-18 10:58:36 11175 人阅读 作者:萧萧 编辑:萧萧[爆料] 评论(46) 戴尔近日上线了一个新的网页,对比Linux开源系统(主要是Ubuntu)与Win ...
- PL/SQL配置oracle客户端,登录远程数据库配置
本地未安装Oracle数据库,但又想使用PL/SQL连接服务器端的数据库. 1.新建NETWORK文件夹, 在该文件夹下新建ADMIN文件夹, 在该文件夹下新建tnsnames.ora文件(拷贝下面的 ...
- 【LeetCode】142. Linked List Cycle II (2 solutions)
Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...
- shell脚本条件判断
http://blog.csdn.net/ws_zll/article/details/7515310
- iOS - Analyze 静态分析
1.Analyze 使用 Xcode 自带的静态分析工具 Product -> Analyze(快捷键 command + shift + B)可以找出代码潜在错误,如内存泄露,未使用函数和变量 ...