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;
}
gettimeofday是计算机函数,使用C语言编写程序需要获得当前精确时间(1970年1月1日到现在的时间),或者为执行计时,可以使用gettimeofday()函数。
#include <sys/time.h>
int gettimeofday(struct timeval*tv, struct timezone *tz);

其参数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++ 获取系统时间 到秒 || 到毫秒的更多相关文章

  1. VC++ 获取系统时间、程序运行时间(精确到秒,毫秒)的五种方法

    1.使用CTime类(获取系统当前时间,精确到秒) CString str; //获取系统时间 CTime tm; tm=CTime::GetCurrentTime();//获取系统日期 str=tm ...

  2. 【VS开发】VC++ 获取系统时间、程序运行时间(精确到秒,毫秒)的五种方法

    1.使用CTime类(获取系统当前时间,精确到秒) CString str; //获取系统时间 CTime tm; tm=CTime::GetCurrentTime();//获取系统日期 str=tm ...

  3. Android获取系统时间方法的总结

    Android获取系统时间方法的方法有很多种,常用的有Calendar.Date.currentTimeMills等方法. (1)Calendar Calendar获取系统时间首先要用Calendar ...

  4. 用PHP获取系统时间时,时间比当前时间少8个小时

    自PHP5.0开始,用PHP获取系统时间时,时间比当前时间少8个小时.原因是PHP.ini中没有设置timezone时,PHP是使用的UTC时间,所以在中国时间要少8小时. 解决办法: 1.在PHP. ...

  5. C/C++获取系统时间

    C/C++获取系统时间需要使用Windows API,包含头文件"windows.h". 系统时间的数据类型为SYSTEMTIME,可以在winbase.h中查询到如下定义: ty ...

  6. VC++编程中获取系统时间

    <span style="white-space:pre"> </span>总结了在程序中如何获得系统时间的方法 void CGetSystenTimeDl ...

  7. c++ 怎样获取系统时间

    c++ 怎样获取系统时间 2008-04-28 15:34 //方案— 长处:仅使用C标准库:缺点:仅仅能精确到秒级 #include <time.h> #include <stdi ...

  8. 【转】cocos2d-x获取系统时间——2013-08-25 10

    欢迎转载,本帖地址:http://blog.csdn.net/jinjian2009/article/details/9449585 之前使用过cocos2d-x获取系统时间,毫秒级的 long ge ...

  9. c++ 如何获取系统时间 - zjnig711的信息仓库 - 博客频道 - CSDN.NET

    c++ 如何获取系统时间 - zjnig711的信息仓库 - 博客频道 - CSDN.NET c++ 如何获取系统时间 分类: C/C++ 2008-05-08 22:15 14115人阅读 评论(5 ...

随机推荐

  1. 【转】【数据结构】【有n个元素依次进栈,则出栈序列有多少种】

    卡特兰数 大神解释:https://blog.csdn.net/akenseren/article/details/82149145      权侵删 原题 有一个容量足够大的栈,n个元素以一定的顺序 ...

  2. Beta(4/7)

    鐵鍋燉腯鱻 项目:小鱼记账 团队成员 项目燃尽图 冲刺情况描述 站立式会议照片 各成员情况 团队成员 学号 姓名 git地址 博客地址 031602240 许郁杨 (组长) https://githu ...

  3. Redis新接触

    一.redis简介 redis即Remote Dictionary Server,是一个key—value存储系统. 二.优点 1.redis支持的存储类型较多,如String.List.Hash.s ...

  4. npm的一些常用命令(在国内,建议使用cnpm,在淘宝镜像里面下载就行)

    npm的一些常用命令(在国内,npm操作可能会比较慢,建议使用cnpm,在淘宝镜像里面下载就行,用法和npm一样) cnpm安装地址,直接安装 npm install cnpm -g --regist ...

  5. angular.isString()

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. robot framework + win7 64 上的安装

    1.安装 python 2.7   2.cmd  管理模式 python -m pip install --upgrade pip     pip install robotframework==3. ...

  7. PowerDesigner软件建立新模型。

      打开PowerDesigner软件,选择菜单文件->建立新模型,或者敲击键盘ctrl+N   弹出建立新模型窗口,模型类型选择Object-Oriented Model,图选择Class D ...

  8. oracle根据某个字段的值进行排序

    需求:按照颜色为蓝色.红色.黄色进行排序: order by  case                  when color = '蓝色' then                   1     ...

  9. python生成excel测试数据

    在功能测试时,经常会测到excel文件导入导出的功能,导入与导出时,需要测试系统单次导入大批量数据时是否正常, 如果系统承受不起太大的数据导入,则需要开发限制单次导入的数量,以防止系统服务异常.大量的 ...

  10. python 函数enumerate(x,y)的用法

    enumerate(x,y)函数是把元组tuple.字符串str.列表list里面的元素遍历和索引组合,其用法与range()函数很相似, 下面示例enumerate(x,y)用法以及range(x) ...