muduo Timestamp详解
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详解相关推荐
- muduo scoped_ptr详解
1. 简介 scoped_ptr类似于智能指针只能在作用域里使用,不希望被转让. 2. 类与接口 #ifndef _SCOPE_PTR_HH #define _SCOPE_PTR_HH // scop ...
- muduo BlockingQueue详解
1. 简介 利用条件变量和std::deque实现阻塞队列 2. 类与接口 插入数据,有2个版本 void put(const T& x) 非常量可以初始化一个底层const,反过来却不行 可 ...
- muduo StringPiece详解
1. 简介 该StringPiece是PCRE的实现,是对字符串的一个proxy类而已(即设计模式中的代理模式). 2. 背景 很多时候,当传入一个字符串到函数时,往往只是读取字符串时 若使用std: ...
- muduo Date详解
1. 简介 用于Julian Day和普通年月日的转换 2. 类与接口 2.1 class Date : public copyable ① Date::Date(int y, int m, int ...
- muduo Thread详解
1. 简介 Thread用于线程管理. 2. pthread_atfork 在进行linux系统里开发时,经常会调用linux的系统函数fork来产生一个子进程,如果父子进程都没有用到pthread线 ...
- muduo Mutex详解
1. 简介 多线程环境下利用pthread进行共享资源的互斥操作,该模块使用了Thread safety annotations技术,若使用了cling c++解析器,可以在编译时进行线程安全分析,如 ...
- muduo Logging详解
1. 简介 Logging用于将日志事件信息(时间 日志级别 文件名 行号等)加到输出缓冲区,默认输出到stdout. 2. 类与接口 ① 私有内部类Impl 将日志事件(时间 日志级别 文件名 行号 ...
- MySQL5日期类型DATETIME和TIMESTAMP相关问题详解
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://lavasoft.blog.51cto.com/62575/280284 MySQ ...
- python datetime to timestamp_python timestamp和datetime之间转换详解
做开发中难免时间类型之间的转换, 最近就发现前端js和后端django经常要用到这个转换, 其中jsDate.now()精确到毫秒,而Python中Datetime.datetime.now()是精确 ...
最新文章
- 【C语言】实现strcat
- 通过仿真和综合认识T触发器(Verilog HDL语言描述T触发器)
- LA3942字典树+递推
- MultipartFile 文件上传
- 印度软件开发人员_我如何辍学并在19岁时在印度找到了一份开发人员的工作
- 《Android开发艺术探索》读书笔记 (4) 第4章 View的工作原理
- 验证Vsphere 5 支持大于2TB磁盘
- SharePoint 2013让页面显示错误
- 如何快速准确的识别出一个文件的具体类型
- 类 workbooks 的 open 方法无效_第十九章 Cach 命令大全 OPEN 命令
- 第5次作业+105032014070+胡阳洋
- java int 原子_java中的原子操作类AtomicInteger及其实现原理
- Julia : REPL中的一些快捷键
- 公司邮箱通讯录的更新
- STL格式抽壳补孔洞破面修复等,Magics视频教程
- 六级考研单词之路-三十
- 魔兽争霸的历史(ZT)第二章
- html_09网页超链接
- C51单片机教程之工程建立
- 80秒验证13亿个身份证号码,包含省市县验证