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. Codeforces 461D. Appleman and Complicated Task 构造,计数

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF461D.html 题解 首先我们可以发现如果确定了第一行,那么方案就唯一了. 然后,我们来看看一个点的值确定 ...

  2. LOJ.2865.[IOI2018]狼人(Kruskal重构树 主席树)

    LOJ 洛谷 这题不就是Peaks(加强版)或者归程么..这算是\(IOI2018\)撞上\(NOI2018\)的题了? \(Kruskal\)重构树(具体是所有点按从小到大/从大到小的顺序,依次加入 ...

  3. Spring Cloud微服务笔记(一)微服务概念

    微服务概念 一.什么是微服务架构 微服务,是一个小的.松耦合的分布式服务. 为什么需要微服务: 1)单体系统部署在一个进程中,修改了一个小功能,为了部署上线就会影响其他功能. 2)单体应用各个功能模块 ...

  4. docker+ubuntu14.04+cuda7.0

    参考链接: http://tleyden.github.io/blog/2014/10/25/docker-on-aws-gpu-ubuntu-14-dot-04-slash-cuda-6-dot-5 ...

  5. GMA Round 1 数列与方程

    传送门 数列与方程 首项为1,各项均大于0的数列{$a_n$}的前n项和$S_n$满足对于任意正整数n:$S_{n+1}^2-2*S_{n+1}*S_{n}-\sqrt{2}*S_n-1=0$,求$a ...

  6. ECMA Script 6_Symbol() 唯一类型值声明函数_Symbol 数据类型

    Symbol 数据类型 let s = Symbol(); typeof s; // "symbol" 是 ES6 继 Number,String,Boolean,Undefine ...

  7. shell-特殊变量列表

    特殊变量列表 变量 含义 $0 当前脚本的文件名 $n 传递给脚本或函数的参数.n $# 传递给脚本或函数的参数个数. $* 传递给脚本或函数的所有参数.有引号则引号内,作为一个参数传入. $@ 传递 ...

  8. springBoot使用@Value标签读取*.properties文件的中文乱码问题

    上次我碰到获取properties文件中的中文出现乱码问题. 查了下资料,原来properties默认的字符编码格式为asci码,所以我们要对字符编码进行转换成UTF-8格式 原先代码:@Proper ...

  9. @ConfigurationProperties注解取消location属性

    当我正在自学如何自定义properties配置文件,为了防止不必要的麻烦,重新创建了一个新的properties文件 fu.properties 然后在自定义的配置类中引入fu.properties/ ...

  10. python全栈开发 * mysql数据类型 * 180829

    * 库的操作 (增删改查)一.系统数据库查看系统库命令 show databases1.information_schema: 虚拟库,不占用磁盘空间,存储的是数据库启动后的一些参数,如用户表信息.列 ...