⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nltime.h

📁 一些unix下的c/c++的util包
💻 H
字号:
/**
 * 时间类定义
 * @file NLTime.h
 * @date 14-Jul-2005
 * @author 胡春雨
 * @version 1.0.0: 初始版本
 */

#ifndef _NL_TIME_H
#define _NL_TIME_H

#include <time.h>
#include <sys/time.h>
#include <string>

namespace nlkit
{

/**
 * 按时间循环计划
 */
enum NLDailyRollingSchedule
{
	MONTHLY = 1,								/**< 按月 */
	WEEKLY,										/**< 按星期 */
	DAILY,										/**< 按天 */
	TWICE_DAILY,								/**< 按半天 */
	HOURLY,										/**< 按小时 */
	MINUTELY									/**< 按分 */
};

/**
 * 时间类
 */
class NLTime
{
public:
	/**
	 * 构造函数
	 * @param 无
	 * @return 无
	 */
	NLTime();

	/**
	 * 构造函数
	 * @param tv_sec:	秒
	 * @param tv_usec:	毫秒
	 * @return 无
	 */
	NLTime(long tv_sec, long tv_usec);

	/**
	 * 构造函数
	 * @param time:	时间
	 * @return 无
	 */
	NLTime(time_t time);


	/**
	 * 虚拟析构函数
	 * @param 无
	 * @return 无
	 */
	virtual ~NLTime();

	/**
	 * 取当前时间
	 * @param 无
	 * @return NLTime:	当前时间
	 */
	static NLTime gettimeofday();

	/**
	 * 取秒
	 * @param 无
	 * @return long:	秒数值
	 */
	long sec() const { return tv_sec; }

	/**
	 * 取毫秒
	 * @param 无
	 * @return long:	毫秒数值
	 */
	long usec() const { return tv_usec; }

	/**
	 * 设置秒
	 * @param s:	秒数值
	 * @return 无
	 */
	void sec(long s) { tv_sec = s; }

	/**
	 * 设置毫秒
	 * @param us:	毫秒数值
	 * @return 无
	 */
	void usec(long us) { tv_usec = us; }

	/**
	 * 设置时间
	 * @param t:	时间struct tm指针
	 * @return int:	秒数值
	 */
	int setTime(struct tm* t);

	/**
	 * 取时间
	 * @param 无
	 * @return time_t:	时间值
	 */
	time_t getTime() const;

	/**
	 * 取时间
	 * @param t:	时间struct tm指针
	 * @return 无
	 */
	void gmtime(struct tm* t) const;

	/**
	 * 取时间
	 * @param t:	时间struct tm指针
	 * @return 无
	 */
	void localtime(struct tm* t) const;

	/**
	 * 按格式取时间字符串
	 * @param fmt:	格式
	 * @param use_gmtime:	是否使用gmtime
	 *  - true:使用gmtime
	 *  - false:使用localtime
	 * @return string:	转换后的字符串
	 * @remarks 使用%q或者%Q要使用%%来忽略strftime的转换
	 */
	std::string getFormattedTime(const std::string& fmt, bool use_gmtime = false) const;

	/**
	 * 按格式设置时间
	 * @param timebuf:	时间缓冲区
	 * @param fmt:	格式
	 * @return 无
	 * @remarks 目前只支持默认的格式
	 */
	void setFormattedTime(const char* timebuf, const char* fmt = "%Y%m%d%H%M%S");

	/**
	 * 取下一次循环的时间
	 * @param t:	当前时间
	 * @param schedule:	循环类型
	 * @param value:	间隔数值
	 * @return NLTime:	下一次循环的时间
	 */
	static NLTime calculateNextRolloverTime(const NLTime& t, NLDailyRollingSchedule schedule, int value = 1);

	/**
	 * 设置计算的开始时间
	 * @param t:	需要设置的时间
	 * @param schedule:	循环类型
	 * @return NLTime:	无
	 */
	static void setCalculateBeginTime(NLTime& t, NLDailyRollingSchedule schedule);

	/**
	 * 获得计算的开始时间
	 * @param t:	需要获得的时间
	 * @param schedule:	循环类型
	 * @return NLTime:	无
	 */
	static NLTime getCalculateBeginTime(const NLTime& t, NLDailyRollingSchedule schedule);

	NLTime& operator += (const NLTime& rhs);
	NLTime& operator -= (const NLTime& rhs);
	NLTime& operator /= (long rhs);
	NLTime& operator *= (long rhs);
	bool operator == (const NLTime& rhs) { return tv_sec == rhs.tv_sec && tv_usec == rhs.tv_usec; }
	bool operator != (const NLTime& rhs) { return !(*this == rhs); }

private:
	long tv_sec;								/**< 秒 */
	long tv_usec;								/**< 微秒 */
};

const NLTime operator + (const NLTime& lhs, const NLTime& rhs);
const NLTime operator - (const NLTime& lhs, const NLTime& rhs);
const NLTime operator / (const NLTime& lhs, long rhs);
const NLTime operator * (const NLTime& lhs, long rhs);
bool operator < (const NLTime& lhs, const NLTime& rhs);
bool operator <= (const NLTime& lhs, const NLTime& rhs);
bool operator > (const NLTime& lhs, const NLTime& rhs);
bool operator >= (const NLTime& lhs, const NLTime& rhs);
bool operator == (const NLTime& lhs, const NLTime& rhs);
bool operator != (const NLTime& lhs, const NLTime& rhs);
}
#endif // !_NL_TIME_H

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -