C/C++ 获取系统时间 到秒 || 到毫秒
string getNowSysTime(string &outPut)
{
char szBuf[] = {};
struct timeval tv;
struct timezone tz;
struct tm *p; gettimeofday(&tv, &tz);
p = localtime(&tv.tv_sec);
snprintf(szBuf, , "%02d-%02d-%02d %02d:%02d:%02d", p->tm_year + , p->tm_mon + , p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec); return outPut = szBuf;
} string GetSysUsecTimeHMS()
{
char szBuf[] = {};
struct timeval tv;
struct timezone tz;
struct tm *p; gettimeofday(&tv, &tz);
p = localtime(&tv.tv_sec);
snprintf(szBuf, , "%02d-%02d-%02d %02d:%02d:%02d.%06ld", p->tm_year + , p->tm_mon + , p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec, tv.tv_usec); return szBuf;
}
其参数tv是保存获取时间结果的结构体,参数tz用于保存时区结果:
struct timezone{
int tz_minuteswest;/*格林威治时间往西方的时差*/
int tz_dsttime;/*DST 时间的修正方式*/
}
timezone 参数若不使用则传入NULL即可。
而结构体timeval的定义为:
struct timeval{
long int tv_sec; // 秒数
long int tv_usec; // 微秒数
}
它获得的时间精确到微秒(1e-6 s)量级。在一段代码前后分别使用gettimeofday可以计算代码执行时间:
struct timeval tv_begin, tv_end;
gettimeofday(&tv_begin, NULL);
foo();
gettimeofday(&tv_end, NULL);
函数执行成功后返回0,失败后返回-1,错误代码存于errno中。
C/C++ 获取系统时间 到秒 || 到毫秒的更多相关文章
- VC++ 获取系统时间、程序运行时间(精确到秒,毫秒)的五种方法
1.使用CTime类(获取系统当前时间,精确到秒) CString str; //获取系统时间 CTime tm; tm=CTime::GetCurrentTime();//获取系统日期 str=tm ...
- 【VS开发】VC++ 获取系统时间、程序运行时间(精确到秒,毫秒)的五种方法
1.使用CTime类(获取系统当前时间,精确到秒) CString str; //获取系统时间 CTime tm; tm=CTime::GetCurrentTime();//获取系统日期 str=tm ...
- Android获取系统时间方法的总结
Android获取系统时间方法的方法有很多种,常用的有Calendar.Date.currentTimeMills等方法. (1)Calendar Calendar获取系统时间首先要用Calendar ...
- 用PHP获取系统时间时,时间比当前时间少8个小时
自PHP5.0开始,用PHP获取系统时间时,时间比当前时间少8个小时.原因是PHP.ini中没有设置timezone时,PHP是使用的UTC时间,所以在中国时间要少8小时. 解决办法: 1.在PHP. ...
- C/C++获取系统时间
C/C++获取系统时间需要使用Windows API,包含头文件"windows.h". 系统时间的数据类型为SYSTEMTIME,可以在winbase.h中查询到如下定义: ty ...
- VC++编程中获取系统时间
<span style="white-space:pre"> </span>总结了在程序中如何获得系统时间的方法 void CGetSystenTimeDl ...
- c++ 怎样获取系统时间
c++ 怎样获取系统时间 2008-04-28 15:34 //方案— 长处:仅使用C标准库:缺点:仅仅能精确到秒级 #include <time.h> #include <stdi ...
- 【转】cocos2d-x获取系统时间——2013-08-25 10
欢迎转载,本帖地址:http://blog.csdn.net/jinjian2009/article/details/9449585 之前使用过cocos2d-x获取系统时间,毫秒级的 long ge ...
- c++ 如何获取系统时间 - zjnig711的信息仓库 - 博客频道 - CSDN.NET
c++ 如何获取系统时间 - zjnig711的信息仓库 - 博客频道 - CSDN.NET c++ 如何获取系统时间 分类: C/C++ 2008-05-08 22:15 14115人阅读 评论(5 ...
随机推荐
- Redis 数据结构之dict(2)
本文及后续文章,Redis版本均是v3.2.8 上篇文章<Redis 数据结构之dict>,我们对dict的结构有了大致的印象.此篇文章对dict是如何维护数据结构的做个详细的理解. 老规 ...
- Linux远程登录ssh免密码配置方法(仅供参考)
这篇文章主要介绍了linux远程登录ssh免密码配置方法,需要的朋友可以参考下(http://www.0834-3659999.com) 一.情景 公司刚上几台Linux,现在要把主机之间都能远程ss ...
- redis启动过程源码解析
redis整个程序的入口函数在server.c中的main函数,函数调用关系如下图1,调用顺序为从上到下,从左至右. 图1 redis启动函数调用图 main函数源码如下,1-55行根据配置文件和启动 ...
- Codeforces.871D.Paths(莫比乌斯反演 根号分治)
题目链接 \(Description\) 给定\(n\),表示有一张\(n\)个点的无向图,两个点\(x,y\)之间有权值为\(1\)的边当且仅当\(\gcd(x,y)\neq1\).求\(1\sim ...
- STS的安装以及IDEA安装和破解过程
一.STS的下载 1·下载地址:直接百度搜索STS,选择Download STS 3 2.选择电脑对应的版本 直接下载 3.安装包解压后在有jdk的情况下就可以直接使用 二.IDEA的安装 下载网址: ...
- [LeetCode] Insert into a Binary Search Tree 二叉搜索树中插入结点
Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert t ...
- PowerShell导出场中的WSP包到本地
Add-PSSnapin Microsoft.SharePoint.PowerShell –erroraction SilentlyContinue## setup our output direct ...
- VUE iview date-picker取时间范围...
x HTML <script src="//unpkg.com/vue/dist/vue.js"></script> <script src=&quo ...
- day23:类的命名空间和组合
1,类属性:静态属性,方法:动态属性:双下init方法,每当我们调用类的时候就会自动的触发这个方法,默认传self,在init方法里面可以对self赋值:在类的内部,self就是一个对象,我们自己实例 ...
- APICloud · 跨越2018,技术改变世界
在APICloud发展轨迹中, 2018注定是疾速的一年, 更多的风口趋势和现象级应用背后, 是技术在推动着世界的加速转动. APICloud所提供的技术服务,在混合之力的驱动下不断完善升级,“让你的 ...