Console Library 8.0.0
A header-only library that makes C++ simple
Loading...
Searching...
No Matches
console::DateTime Class Reference

#include <time.h>

Public Member Functions

 DateTime ()
 DateTime (int year, int month, int day, int hour=0, int minute=0, int second=0)
 DateTime (const std::string &str, const std::string &fmt="%Y-%m-%d %H:%M:%S")
 DateTime (const std::wstring &str, const std::wstring &fmt=L"%Y-%m-%d %H:%M:%S")
int year () const
int month () const
int day () const
int hour () const
int minute () const
int second () const
int weekday () const
int yearday () const
bool isdst () const
bool is_leap () const
int days_in_month () const
int iso_weekday () const
 获取本周第几天(周一=1, ..., 周日=7)。
int iso_week () const
 获取 ISO 8601 周数。
DateTime begin_of_month () const
 获取当月第一天的 DateTime。
DateTime end_of_month () const
 获取当月最后一天的 DateTime。
DateTime begin_of_day () const
 获取当天 00:00:00 的 DateTime。
DateTime end_of_day () const
 获取当天 23:59:59 的 DateTime。
bool is_weekend () const
 判断是否为周末。
int day_of_year () const
 获取该年中的第几天(1-based)。
std::string str (const std::string &fmt="%Y-%m-%d %H:%M:%S") const
 格式化为字符串。
std::wstring wstr (const std::wstring &fmt=L"%Y-%m-%d %H:%M:%S") const
 格式化为宽字符串。
std::time_t to_time_t () const
 转为 std::time_t。

Static Public Member Functions

static bool is_leap (int y)
 判断是否为闰年。
static int days_in_month (int y, int m)
 获取指定月份的天数。

Private Member Functions

void normalize ()

Private Attributes

std::tm tm_ {}

Friends

std::ostream & operator<< (std::ostream &os, const DateTime &dt)
 输出日期时间到流,格式为 "%Y-%m-%d %H:%M:%S"。
std::wostream & operator<< (std::wostream &os, const DateTime &dt)
 输出日期时间到宽字符流。
比较运算
bool operator== (const DateTime &lhs, const DateTime &rhs)
bool operator!= (const DateTime &lhs, const DateTime &rhs)
bool operator< (const DateTime &lhs, const DateTime &rhs)
bool operator> (const DateTime &lhs, const DateTime &rhs)
bool operator<= (const DateTime &lhs, const DateTime &rhs)
bool operator>= (const DateTime &lhs, const DateTime &rhs)

算术运算

DateTime & operator+= (Time t)
 增加指定秒数。
DateTime & operator-= (Time t)
 减少指定秒数。
DateTime operator+ (DateTime dt, Time t)
DateTime operator- (DateTime dt, Time t)
Time operator- (const DateTime &lhs, const DateTime &rhs)
 计算两个 DateTime 的时间差。

Constructor & Destructor Documentation

◆ DateTime() [1/4]

console::DateTime::DateTime ( )
inline

◆ DateTime() [2/4]

console::DateTime::DateTime ( int year,
int month,
int day,
int hour = 0,
int minute = 0,
int second = 0 )
inline

◆ DateTime() [3/4]

console::DateTime::DateTime ( const std::string & str,
const std::string & fmt = "%Y-%m-%d %H:%M:%S" )
inline

◆ DateTime() [4/4]

console::DateTime::DateTime ( const std::wstring & str,
const std::wstring & fmt = L"%Y-%m-%d %H:%M:%S" )
inline

Member Function Documentation

◆ begin_of_day()

DateTime console::DateTime::begin_of_day ( ) const
inline

获取当天 00:00:00 的 DateTime。

◆ begin_of_month()

DateTime console::DateTime::begin_of_month ( ) const
inline

获取当月第一天的 DateTime。

◆ day()

int console::DateTime::day ( ) const
inline

◆ day_of_year()

int console::DateTime::day_of_year ( ) const
inline

获取该年中的第几天(1-based)。

◆ days_in_month() [1/2]

int console::DateTime::days_in_month ( ) const
inline

◆ days_in_month() [2/2]

