C++11中输出当前时间最直接的方法:

std::time_t t2 = std::time(nullptr);
cout << std::put_time(std::localtime(&t), "%Y-%m-%d %H.%M.%S") << "." << msecs << endl;

这种方法可以输出年月日时分秒,不过却不能输出毫秒,如果要输出毫秒需要用下面的方法:

auto n = chrono::system_clock::now();
auto m = n.time_since_epoch();
auto diff = duration_cast<milliseconds>(ms).count();
auto const msecs = diff % ; std::time_t t = system_clock::to_time_t(n1);
cout << std::put_time(std::localtime(&t), "%Y-%m-%d %H.%M.%S") << "." << msecs << endl;

将绝对时间转换为标准时间字符串的方法:

#include <string>
#include <chrono>
#include <cinttypes>
#include <ctime>
#include <sstream>
#include <iomanip> std::string millisecond_to_str(std::int64_t milliseconds)
{
std::chrono::milliseconds ms(milliseconds);
std::chrono::time_point<std::chrono::high_resolution_clock, std::chrono::milliseconds> t1(ms);
std::time_t t = std::chrono::system_clock::to_time_t(t1); std::stringstream ss;
auto const msecs = ms.count() % ;
ss << std::put_time(std::localtime(&t), "%Y-%m-%d %H.%M.%S") << "." << msecs;
return ss.str();
}

c++11 输出时间的更多相关文章

  1. Java abstract类的基本使用 和 [abstract类实现]打印1000以内的所有素数并输出时间

    笔记: /** 关键字abstract ,实现抽象类,相当于给出类的大纲,子类只管继承,但抽象类不可被实例化! * 1.抽象方法只保留方法的功能,而具体的执行,交给继承抽象类的子类,由子类重写所有的抽 ...

  2. c++11 Chrono时间库

    c++11 Chrono时间库 http://en.cppreference.com/mwiki/index.php?title=Special%3ASearch&search=chrono ...

  3. (转)linux 中使用ls指定输出时间格式

    linux 中使用ls指定输出时间格式 原文:http://blog.csdn.net/chaofanwei/article/details/13018753 ls -l --time-style=x ...

  4. java中如何按一定的格式输出时间, 必须给出例子

    题目2: 按一定的格式输出时间 import java.util.*;import java.text.SimpleDateFormat;public class Test {    public s ...

  5. python的日志模块:logging;django的日志系统;django日志输出时间修改

    Django的log,主要是复用Python标准库中的logging模块,在settings.py中进行配置 源代码 1.__init__.py包含以下类: StreamHandler Formatt ...

  6. struts2 <s:property/>标签的使用--输出时间格式转换

    转载地址http://hi.baidu.com/nonyi_com/blog/item/acf1b8d74b6cf63e07088bc4.html 最近在使用struts2的<s:propert ...

  7. Asp.Net MVC5 格式化输出时间日期

    刚好用到这个,网上找的全部是输出文本框内容的格式化日期时间 而我需要只是在一个表格中的单元个中输出单纯的文字 最后在MSDN上找到 HtmlHelper.FormatValue 方法 public s ...

  8. tp在页面输出时间

    输出时间戳 :{:time()} 输出当前时间:{:date('Y-m-d H:i:s')} 输出1970的时间:{:date('Y-m-d H:i:s',$vo['create_time'])}

  9. python学习笔记 | strftime()格式化输出时间

    time模块 import time t = time.strftime("%Y-%m-%d %H:%M:%S") print(t) datetime模块 import datet ...

随机推荐

  1. pyqt5 'QWidget' object has no attribute 'setCentralWidget'(转)

    pyqt5 'QWidget' object has no attribute 'setCentralWidget' 2019年02月18日 16:48:28 wardenjohn 阅读数:78   ...

  2. Python常用模块--string

    该模块提供3个常用的功能: * 提供常用的字符串常量(感觉用途不大) * 提供字符串替换功能,主要用途是上下文的国际化(通过str可以实现,不介绍,感兴趣的自己看官网) * 提供字符串的格式化功能(感 ...

  3. 4572: [Scoi2016]围棋 轮廓线DP KMP

    国际惯例的题面:这种题目显然DP了,看到M这么小显然要状压.然后就是具体怎么DP的问题.首先我们可以暴力状压上一行状态,然后逐行转移.复杂度n*3^m+3^(m*2),显然过不去. 考虑状态的特殊性, ...

  4. 4067: [Ctsc2015]gender 动态规划 网络流

    国际惯例的题面:首先这题是缺少两个隐藏条件的:第一个是这k条链的起点和终点所在的层是对齐的,也就是说不会出现两条链错开的情况,且这张图恰好由n层组成:第二个就是任意一个点都包含在与链上的点直接或间接相 ...

  5. STL之heap与优先级队列Priority Queue详解

    一.heap heap并不属于STL容器组件,它分为 max heap 和min heap,在缺省情况下,max-heap是优先队列(priority queue)的底层实现机制.而这个实现机制中的m ...

  6. 解决 ajax 不能下载文件的问题

  7. 阿里云ecs 增加虚拟网卡

    cd /etc/sysconfig/network-scripts/   #进入网卡目录 cp ifcfg-eth0 ifcfg-eth0:1   # 复制出ifcfg-eth0:1虚拟网卡 vi i ...

  8. C#利用QRCoder生产二维码

    系统使用.NET4.5.1 代码如下: using System; using System.Collections.Generic; using System.Linq; using System. ...

  9. GeoHash原理和可视化显示

    最近在做附近定位功能的产品,geohash是一个非常不错的实现方式.查询资料,发现阿里的这篇文章讲解的很好.但文中并没有给出geohash显示的工具.无奈,也没有查到类似的.只好自己简单显示一下,方便 ...

  10. 【T04】开发并使用应用程序框架

    1.TCP/IP应用程序分为 TCP服务器 TCP客户端 UDP服务器 UDP客户端 2.构建框架库是比较简单的一件事,主要就是对socket编程.