TpDateTime 类
V0.2.1TpUtils
2025-11-14
介绍
日期时间复合类,提供精确到毫秒的日期时间操作
基础信息
| 头文件: | include <TpDateTime.h> |
|---|---|
| Cmake: | None |
| 发布版本: | V0.2.1 |
| 继承类: | |
| 派生类: |
Public 成员函数
| TpDateTime() | ||
|---|---|---|
| ~TpDateTime() | ||
| TpDate TpDateTime::date | date() const | 获取日期部分 |
| TpTime | time() const | 获取时间部分 |
| toMSecsSinceEpoch() const | 转换为UTC毫秒时间戳 | |
| toSecsSinceEpoch() const | 转换为UTC秒时间戳 | |
| setMSecsSinceEpoch(int64_t msecs) | 通过UTC毫秒时间戳设置时间 | |
| setSecsSinceEpoch(int64_t secs) | 通过UTC秒时间戳设置时间 | |
| setDate(const TpDate &date) | 设置日期部分(保持时间不变) | |
| setTime(const TpTime &time) | 设置时间部分(保持日期不变) | |
| TpString | toString(const TpString &format) const | 格式化输出日期时间 |
| TpDateTime TpDateTime::addDays | addDays(int64_t days) const | 增加指定天数 |
| TpDateTime TpDateTime::addMonths | addMonths(int32_t months) const | 增加指定月数 |
| TpDateTime TpDateTime::addYears | addYears(int32_t years) const | 增加指定年数 |
| TpDateTime TpDateTime::addSecs | addSecs(int64_t secs) const | 增加指定秒数 |
| TpDateTime TpDateTime::addMSecs | addMSecs(int64_t msecs) const | 增加指定毫秒数 |
| daysTo(const TpDateTime &other) const | 计算到目标日期的天数差 | |
| secsTo(const TpDateTime &other) const | 计算到目标时间的秒数差 | |
| msecsTo(const TpDateTime &other) const | 计算到目标时间的毫秒数差 | |
| TpDateTime & TpDateTime::operator= | operator=(const TpDateTime &other) noexcept | |
| operator==(const TpDateTime &other) const | ||
| operator<(const TpDateTime &other) const | ||
| operator!=(const TpDateTime &other) const | ||
| operator<=(const TpDateTime &other) const | ||
| operator>(const TpDateTime &other) const | ||
| operator>=(const TpDateTime &other) const | ||
| static TpDateTime TpDateTime::currentDateTime | currentDateTime() | 获取当前系统日期时间(本地时区) |
| static TpDateTime TpDateTime::fromString | fromString(const TpString &s, const TpString &format) | 从格式字符串解析日期时间 |
| static TpDateTime TpDateTime::fromMSecsSinceEpoch | fromMSecsSinceEpoch(int64_t msecs) | 从毫秒时间戳创建对象(UTC时区) |
| static TpDateTime TpDateTime::fromSecsSinceEpoch | fromSecsSinceEpoch(int64_t secs) | 从秒时间戳创建对象(UTC时区) |
| currentMSecsSinceEpoch() noexcept | 获取当前UTC毫秒时间戳 | |
| currentSecsSinceEpoch() noexcept | 获取当前UTC秒时间戳 |
成员函数说明
TpDateTime::TpDateTime()
暂无注释...
TpDateTime::~TpDateTime()
暂无注释...
TpDate TpDateTime::date() const
获取日期部分...
获取日期部分
返回值:日期对象副本
TpTime TpDateTime::time() const
获取时间部分...
获取时间部分
返回值:时间对象副本
int64_t TpDateTime::toMSecsSinceEpoch() const
转换为UTC毫秒时间戳...
转换为UTC毫秒时间戳
返回值:自epoch的毫秒数
int64_t TpDateTime::toSecsSinceEpoch() const
转换为UTC秒时间戳...
转换为UTC秒时间戳
返回值:自epoch的秒数
void TpDateTime::setMSecsSinceEpoch(int64_t msecs)
通过UTC毫秒时间戳设置时间...
通过UTC毫秒时间戳设置时间
参数:msecs: 自epoch的毫秒数
void TpDateTime::setSecsSinceEpoch(int64_t secs)
通过UTC秒时间戳设置时间...
通过UTC秒时间戳设置时间
参数:secs: 自epoch的秒数
void TpDateTime::setDate(const TpDate &date)
设置日期部分(保持时间不变)...
设置日期部分(保持时间不变)
参数:date: 新日期对象
void TpDateTime::setTime(const TpTime &time)
设置时间部分(保持日期不变)...
设置时间部分(保持日期不变)
参数:time: 新时间对象
TpString TpDateTime::toString(const TpString &format) const
格式化输出日期时间...
格式化输出日期时间
参数:format: 格式字符串,如"yyyy-MM-dd HH:mm:ss.zzz"
返回值:格式化后的字符串
TpDateTime TpDateTime::addDays(int64_t days) const
增加指定天数...
增加指定天数
参数:days: 要增加的天数(可为负数)
返回值:新日期时间对象
TpDateTime TpDateTime::addMonths(int32_t months) const
增加指定月数...
增加指定月数
参数:months: 要增加的月数(可为负数)
返回值:新日期时间对象(自动调整月末日期)
TpDateTime TpDateTime::addYears(int32_t years) const
增加指定年数...
增加指定年数
参数:years: 要增加的年数(可为负数)
返回值:新日期时间对象(自动处理闰年)
TpDateTime TpDateTime::addSecs(int64_t secs) const
增加指定秒数...
增加指定秒数
参数:secs: 要增加的秒数(可为负数)
返回值:新日期时间对象
TpDateTime TpDateTime::addMSecs(int64_t msecs) const
增加指定毫秒数...
增加指定毫秒数
参数:msecs: 要增加的毫秒数(可为负数)
返回值:新日期时间对象
int64_t TpDateTime::daysTo(const TpDateTime &other) const
计算到目标日期的天数差...
计算到目标日期的天数差
参数:other: 目标日期时间
返回值:间隔天数
int64_t TpDateTime::secsTo(const TpDateTime &other) const
计算到目标时间的秒数差...
计算到目标时间的秒数差
参数:other: 目标日期时间
返回值:间隔秒数
int64_t TpDateTime::msecsTo(const TpDateTime &other) const
计算到目标时间的毫秒数差...
计算到目标时间的毫秒数差
参数:other: 目标日期时间
返回值:间隔毫秒数
TpDateTime & TpDateTime::operator=(const TpDateTime &other) noexcept
暂无注释...
bool TpDateTime::operator==(const TpDateTime &other) const
暂无注释...
bool TpDateTime::operator<(const TpDateTime &other) const
暂无注释...
bool TpDateTime::operator!=(const TpDateTime &other) const
暂无注释...
bool TpDateTime::operator<=(const TpDateTime &other) const
暂无注释...
bool TpDateTime::operator>(const TpDateTime &other) const
暂无注释...
bool TpDateTime::operator>=(const TpDateTime &other) const
暂无注释...
static TpDateTime TpDateTime::currentDateTime()
获取当前系统日期时间(本地时区)...
获取当前系统日期时间(本地时区)
返回值:当前日期时间对象
static TpDateTime TpDateTime::fromString(const TpString &s, const TpString &format)
从格式字符串解析日期时间...
从格式字符串解析日期时间
参数:s: 日期时间字符串,如 "2023-08-25 14:30:45.500"
参数:format: 格式模板,支持:
- 日期部分:yyyy(年), MM(月), dd(日)
- 时间部分:HH(时), mm(分), ss(秒), zzz(毫秒)
参数:std::invalid_argument: 格式不匹配时抛出
返回值:解析后的日期时间对象
static TpDateTime TpDateTime::fromMSecsSinceEpoch(int64_t msecs)
从毫秒时间戳创建对象(UTC时区)...
从毫秒时间戳创建对象(UTC时区)
参数:msecs: 自1970-01-01T00:00:00 UTC的毫秒数
返回值:对应UTC日期时间对象
static TpDateTime TpDateTime::fromSecsSinceEpoch(int64_t secs)
从秒时间戳创建对象(UTC时区)...
从秒时间戳创建对象(UTC时区)
参数:secs: 自1970-01-01T00:00:00 UTC的秒数
返回值:对应UTC日期时间对象
static int64_t TpDateTime::currentMSecsSinceEpoch() noexcept
获取当前UTC毫秒时间戳...
获取当前UTC毫秒时间戳
返回值:当前时刻的毫秒时间戳
static int64_t TpDateTime::currentSecsSinceEpoch() noexcept
获取当前UTC秒时间戳...
获取当前UTC秒时间戳
返回值:当前时刻的秒时间戳
