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 ...
随机推荐
- Sicily T-primes
DescriptionWe know that prime numbers are positive integers that have exactly two distinct positive ...
- 141. 环形链表 [JS Undefined类型的运用]
[解析] 1.遍历每个节点 2.遍历过的节点用新的空间来标记 JS新的空间不用在使用前声明,用法参考实现. 3.如果遇到标记过的节点则是环形 [实现] /** * Definition for sin ...
- python traceback获取详细的异常信息
运行结果 : 用try except 会报出报错信息,但是没有具体哪个地方报错,多少行,这样不利于查找报错信息 这时我们就可以使用traceback模块 运行结果: 如上图,报错信息会具体 ...
- C++调用C语言的库函数
在项目中,使用C语言编写了一个socket后台程序tkcofferd,并且为方便客户端的使用,提供了动态库,其中包含socket接口. 现在的需求是使用qt做一个前端界面,用来展示tkcofferd的 ...
- nagios的安装与部署
参考文献: https://www.cnblogs.com/mchina/archive/2013/02/20/2883404.html https://www.jianshu.com/p/3476d ...
- [LeetCode] Preimage Size of Factorial Zeroes Function 阶乘零的原像个数函数
Let f(x) be the number of zeroes at the end of x!. (Recall that x! = 1 * 2 * 3 * ... * x, and by con ...
- DEV_TreeList使用经验小结
1. 点击叶子节点是希望Open键显示,点击非叶子节点时希望隐藏.实践中发现点击到了非叶子节点图标,Open没有隐藏,如何解决? 增加一个判断: if (_hitInfo.HitInfoType != ...
- react-redux简单的记事本应用
首先给大家普及一个方法: let data = [ {id:1,text:'手机',check:false}, {id:2,text:'电脑',check:false}, {id:3,text:'平板 ...
- python获取文件所在目录
1.执行的python程序获取自己文件所在目录 import os,sys os.chdir(sys.path[0]); dir_name = os.path.abspath(os.path.join ...
- Python练手例子(7)
37.对10个数进行排序. 程序分析:可以利用选择法,即从后9个比较过程中,选择一个最小的与第一个元素交换,下次类推,即用第二个元素与后8个进行比较,并进行交换. #python 3.7 if __n ...