(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的使用的更多相关文章

  1. localtime和localtime_r

    上程序: #include <cstdlib> #include <iostream> #include <time.h> #include <stdio.h ...

  2. libc中的标准函数 localtime和localtime_r 的用法

    http://baike.baidu.com/view/1080853.htm 随便一查,就可以查到基本用法,但是... http://blog.csdn.net/maocl1983/article/ ...

  3. localtime 和 localtime_r

    #include <cstdlib> #include <iostream> #include <time.h> #include <stdio.h> ...

  4. localtime 和 localtime_r 的区别

    转自:http://blog.csdn.net/maocl1983/article/details/6221810 #include <cstdlib> #include <iost ...

  5. localtime死锁——多线程下fork子进程

    近期測试我们自己改进的redis,发如今做rdb时,子进程会一直hang住.gdb attach上.堆栈例如以下: (gdb) bt #0 0x0000003f6d4f805e in __lll_lo ...

  6. Visual studio 2015程序转Eclipse gun编译出现的问题总结

    Visual studio 2015程序转Eclipse gun编译出现的问题总结 1.C++11支持 1)Project settings project右键-> c/c++ build -& ...

  7. C++中的时间函数

    C++获取时间函数众多,何时该用什么函数,拿到的是什么时间?该怎么用?很多人都会混淆. 本文是本人经历了几款游戏客户端和服务器开发后,对游戏中时间获取的一点总结. 最早学习游戏客户端时,为了获取最精确 ...

  8. [转贴]实践:C++平台迁移以及如何用C#做C++包装层

    终于有个C++ 如何调用C#类库的文章,收藏之 在前面,我们看过OpenTK与MOgre,这二个项目都是C#项目,但是他的实现都是C++.他们简单来说就是一个包装层.常见的包装方式有二种,一 种就是我 ...

  9. C++ —— 时间与日期

    导读 在平时编程中有时需要获取当前的时间或者日期,然而不同的平台不同的场景下,有时使用的API也不尽相同.一般来说,C/C++中关于时间的标准库函数在不同的平台的都可以使用,可一些与平台相关的函数就只 ...

随机推荐

  1. python之函数用法vars()

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法vars() #vars() #说明:返回对象object的属性和属性值的字典对象 ' ...

  2. 用jquery写的校验用户名

    $(function(){ $("input[name='username']").blur(function(){ var uname = $(this).val(); cons ...

  3. Linux-JAVA-JDK完整配置过程

        JDK是目前很流行的开发工具包,内含jre是支撑jvm运行的组件. 01.jdk下载 http://www.oracle.com/technetwork/java/javase/downloa ...

  4. cpuinfo详解

    cat /proc/cpuinfo processor: 23:超线程技术的虚拟逻辑核第24个   ###一般看最后一个0...23 表示24线程 vendor_id: GenuineIntel:CP ...

  5. oracle-imp导入小错filesize设置

    ***********************************************声明*************************************************** ...

  6. PHP递归目录的5种方法

    <?php //方法一:使用glob循环 function myscandir1($path, &$arr) { foreach (glob($path) as $file) { if ...

  7. 获取网站资源 getResourceAsStream

    获取网站资源(重点) public void doGet(HttpServletRequest request, HttpServletResponse response)throws Servlet ...

  8. js:获取节点相关的 nodeName,nodeType,nodeValue

    getElementById() getElementsByName() getElementsByTagName() hasChildNodes() nodeName nodeType=1元素节点/ ...

  9. 主流ETL(Extract-Transform-Load)工具选型,Kettle Spoon、Datastage、Powercenter介绍

    参考:三大主流ETL工具选型 ETL工具 Kettle Spoon 开源ETL工具,所以免费,用java开发的. Ascential公司的Datastage(在2005年被IBM收购现在是 IBM 的 ...

  10. Hadoop Map/Reduce教程

    原文地址:http://hadoop.apache.org/docs/r1.0.4/cn/mapred_tutorial.html 目的 先决条件 概述 输入与输出 例子:WordCount v1.0 ...