1. 简介

Timestamp用于提供时间戳相关的工具函数。

2. 类与接口

① string toString() const
返回时间的字符串形式,例如1649224501.687051

② string toFormattedString(bool showMicroseconds = true) const
返回时间的字符串形式,showMicroseconds为true时,例如20220406 05:55:01.687051,为false时,例如20220406 05:55:01

③ time_t secondsSinceEpoch() const
返回距离1970-1-1 00:00:00的秒数

④ static Timestamp now()
获取当前时间

⑤ double timeDifference(Timestamp high, Timestamp low)
获取时间间隔单位为秒

⑥ Timestamp addTime(Timestamp timestamp, double seconds)
从当前时间加上对应秒数

3. PRID64

c++使用PRID64,需要两步:
1.包含头文件:<inttypes.h>
2.定义宏:__STDC_FORMAT_MACROS,可以通过编译时加-D__STDC_FORMAT_MACROS,或者在包含文件之前定义这个宏。
int64_t用来表示64位整数,在32位系统中是long long int,在64位系统中是long int,所以打印int64_t的格式化方法是:

printf("%ld", value); // 64bit OS
printf("%lld", value); // 32bit OS

可借助PRID64实现跨平台的方法:

#include <inttypes.h>
printf("%" PRId64 "\n", value);
// 相当于64位的:
printf("%" "ld" "\n", value);
// 或32位的:
printf("%" "lld" "\n", value);

4. test_timestamp.cc

//
//  test_timestamp.cc
//  test_timestamp
//
//  Created by blueBling on 22-3-31.
//  Copyright (c) 2022年blueBling. All rights reserved.
//#include "Timestamp.h"#include <iostream>
#include <vector>using std::cout;
using std::endl;
using std::vector;int test_timestamp() {Timestamp time1(Timestamp::now());cout << time1.toString() << endl;cout << time1.toFormattedString(true) << endl;cout << time1.toFormattedString(false) << endl;// 创建1000000个Timestamp个对象所需要的时间const int kNumber = 1000*1000;std::vector<Timestamp> stamps;stamps.reserve(kNumber);for (int i = 0; i < kNumber; i++) {stamps.push_back(Timestamp::now());}cout << stamps.front().toFormattedString(false) << endl;cout << stamps.back().toFormattedString(false) << endl;cout << timeDifference(stamps.back(), stamps.front()) << endl;return 0;
}int main() {test_timestamp();return 0;
}

测试结果

1649224501.687051
20220406 05:55:01.687051
20220406 05:55:01
20220406 05:55:01
20220406 05:55:01
0.064806

5. 源码

GitHub

muduo Timestamp详解相关推荐

  1. muduo scoped_ptr详解

    1. 简介 scoped_ptr类似于智能指针只能在作用域里使用,不希望被转让. 2. 类与接口 #ifndef _SCOPE_PTR_HH #define _SCOPE_PTR_HH // scop ...

  2. muduo BlockingQueue详解

    1. 简介 利用条件变量和std::deque实现阻塞队列 2. 类与接口 插入数据,有2个版本 void put(const T& x) 非常量可以初始化一个底层const,反过来却不行 可 ...

  3. muduo StringPiece详解

    1. 简介 该StringPiece是PCRE的实现,是对字符串的一个proxy类而已(即设计模式中的代理模式). 2. 背景 很多时候,当传入一个字符串到函数时,往往只是读取字符串时 若使用std: ...

  4. muduo Date详解

    1. 简介 用于Julian Day和普通年月日的转换 2. 类与接口 2.1 class Date : public copyable ① Date::Date(int y, int m, int ...

  5. muduo Thread详解

    1. 简介 Thread用于线程管理. 2. pthread_atfork 在进行linux系统里开发时,经常会调用linux的系统函数fork来产生一个子进程,如果父子进程都没有用到pthread线 ...

  6. muduo Mutex详解

    1. 简介 多线程环境下利用pthread进行共享资源的互斥操作,该模块使用了Thread safety annotations技术,若使用了cling c++解析器,可以在编译时进行线程安全分析,如 ...

  7. muduo Logging详解

    1. 简介 Logging用于将日志事件信息(时间 日志级别 文件名 行号等)加到输出缓冲区,默认输出到stdout. 2. 类与接口 ① 私有内部类Impl 将日志事件(时间 日志级别 文件名 行号 ...

  8. MySQL5日期类型DATETIME和TIMESTAMP相关问题详解

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://lavasoft.blog.51cto.com/62575/280284 MySQ ...

  9. python datetime to timestamp_python timestamp和datetime之间转换详解

    做开发中难免时间类型之间的转换, 最近就发现前端js和后端django经常要用到这个转换, 其中jsDate.now()精确到毫秒,而Python中Datetime.datetime.now()是精确 ...

最新文章

  1. 线段树 ---- Codeforces 737 Div2 D. Ezzat and Grid 维护dp
  2. doctype的三种类型
  3. Linux正則表達式-定位元字符
  4. leetcode之Climbing Stairs爬楼梯
  5. Java动态解析域名
  6. 转载:简单介绍Python中的try和finally和with方法
  7. Flex Failed to connect; session timed out.解决办法
  8. 永宏plc和台达vfd-m变頻器modbs rtu通讯程序史上最好用的plc和变頻器rtu通讯程序
  9. 手机自学html,5套Bootstrap手机模板HTML源文件分享
  10. linux ftp命令下载目录,linux命令行下的ftp 多文件下载和目录下载
  11. java程序员实习日记_java实习日记.doc
  12. 【css】让img图片居中显示
  13. 计算机网卡的总线接口分类有,网卡的详细分类是怎样的?以太网卡有将高层数据转换为比特流的功能吗?...
  14. 一个典型的语音识别系统
  15. mysql 英文月份_mysql – STR_TO_DATE替换非英文月份名称
  16. 苏菲兔子和魔法师小麦
  17. 查询并处理占用8080端口进程
  18. 学而后思,方能发展;思而立行,终将卓越
  19. 黑马视频免费领取了。
  20. lombok和JPA的死递归

热门文章

  1. 前锋python百度云_关于python:使用索引在pandas DataFrame中设置特定单元格的值
  2. 误操作导致引导区崩溃的补救办法(需要使用火绒预备份引导区)
  3. 指令集与CPU架构概念与联系
  4. 软件测试分类(一)--按测试阶段划分
  5. echarts 柱状图添加排序图标/序号
  6. 面向过程和面向对象程序设计的区别
  7. 破解宽带我世界对路由器的使用限制
  8. android开机画面在uboot里吗,uboot里开机LOGO显示功能解析
  9. 反射工具类Reflections
  10. 首次力压 macOS!Linux 杀疯了!!