(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. JDK1.7的一些新特性

    整理了几条对开发可能用到概率高的 1.swicth支持对String的判断:(以前只能支持Int及以下的) switch (s) { case "1": break; case & ...

  2. (转)Content-Disposition的使用和注意事项

    最近不少Web技术圈内的朋友在讨论协议方面的事情,有的说web开发者应该熟悉web相关的协议,有的则说不用很了解.个人认为这要分层次来看待这个问 题,对于一个新手或者刚入门的web开发人员而言,研究协 ...

  3. 网线/双绞线上各标识CAT, AWG, PR, UTP/STP/FTP/SFTP的含义

    CAT5, CAT5e, CAT6 表示网线类别, 常见的有 CAT5, CAT5e, CAT6分别表示五类, 超五类, 六类网线 24AWG, 26AWG American Wire Gauge是美 ...

  4. 普通spring jsp+mybatis项目修改为springboot + jsp +mybatis项目

    概述 由于公司决定使用spring cloud,但是公司积累了大量的普通的jsp项目,老的项目直接全部修改为springboot成本过高,周期比较长,而且公司业务正在快速拓展,所以需要把之前的老项目修 ...

  5. 解释一下文件/etc/fstab的内容

    /etc/fstab 内容解释(偷个懒,把别人的话拷贝过来,做个标记,然后下班走人...)/dev/hda1 /mnt/c ntfs ro,users,gid=users,umask=0002,nls ...

  6. 开源的数据可视化JavaScript图表库:ECharts

    ECharts (Enterprise Charts 商业产品图表库)是基于HTML5 Canvas的一个纯Javascript图表库,提供直观,生动,可交互,可个性化定制的数据可视化图表.创新的拖拽 ...

  7. Linux 通过cron定期执行 php文件(转)

    Linux 通过cron定期执行 php文件 补充几点: 1. 要在php文件头加上解释器的路径,通常是 #!/usr/bin/php 2. 授予要执行的php文件执行权限   chmod a+x x ...

  8. JQuery 导入导出 Excel

    正在做一个小项目, 从数据库中查询数据放在 HTML Table 中. 现在想要从这个 table 中导出数据来. 另外用户需要选择导出的列. 使用 jQuery 的导出插件可以完成这个需求. jQu ...

  9. PowerDesigner删除外键关系,而不删除外键列[转]

    PowerDesigner中配置外键关系时,如果要删除配置的外键关系,默认设置会一同删除外键列. 要更改此设置,需在菜单栏tools中打开Model Options,在Model Settings中点 ...

  10. FA_手工明细增加固定资产(流程)

    2014-06-08 Created By BaoXinjian