C/C++获取系统当前时间
time_t time (time_t* timer);
获取系统当前日历时间 UTC 1970-01-01 00:00:00开始的unix时间戳
参数:timer 存取结果的时间指针变量,类型为time_t,指针变量可以为null。如果timer指针非null,则time()函数返回值变量与timer指针一样,都指向同一个内存地址;否则如果timer指针为null,则time()函数返回一个time_t变量时间。
返回值,如果成功,获取当前系统日历时间,否则返回 -1。
二、结构体 struct tm
变量 | 类型 | 说明 | 范围 |
tm_sec | int | 每分钟的秒数 | [0 - 61] |
tm_min | int | 每小时后面的分钟数 | [0 - 59] |
tm_hour | int | 凌晨开始的小时数 | [0 - 23] |
tm_mday | int | 从每月份开始算的天数 | [1 - 31] |
tm_mon | int | 从一月份开始的月份数 | [0 - 11] |
tm_year | int | 从1900年开始的年数 | |
tm_wday | int | 从每周天开始算的天数 | [0 - 6] |
tm_yday | int | 一年的第几天,从零开始 | [0 - 365] |
tm_isdst | int | 夏令时 | |
这里有几个地方要注意:
1. tm_sec 在C89的范围是[0-61],在C99更正为[0-60]。通常范围是[0-59],只是某些系统会出现60秒的跳跃。
2. tm_mon 是从零开始的,所以一月份为0,十二月份为11。
三、本地时间转换函数localtime(time_t*)
函数原型
struct tm * localtime (const time_t * timer);
将日历时间转换为本地时间,从1970年起始的时间戳转换为1900年起始的时间数据结构
四、源码及编译
current_time.cpp
#include <cstdio>
#include <ctime> int main(int argc, char* argv[]) {
time_t rawtime;
struct tm *ptminfo; time(&rawtime);
ptminfo = localtime(&rawtime);
printf("current: %02d-%02d-%02d %02d:%02d:%02d\n",
ptminfo->tm_year + 1900, ptminfo->tm_mon + 1, ptminfo->tm_mday,
ptminfo->tm_hour, ptminfo->tm_min, ptminfo->tm_sec);
return 0;
} 编译及运行
$ g++ current_time.cpp
$ ./a.out
current: 2017-07-26 23:32:46
C/C++获取系统当前时间的更多相关文章
- java获取系统指定时间年月日
java获取系统指定时间年月日 private String setDateTime(String falg) { Calendar c = Calendar.getInstance(); c.set ...
- Unity3D获取系统当前时间,并格式化显示
Unity 获取系统当前时间,并格式化显示.通过“System.DateTime”获取系统当前的时间,然后通过格式化把获得的时间格式化显示出来,具体如下: 1.打开Unity,新建一个空工程,Unit ...
- Oracle,MySQL,sqlserver三大数据库如何获取系统当前时间
Oracle中如何获取系统当前时间:用SYSDATE() MySQL中获取系统当前时间主要有以下几点: (1)now()函数以('YYYY-MM-dd HH:mm:SS')返回当前的日期时间,可以直接 ...
- java 获取系统当前时间并格式化
java 获取系统当前时间并格式化 CreateTime--2018年5月9日11:41:00 Author:Marydon 实现方式有三种 updateTime--2018年7月23日09点32 ...
- 使用js时,如何获取系统当前时间并且得到格式为"yyyy年MM月"的日期
1.使用js时,如何获取系统当前时间并且得到格式为"yyyy年MM月"的日期: 1 var newdate = new Date(); 2 var nowyear = newdat ...
- C++ 获取系统当前时间(日历时)
获取系统当前时间(日历时) //Linux & C++11 #include <chrono> #include <ctime> using namespace std ...
- android service 样例(电话录音和获取系统当前时间)
关于android service 的具体解释请參考: android四大组件--android service具体解释.以下将用两个实例具体呈现Android Service的两种实现. 一个是st ...
- C# 获取系统开机时间
原文:C# 获取系统开机时间 /// /// 获取系统开机时间 /// /// private DateTime GetComput ...
- T_SQL 获取系统当前时间与明天时间的两种格式
--获取系统明天的时间 select CONVERT(nvarchar(20),dateadd(d,1,getdate()),120) 2017-01-21 15:04:10 sele ...
随机推荐
- Windows 虚拟机 VM
VMware是全球台式电脑及资料中心虚拟化解决方案的领导厂商.VMWare Workstation是该公司出品的“虚拟 PC”软件(即:大家常说的“虚拟机”),通过它可在一台电脑上同时运行更多的Mic ...
- 一次完整的HTTP请求与响应涉及哪些知识?
Java技术栈 www.javastack.cn 优秀的Java技术公众号 作者:Ruheng 地址:http://www.jianshu.com/p/c1d6a294d3c0 本文以HTTP请求和响 ...
- 20.multi_case01
# 多进程,使用Pool from multiprocessing import Pool def f(x): return x*x if __name__ == '__main__': p = Po ...
- spring boot中使用javax.validation以及org.hibernate.validator校验入参
这里springboot用的版本是:<version>2.1.1.RELEASE</version> 自带了hibernate.validator,所以不用添加额外依赖 1.创 ...
- 有关Tensorboard问题
先说我的各个版本: 操作系统: win7 64 Python: 3.5 Tensorflow: 1.2 Tensorboard: 1.6 错误一: 只显示Graphs,不显示Histogram和Sca ...
- Activit单元i测试(与spring集成测试)
1.测试 eclipse下安装activiti插件以及maven 右键新建activiti project(这时会自动创建pom依赖以及activiti.cfg.xml,但还不是maven项目) 选中 ...
- iOS开发系列-文件下载
小文件下载 NSURLConnection下载小文件 #import "ViewController.h" @interface ViewController ()<NSUR ...
- UMP系统架构 Controller服务器
- (转)行为树(Behavior Tree)实践(1)– 基本概念
通过一个例子来介绍一下行为树的基本概念,会比较容易理解,看下图: 这是我们为一个士兵定义的一颗行为树(可以先不管这些绿圈和红圈是干吗的),首先,可以看到这是一个树形结构的图,有根节点,有分支,而且子节 ...
- 07.07NOIP模拟赛
考中 考试时不知道自己在想啥.. 拿到第一题:woc组合数学,第二题:woc组合数学,第三题,woc组合数学. 然后开始认真读题…… 我tm真是闲的... 第一题是15年山东省选题,感觉暴力搜索都没法 ...