吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1624|回复: 6
收起左侧

[C&C++ 转载] Timestamp 显示时间戳

[复制链接]
古月不傲 发表于 2021-1-13 15:30
本帖最后由 古月不傲 于 2021-1-13 15:50 编辑

[C++] 纯文本查看 复制代码
// Use of this source code is governed by a BSD-style license
// that can be found in the License file.
//
// Author: Shuo Chen (chenshuo at chenshuo dot com)

#ifndef MUDUO_BASE_TIMESTAMP_H
#define MUDUO_BASE_TIMESTAMP_H

//#include "muduo/base/copyable.h"
//#include "muduo/base/Types.h"

//#include <boost/operators.hpp>
#include <iostream>
#include <string>
#include <stdint.h>
#include <inttypes.h>
#include <sys/time.h>

namespace muduo
{
	///
	/// Time stamp in UTC, in microseconds resolution.
	///
	/// This class is immutable.
	/// It's recommended to pass it by value, since it's passed in register on x64.
	///	基于UTC的时间戳,使用微妙
	//	该类是不可改变的
	//	建议通过值传递,因为是在64位机器上嘛
	class Timestamp // : 
		//public muduo::copyable,						// 显示该对象可以被拷贝、赋值
		//public boost::equality_comparable<Timestamp>, // 只要重载== 	自动重载 !=
		//public boost::less_than_comparable<Timestamp>	// 只要重载< 	自动重载 > <= >=
	{
		public:
			///
			/// Constucts an invalid Timestamp.
			///
			Timestamp()
				: microSecondsSinceEpoch_(0)
			{
			}

			///
			/// Constucts a Timestamp at specific time
			///
			/// @param microSecondsSinceEpoch
			// 在特定时间内构造时间戳
			explicit Timestamp(int64_t microSecondsSinceEpochArg)
				: microSecondsSinceEpoch_(microSecondsSinceEpochArg)
			{
			}

		public:
			// 交换时间
			void swap(Timestamp& that)
			{
				std::swap(microSecondsSinceEpoch_, that.microSecondsSinceEpoch_);
			}

			// default copy/assignment/dtor are Okay

			// 检测时间是否有效
			bool valid() const 
			{
			   	return microSecondsSinceEpoch_ > 0; 
			}

			// for internal usage.
			// 返回微妙,用于重载比较
			int64_t microSecondsSinceEpoch() const 
			{
			   	return microSecondsSinceEpoch_; 
			}

			// 返回妙 	用微妙 / 1000000即可
			time_t secondsSinceEpoch() const
			{ 
				return static_cast<time_t>(microSecondsSinceEpoch_ / kMicroSecondsPerSecond); 
			}

			// 编译时断言
			// static_assert(sizeof(Timestamp) == sizeof(int64_t),
					// "Timestamp should be same size as int64_t");

			// 将 秒 + 微妙 格式成字符串
			std::string toString() const
			{
				char buf[32] {};

				int64_t seconds = microSecondsSinceEpoch_ / kMicroSecondsPerSecond;
				int64_t microseconds = microSecondsSinceEpoch_ % kMicroSecondsPerSecond;
				snprintf(buf, sizeof(buf), "%" PRId64 ".%06" PRId64 "", seconds, microseconds);
				
				return buf;
			}

			// 输出当前时间格式
			std::string toFormattedString(bool showMicroseconds) const
			{
				char buf[64] {};
				time_t seconds = static_cast<time_t>(microSecondsSinceEpoch_ / kMicroSecondsPerSecond);
				struct tm tm_time;
				gmtime_r(&seconds, &tm_time);

				// 显示微妙
				if (showMicroseconds)
				{
					int microseconds = static_cast<int>(microSecondsSinceEpoch_ % kMicroSecondsPerSecond);
					snprintf(buf, sizeof(buf), "%4d%02d%02d %02d:%02d:%02d.%06d",
							tm_time.tm_year + 1900, tm_time.tm_mon + 1, tm_time.tm_mday,
							tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec,
							microseconds);
				}
				// 不显示
				else
				{
					snprintf(buf, sizeof(buf), "%4d%02d%02d %02d:%02d:%02d",
							tm_time.tm_year + 1900, tm_time.tm_mon + 1, tm_time.tm_mday,
							tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec);
				}
				return buf;
			}
		public:	// 注意以下是静态成员函数
			// 获取当前的时间
			static Timestamp now()
			{
				struct timeval tv;
				gettimeofday(&tv, nullptr);
				int64_t seconds = tv.tv_sec;

				// 秒 * 微妙 = 微妙 + 当前的微妙
				return Timestamp(seconds * kMicroSecondsPerSecond + tv.tv_usec);
			}

			// 获取无效的时间,返回0即可
			static Timestamp invalid()
			{
				return Timestamp();
			}

			static Timestamp fromUnixTime(time_t t)
			{
				return fromUnixTime(t, 0);
			}

			// 返回微妙
			static Timestamp fromUnixTime(time_t t, int microseconds)
			{
				return Timestamp(static_cast<int64_t>(t) * kMicroSecondsPerSecond + microseconds);
			}

			static const int kMicroSecondsPerSecond = 1000 * 1000;

		private:
			int64_t microSecondsSinceEpoch_;	// 微妙
	};

	// 重载 < 由于继承了 public boost::less_than_comparable<Timestamp>
	// 所以会默认重载 > <= >=
	inline bool operator<(Timestamp lhs, Timestamp rhs)
	{
		return lhs.microSecondsSinceEpoch() < rhs.microSecondsSinceEpoch();
	}

	// 重载 == 由于继承了 public boost::equality_comparable<Timestamp> 
	// 所以会默认重载 !=
	inline bool operator==(Timestamp lhs, Timestamp rhs)
	{
		return lhs.microSecondsSinceEpoch() == rhs.microSecondsSinceEpoch();
	}

	///
	/// Gets time difference of two timestamps, result in seconds.
	//	获取两者的差,返回秒
	///
	/// @param high, low
	/// @return (high-low) in seconds
	/// @c double has 52-bit precision, enough for one-microsecond
	/// resolution for next 100 years.
	inline double timeDifference(Timestamp high, Timestamp low)
	{
		int64_t diff = high.microSecondsSinceEpoch() - low.microSecondsSinceEpoch();
		return static_cast<double>(diff) / Timestamp::kMicroSecondsPerSecond;
	}

	///
	/// Add @c seconds to given timestamp.
	/// @return timestamp+seconds as Timestamp
	///
	// 对象 + seconds > microsenconds
	inline Timestamp addTime(Timestamp timestamp, double seconds)
	{
		// 转换成微妙
		int64_t delta = static_cast<int64_t>(seconds * Timestamp::kMicroSecondsPerSecond);
		// 当前对象 + 微妙即可
		return Timestamp(timestamp.microSecondsSinceEpoch() + delta);
	}
}  // namespace muduo

#endif  // MUDUO_BASE_TIMESTAMP_H

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

Misxiaoduan 发表于 2021-1-13 16:13
我一个搞前端的,是真的卡不懂
墨涵 发表于 2021-1-13 16:36
hezhengtao 发表于 2021-1-13 16:41
838384855 发表于 2021-1-13 17:01
你这是为了同步显示某些电商的服务器时间吗 哈哈 感谢楼主分享
飞tian狐li 发表于 2021-1-13 17:04
感谢分享
CHINA_VIP 发表于 2021-1-13 17:13

感谢分享
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-7 00:15

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表