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

📄 datetime_date.h

📁 Oracle OCI 应用
💻 H
字号:

/**
 * Date.h
 *
 * @author aizy
 * @date 2005-1-5
 */

#ifndef _UTIL_DATE_H_
#define _UTIL_DATE_H_

// C header
#include <time.h>

// C++ header
#include <string>

#include "DateUtil.h"

namespace util {

class Date  
{
public:
	/**
	 * 缺省构造函数
	 */
	Date();

	/**
	 * 构造Date对象
	 *
	 * @param  timeMillis  从格林尼治标准时间1970年1月1号 00:00:00至今的毫秒数
	 * @see    GetCurrentTimeMillis()
	 */
	explicit Date(int64 timeMillis);

	/**
	 * 构造Date对象
	 *
	 * @param  sec  从格林尼治标准时间1970年1月1号 00:00:00至今的秒数
	 * @param  millis  毫秒数
	 */
	Date(time_t sec, int millis);

	/**
	 * 构造Date对象,并以当前时区初始化Date对象
	 * 【注意:不推荐使用该函数】
	 *
	 * @param   year    the year between 1970–2038.
	 * @param   month   the month between 1-12.
	 * @param   date    the day of the month between 1-31.
	 * @param   hrs     the hours between 0-23.
	 * @param   min     the minutes between 0-59.
	 * @param   sec     the seconds between 0-59.
	 * @param   msec    the mili-seconds between 0-999.
	 * @deprecated
	 */
	Date(int year, int month, int date, int hrs, int min, int sec, int msec=0);

	/**
	 * 构造Date对象,并以操作系统的时区初始化Date对象
	 * 【注意:不推荐使用该函数】
	 *
	 * @param   year    the year between 1970–2038.
	 * @param   month   the month between 1-12.
	 * @param   date    the day of the month between 1-31.
	 * @deprecated
	 */
	Date(int year, int month, int date);

	/**
	 * 拷贝构造函数
	 *
	 * @param dateSrc
	 */
	Date(const Date& dateSrc);

	virtual ~Date();

public:
	/**
	 * 返回毫秒时间
	 *
	 * @return  从格林尼治标准时间1970年1月1号 00:00:00至今的毫秒数
	 */
	int64 GetTime() const;

	/**
	 * 设置毫秒时间
	 *
	 * @param timeMillis 从格林尼治标准时间1970年1月1号 00:00:00至今的毫秒数
	 */
	void SetTime(int64 timeMillis);

	/**
	 * 返回当前时区的年
	 *
	 * @return  the year between 1970–2038.
	 */
	int GetYear() const;

	/**
	 * 返回当前时区的月
	 * 1   表示 一月
	 * 2   表示 二月
	 * ...
	 * 12  表示 十二月
	 *
	 * @return  the month between 1-12.
	 */
	int GetMonth() const;

	/**
	 * 返回当前时区的日,这里指月中的日
	 *
	 * @return  the day of the month between 1-31.
	 */
	int GetDay() const;

	/**
	 * 返回当前时区的日,这里指一个周中的7个日
	 * 0  表示 周日
	 * 1  表示 周一
	 * 2  表示 周二
	 * 3  表示 周三
	 * 4  表示 周四
	 * 5  表示 周五
	 * 6  表示 周六
	 *
	 * @return  the day of the month between 0-6.
	 */
	int GetDayOfWeek() const;

	/**
	 * 返回当前时区的日,这里指年中的日
	 * 1  表示 一月一号
	 *
	 * @return  the day of the year between 1-356.
	 */
	int GetDayOfYear() const;

	/**
	 * 返回当前时区的小时
	 *
	 * @return  the hours between 0-23.
	 */
	int GetHour() const;

	/**
	 * 返回当前时区的分钟
	 *
	 * @return  the minutes between 0-59.
	 */
	int GetMinute() const;

	/**
	 * 返回当前时区的秒
	 *
	 * @return  the seconds between 0-59.
	 */
	int GetSecond() const;

	/**
	 * 取秒数
	 * @return 从格林尼治标准时间1970年1月1号 00:00:00至今的秒数
	 */
	time_t GetTimeSeconds() const;

	/**
	 * 取毫秒数
	 * @return the milli second between 0-999.
	 */
	int GetTimeMillis() const;

	/**
	 * 测试this Date是否在指定时间之前
	 * @param   when   a date.
	 * @return
	 *         -true 在指定时间之前.
	 *         -false 不在指定时间之前.
	 */
	bool Before(const Date& when);

	/**
	 * 测试this Date是否在指定时间之后
	 * @param   when   a date.
	 * @return
	 *         -true 在指定时间之后.
	 *         -false 不在指定时间之后.
	 */
	bool After(const Date& when);

	/**
	 * 转换成yyyyMMdd HHmmss 格式的字符串
	 */
	virtual std::string ToString() const;

	void Init(int year, int month, int date, int hrs, int min, int sec, int msec=0);
	
	bool IsValid();

private:
	struct tm* GetLocalTm() const;

private:
	int64 m_timeMillis;
};

inline time_t Date::GetTimeSeconds() const
{
	return (time_t)(m_timeMillis/1000);
}

inline int Date::GetTimeMillis() const
{
	return (int)(m_timeMillis%1000);
}

std::ostream& operator<<(std::ostream& target, const Date& date);

} // namespace util

#endif // _UTIL_DATE_H_

⌨️ 快捷键说明

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