int console::DateTime::days_in_month ( int y,
int m )
inlinestatic

获取指定月份的天数。

Parameters
y年份。
m月份(1-12)。
Returns
该月的天数。

◆ end_of_day()

DateTime console::DateTime::end_of_day ( ) const
inline

获取当天 23:59:59 的 DateTime。

◆ end_of_month()

DateTime console::DateTime::end_of_month ( ) const
inline

获取当月最后一天的 DateTime。

◆ hour()

int console::DateTime::hour ( ) const
inline

◆ is_leap() [1/2]

bool console::DateTime::is_leap ( ) const
inline

◆ is_leap() [2/2]

bool console::DateTime::is_leap ( int y)
inlinestatic

判断是否为闰年。

Parameters
y年份,默认为当前 year()。
Returns
true 如果是闰年。

◆ is_weekend()

bool console::DateTime::is_weekend ( ) const
inline

判断是否为周末。

◆ isdst()

bool console::DateTime::isdst ( ) const
inline

◆ iso_week()

int console::DateTime::iso_week ( ) const
inline

获取 ISO 8601 周数。

Returns
当前日期所在的 ISO 周数(1-53)。

◆ iso_weekday()

int console::DateTime::iso_weekday ( ) const
inline

获取本周第几天(周一=1, ..., 周日=7)。

◆ minute()

int console::DateTime::minute ( ) const
inline

◆ month()

int console::DateTime::month ( ) const
inline

◆ normalize()

void console::DateTime::normalize ( )
inlineprivate

◆ operator+=()

DateTime & console::DateTime::operator+= ( Time t)
inline

增加指定秒数。

◆ operator-=()

DateTime & console::DateTime::operator-= ( Time t)
inline

减少指定秒数。

◆ second()

int console::DateTime::second ( ) const
inline

◆ str()

std::string console::DateTime::str ( const std::string & fmt = "%Y-%m-%d %H:%M:%S") const
inline

格式化为字符串。

Parameters
fmt格式化字符串,默认为 "%Y-%m-%d %H:%M:%S"。
Returns
std::string 格式化结果。

◆ to_time_t()

std::time_t console::DateTime::to_time_t ( ) const
inline

转为 std::time_t。

◆ weekday()

int console::DateTime::weekday ( ) const
inline

◆ wstr()

std::wstring console::DateTime::wstr ( const std::wstring & fmt = L"%Y-%m-%d %H:%M:%S") const
inline

格式化为宽字符串。

Parameters
fmt格式化字符串,默认为 L"%Y-%m-%d %H:%M:%S"。
Returns
std::wstring 格式化结果。

◆ year()

int console::DateTime::year ( ) const
inline

◆ yearday()

int console::DateTime::yearday ( ) const
inline

◆ operator!=

bool operator!= ( const DateTime & lhs,
const DateTime & rhs )
friend

◆ operator+

DateTime operator+ ( DateTime dt,
Time t )
friend

◆ operator- [1/2]

Time operator- ( const DateTime & lhs,
const DateTime & rhs )
friend

计算两个 DateTime 的时间差。

◆ operator- [2/2]

DateTime operator- ( DateTime dt,
Time t )
friend

◆ operator<

bool operator< ( const DateTime & lhs,
const DateTime & rhs )
friend

◆ operator<< [1/2]

std::ostream & operator<< ( std::ostream & os,
const DateTime & dt )
friend

输出日期时间到流,格式为 "%Y-%m-%d %H:%M:%S"。

◆ operator<< [2/2]

std::wostream & operator<< ( std::wostream & os,
const DateTime & dt )
friend

输出日期时间到宽字符流。

◆ operator<=

bool operator<= ( const DateTime & lhs,
const DateTime & rhs )
friend

◆ operator==

bool operator== ( const DateTime & lhs,
const DateTime & rhs )
friend

◆ operator>

bool operator> ( const DateTime & lhs,
const DateTime & rhs )
friend

◆ operator>=

bool operator>= ( const DateTime & lhs,
const DateTime & rhs )
friend

Member Data Documentation

◆ tm_

std::tm console::DateTime::tm_ {}
private

The documentation for this class was generated from the following file: