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. 047 大数据下的java client连接JDBC

    1.前提 启动hiveserver2服务 url,username,password. 2.官网 3.程序 4.结果 emp的第一列与第二列 5.源程序 package com.cj.it.hiveU ...

  2. Ubuntu pkg_resources.DistributionNotFound: The 'Scrapy==1.0.3' distribution was not found and is required by the application

    在Ubuntu16.0.4上安装scrapy,参考:Ubuntu16.04安装Scrapy命令 都安装成功后,import scrapy也都没出错,但是在终端输入:scrapy 命令时出现错误: Fi ...

  3. 9,EasyNetQ-版本化消息

    要启用对版本化消息的支持,您需要确保配置所需的组件. 最简单的方法是: var bus = RabbitHutch.CreateBus( "host=localhost", ser ...

  4. Cocos2d-X开发教程-捕鱼达人 Cocos2-x development tutorial - fishing talent

    Cocos2d-X开发教程-捕鱼达人 Cocos2-x development tutorial - fishing talent 作者:韩梦飞沙 Author:han_meng_fei_sha 邮箱 ...

  5. C#全能数据库操作类及调用示例

    C#全能数据库操作类及调用示例 using System; using System.Data; using System.Data.Common; using System.Configuratio ...

  6. Java并发程序设计(十一)设计模式与并发之生产者-消费者模式

    设计模式与并发之生产者-消费者模式 生产者-消费者模式是一个经典的多线程设计模式.它为多线程间的协作提供了良好的解决方案.在生产者-消费者模式中,通常由两类线程,即若干个生产者线程和若干个消费者线程. ...

  7. 潭州课堂25班:Ph201805201 WEB 之 JS 第六课 (课堂笔记)

    上节补充方法 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...

  8. 爬虫scrapy的使用

    1.常用命令

  9. Shell中的>/dev/null 2>&1 与 2>&1 >/dev/null 与&>/dev/null 的区别

    默认情况下,总是有三个文件处于打开状态,标准输入(键盘输入).标准输出(输出到屏幕).标准错误(也是输出到屏幕),它们分别对应的文件描述符是0,1,2 .那么我们来看看下面的几种重定向方法的区别: & ...

  10. DBProxy

    DBProxy/USER_GUIDE.md at master · Meituan-Dianping/DBProxy   https://github.com/Meituan-Dianping/DBP ...