|
ngl_server 1.0
基于 Actor 模型的 C++ 服务器框架
|
时间工具命名空间,提供时间相关的工具函数。 More...
Enumerations | |
| enum | { MILLISECOND = 1000 , MINUTES_SECOND = 1 * 60 , MINUTES_MILLISECOND = MINUTES_SECOND * MILLISECOND , HOUR_MINUTES = 1 * 60 , HOUR_SECOND = MINUTES_SECOND * HOUR_MINUTES , HOUR_MILLISECOND = HOUR_SECOND * MILLISECOND , DAY_HOUR = 24 , DAY_MINUTES = DAY_HOUR * HOUR_MINUTES , DAY_SECOND = DAY_HOUR * HOUR_SECOND , DAY_MILLISECOND = DAY_SECOND * MILLISECOND , WEEK_DAY = 7 , WEEK_HOUR = WEEK_DAY * DAY_HOUR , WEEK_MINUTES = WEEK_DAY * DAY_MINUTES , WEEK_SECOND = WEEK_DAY * DAY_SECOND , WEEK_MILLISECOND = WEEK_SECOND * MILLISECOND } |
| 时间单位常量定义。 More... | |
Functions | |
| bool | check_month (int amonth) |
| 检查月份是否有效 | |
| bool | check_monthday (int amonthday) |
| 检查日是否有效 | |
| bool | check_week (int aweek) |
| 检查星期是否有效 | |
| bool | check_hour (int hour) |
| 检查小时是否有效 | |
| bool | check_minute (int minute) |
| 检查分钟是否有效 | |
| bool | check_sec (int sec) |
| 检查秒是否有效 | |
| bool | timeout (time_t abeg, int32_t atimeoutms) |
| 检查是否超时 | |
| bool | settime (time_t sti) |
| 设置模拟时间 | |
| time_t | gettime () |
| 获取当前时间(带偏移) | |
| time_t | getsystime () |
| 获取系统真实时间(不带偏移) | |
| time_t | getms () |
| 获取当前毫秒数(仅亚秒部分) | |
| time_t | gettimems () |
| 获取当前时间戳(毫秒) | |
| bool | issameday (time_t a1, time_t a2) |
| 判断两个时间戳是否在同一天 | |
| bool | issameday (time_t autc) |
| 判断时间戳是否与当前时间在同一天 | |
| void | printf_time2str (time_t anow, const char *format) |
| 打印格式化时间字符串到标准输出 | |
| size_t | time2str (char *str, int len, time_t anow, const char *format) |
| 将时间戳格式化为字符串 | |
| std::string | time2str (time_t anow, const char *format) |
| 将时间戳格式化为std::string | |
| std::string | time2str (const char *format) |
| 将当前时间格式化为字符串 | |
| time_t | str2time (const char *astr, const char *format) |
| 将时间字符串解析为时间戳 | |
| time_t | getsecond2time (time_t utc, int hour, int minute, int sec) |
| 获取指定时间当天的指定时刻 | |
| time_t | getsecond2time (int hour, int minute, int sec) |
| 获取今天或明天的指定时刻 | |
| time_t | getsecond2time (int minute, int sec) |
| 获取当前小时或下一小时的指定时刻 | |
| time_t | getsecond2time (int sec) |
| 获取当前分钟或下一分钟的指定时刻 | |
| int | getutcbyhour (time_t utc, int hour) |
| 获取指定小时数后的整点时间 | |
| int | getutcbymin (time_t utc, int amin) |
| 获取指定分钟数后的整分时间 | |
| time_t | getweekday (time_t utc, int aweek, int hour, int minute, int sec) |
| 获取指定星期几的指定时刻 | |
| time_t | getweekday (int aweek, int hour, int minute, int sec) |
| 获取本周指定星期几的指定时刻 | |
| bool | isleapyear (int year) |
| 判断是否为闰年 | |
| bool | monthday (int year, int month, int aday) |
| 检查指定年月日是否有效 | |
| std::pair< bool, time_t > | getmonthday (time_t utc, int amday, int hour, int minute, int sec) |
| 获取本月指定日的指定时刻 | |
| std::pair< bool, time_t > | getmonthday (int amday, int hour, int minute, int sec) |
| 获取本月指定日的指定时刻(使用当前时间) | |
| time_t | getspandays (time_t curr, time_t last) |
| 计算两个时间戳之间的天数差 | |
| void | gettm (time_t curr, tm &atm) |
| 将时间戳转换为tm结构 | |
| void | getweekday (time_t curr, int &weekday, int &hour, int &minute) |
| 获取时间戳的星期、小时和分钟 | |
| int | getweekday (const tm *atm) |
| 从tm结构获取星期几 | |
| int | getweekday (time_t curr) |
| 获取时间戳的星期几 | |
| int | getday (const tm *atm) |
| 从tm结构获取日 | |
| int | getday (time_t curr) |
| 获取时间戳的日 | |
| int | getmonth (const tm *atm) |
| 从tm结构获取月份 | |
| int | getmonth (time_t curr) |
| 获取时间戳的月份 | |
| int | getyear (const tm *atm) |
| 从tm结构获取年份 | |
| int | getyear (time_t curr) |
| 获取时间戳的年份 | |
| int | gethour (const tm *atm) |
| 从tm结构获取小时 | |
| int | gethour (time_t curr) |
| 获取时间戳的小时 | |
| int | getmin (const tm *atm) |
| 从tm结构获取分钟 | |
| int | getmin (time_t curr) |
| 获取时间戳的分钟 | |
| int | getsec (const tm *atm) |
| 从tm结构获取秒 | |
| int | getsec (time_t curr) |
| 获取时间戳的秒 | |
| int | getweekday () |
| 获取当前星期几 | |
| int | gethour () |
| 获取当前小时 | |
| int | getmin () |
| 获取当前分钟 | |
| int | getsec () |
| 获取当前秒 | |
| int | getyear () |
| 获取当前年份 | |
| int | getmonth () |
| 获取当前月份 | |
| int | getday () |
| 获取当前日 | |
| bool | checkutc (time_t autc1, time_t autc2) |
| 检查第一个时间戳是否晚于第二个 | |
| bool | checkutc (time_t autc) |
| 检查时间戳是否晚于当前时间 | |
| bool | issameweek (time_t timestamp1, time_t timestamp2) |
| 判断两个时间戳是否在同一周 | |
| void | test_isweek () |
| 测试issameweek函数 | |
Variables | |
| std::atomic< time_t > | m_offset { 0 } |
| 时间偏移量(秒),用于模拟时间 | |
时间工具命名空间,提供时间相关的工具函数。
该命名空间包含以下功能:
所有时间函数都支持逻辑服务器时间,可以设置时间偏移以实现服务器时间同步。
| anonymous enum |
时间单位常量定义。
定义了常用的时间单位换算关系,便于时间计算。 所有常量都是编译期常量,可用于模板参数和编译期计算。
| bool ngl::tools::time::check_hour | ( | int | hour | ) |
检查小时是否有效
小时校验函数。
| hour | 小时(0-23) |
| hour | 小时值(0-23) |
| bool ngl::tools::time::check_minute | ( | int | minute | ) |
检查分钟是否有效
分钟校验函数。
| minute | 分钟(0-59) |
| minute | 分钟值(0-59) |
| bool ngl::tools::time::check_month | ( | int | amonth | ) |
检查月份是否有效
月份校验函数。
| amonth | 月份(1-12) |
| amonth | 月份值(1-12) |
| bool ngl::tools::time::check_monthday | ( | int | amonthday | ) |
检查日是否有效
日期校验函数。
| amonthday | 日(1-31) |
| amonthday | 日期值(1-31) |
| bool ngl::tools::time::check_sec | ( | int | sec | ) |
检查秒是否有效
秒校验函数。
| sec | 秒(0-59) |
| sec | 秒值(0-59) |
| bool ngl::tools::time::check_week | ( | int | aweek | ) |
检查星期是否有效
星期校验函数。
| aweek | 星期(1-7,1为周一) |
| aweek | 星期值(1-7,1表示星期一) |
| bool ngl::tools::time::checkutc | ( | time_t | autc | ) |
检查时间戳是否晚于当前时间
检查时间戳是否在当前UTC时间单位内。
| autc | 要检查的时间戳 |
| autc | 时间戳 |
| bool ngl::tools::time::checkutc | ( | time_t | autc1, |
| time_t | autc2 ) |
检查第一个时间戳是否晚于第二个
检查两个时间戳是否在同一个UTC时间单位内。
| autc1 | 第一个时间戳 |
| autc2 | 第二个时间戳 |
| autc1 | 第一个时间戳 |
| autc2 | 第二个时间戳 |
| int ngl::tools::time::getday | ( | ) |
获取当前日
从当前逻辑时间提取日期组件。
| int ngl::tools::time::getday | ( | const tm * | atm | ) |
从tm结构获取日
提取日期组件。
| atm | tm结构指针 |
| atm | tm结构指针 |
| int ngl::tools::time::getday | ( | time_t | curr | ) |
获取时间戳的日
从时间戳提取日期组件。
| curr | 时间戳 |
| curr | 时间戳 |
| int ngl::tools::time::gethour | ( | ) |
获取当前小时
从当前逻辑时间提取小时组件。
| int ngl::tools::time::gethour | ( | const tm * | atm | ) |
从tm结构获取小时
提取小时组件。
| atm | tm结构指针 |
| atm | tm结构指针 |
| int ngl::tools::time::gethour | ( | time_t | curr | ) |
获取时间戳的小时
从时间戳提取小时组件。
| curr | 时间戳 |
| curr | 时间戳 |
| int ngl::tools::time::getmin | ( | ) |
获取当前分钟
从当前逻辑时间提取分钟组件。
| int ngl::tools::time::getmin | ( | const tm * | atm | ) |
从tm结构获取分钟
提取分钟组件。
| atm | tm结构指针 |
| atm | tm结构指针 |
| int ngl::tools::time::getmin | ( | time_t | curr | ) |
获取时间戳的分钟
从时间戳提取分钟组件。
| curr | 时间戳 |
| curr | 时间戳 |
| int ngl::tools::time::getmonth | ( | ) |
获取当前月份
从当前逻辑时间提取月份组件。
| int ngl::tools::time::getmonth | ( | const tm * | atm | ) |
从tm结构获取月份
提取月份组件。
| atm | tm结构指针 |
tm_mon范围是0-11,需要加1
| atm | tm结构指针 |
| int ngl::tools::time::getmonth | ( | time_t | curr | ) |
获取时间戳的月份
从时间戳提取月份组件。
| curr | 时间戳 |
| curr | 时间戳 |
| std::pair< bool, time_t > ngl::tools::time::getmonthday | ( | int | amday, |
| int | hour, | ||
| int | minute, | ||
| int | sec ) |
获取本月指定日的指定时刻(使用当前时间)
使用当前逻辑时间构建指定日期和时间的时间戳。
| amday | 日(1-31) |
| hour | 小时 |
| minute | 分钟 |
| sec | 秒 |
| amday | 日期(1-31) |
| hour | 小时(0-23) |
| minute | 分钟(0-59) |
| sec | 秒(0-59) |
| std::pair< bool, time_t > ngl::tools::time::getmonthday | ( | time_t | utc, |
| int | amday, | ||
| int | hour, | ||
| int | minute, | ||
| int | sec ) |
获取本月指定日的指定时刻
构建指定日期和时间的时间戳。
| utc | 基准时间戳 |
| amday | 日(1-31) |
| hour | 小时 |
| minute | 分钟 |
| sec | 秒 |
检查日期和时间的有效性
| utc | 基准时间戳 |
| amday | 日期(1-31) |
| hour | 小时(0-23) |
| minute | 分钟(0-59) |
| sec | 秒(0-59) |
| time_t ngl::tools::time::getms | ( | ) |
获取当前毫秒数(仅亚秒部分)
获取原始系统时间(毫秒)。
用于需要亚秒精度的场景
| int ngl::tools::time::getsec | ( | ) |
获取当前秒
从当前逻辑时间提取秒组件。
| int ngl::tools::time::getsec | ( | const tm * | atm | ) |
从tm结构获取秒
提取秒组件。
| atm | tm结构指针 |
| atm | tm结构指针 |
| int ngl::tools::time::getsec | ( | time_t | curr | ) |
获取时间戳的秒
从时间戳提取秒组件。
| curr | 时间戳 |
| curr | 时间戳 |
| time_t ngl::tools::time::getsecond2time | ( | int | hour, |
| int | minute, | ||
| int | sec ) |
获取今天或明天的指定时刻
从当前逻辑日期构建指定时间的时间戳。
| hour | 小时 |
| minute | 分钟 |
| sec | 秒 |
用于定时任务的调度
| hour | 小时(0-23) |
| minute | 分钟(0-59) |
| sec | 秒(0-59) |
| time_t ngl::tools::time::getsecond2time | ( | int | minute, |
| int | sec ) |
获取当前小时或下一小时的指定时刻
从当前逻辑小时构建指定时间的时间戳。
| minute | 分钟 |
| sec | 秒 |
| minute | 分钟(0-59) |
| sec | 秒(0-59) |
| time_t ngl::tools::time::getsecond2time | ( | int | sec | ) |
获取当前分钟或下一分钟的指定时刻
从当前逻辑分钟构建指定时间的时间戳。
| sec | 秒 |
| sec | 秒(0-59) |
| time_t ngl::tools::time::getsecond2time | ( | time_t | utc, |
| int | hour, | ||
| int | minute, | ||
| int | sec ) |
获取指定时间当天的指定时刻
构建指定时间的时间戳。
| utc | 基准时间戳 |
| hour | 小时(-1表示不修改) |
| minute | 分钟(-1表示不修改) |
| sec | 秒(-1表示不修改) |
将时间戳的时分秒设置为指定值
| utc | 基准时间戳 |
| hour | 小时(0-23) |
| minute | 分钟(0-59) |
| sec | 秒(0-59) |
| time_t ngl::tools::time::getspandays | ( | time_t | curr, |
| time_t | last ) |
计算两个时间戳之间的天数差
| curr | 结束时间戳 |
| last | 开始时间戳 |
将两个时间戳都归一化到当天0点,然后计算差值
| time_t ngl::tools::time::getsystime | ( | ) |
获取系统真实时间(不带偏移)
获取原始系统时间(不含偏移)。
忽略m_offset,返回真实系统时间
| time_t ngl::tools::time::gettime | ( | ) |
获取当前时间(带偏移)
获取逻辑服务器时间。
返回系统时间加上m_offset偏移量 用于测试环境的时间模拟
| time_t ngl::tools::time::gettimems | ( | ) |
获取当前时间戳(毫秒)
获取逻辑服务器时间(毫秒)。
使用system_clock获取高精度时间
| void ngl::tools::time::gettm | ( | time_t | curr, |
| tm & | atm ) |
将时间戳转换为tm结构
将时间戳转换为tm结构。
| curr | 时间戳 |
| atm | [out] 存储结果的tm结构 |
跨平台封装,Windows使用localtime_s,其他平台使用localtime_r
| curr | 时间戳 |
| atm | 输出参数,tm结构引用 |
| int ngl::tools::time::getutcbyhour | ( | time_t | utc, |
| int | hour ) |
获取指定小时数后的整点时间
获取指定小时的下一个时间戳。
| utc | 基准时间戳 |
| hour | 小时数(可为负数) |
将时间戳加上指定小时数,然后归一化到整点
| utc | 基准时间戳 |
| hour | 小时(0-23) |
| int ngl::tools::time::getutcbymin | ( | time_t | utc, |
| int | amin ) |
获取指定分钟数后的整分时间
获取指定分钟的下一个时间戳。
| utc | 基准时间戳 |
| amin | 分钟数(可为负数) |
将时间戳加上指定分钟数,然后归一化到整分
| utc | 基准时间戳 |
| amin | 分钟(0-59) |
| int ngl::tools::time::getweekday | ( | ) |
获取当前星期几
从当前逻辑时间提取星期组件。
| int ngl::tools::time::getweekday | ( | const tm * | atm | ) |
从tm结构获取星期几
提取星期组件。
| atm | tm结构指针 |
| atm | tm结构指针 |
| time_t ngl::tools::time::getweekday | ( | int | aweek, |
| int | hour, | ||
| int | minute, | ||
| int | sec ) |
获取本周指定星期几的指定时刻
构建请求的星期和时间的下一个时间戳。
| aweek | 星期几(1-7) |
| hour | 小时 |
| minute | 分钟 |
| sec | 秒 |
| aweek | 星期几(0-6,0表示星期日) |
| hour | 小时(0-23) |
| minute | 分钟(0-59) |
| sec | 秒(0-59) |
| int ngl::tools::time::getweekday | ( | time_t | curr | ) |
获取时间戳的星期几
从时间戳提取星期组件。
| curr | 时间戳 |
| curr | 时间戳 |
| void ngl::tools::time::getweekday | ( | time_t | curr, |
| int & | weekday, | ||
| int & | hour, | ||
| int & | minute ) |
获取时间戳的星期、小时和分钟
同时提取星期、小时和分钟组件。
| curr | 时间戳 |
| weekday | [out] 星期几(0-6,0为周日) |
| hour | [out] 小时(0-23) |
| minute | [out] 分钟(0-59) |
| curr | 时间戳 |
| weekday | 输出参数,星期几(0-6) |
| hour | 输出参数,小时(0-23) |
| minute | 输出参数,分钟(0-59) |
| time_t ngl::tools::time::getweekday | ( | time_t | utc, |
| int | aweek, | ||
| int | hour, | ||
| int | minute, | ||
| int | sec ) |
获取指定星期几的指定时刻
构建特定星期的时间戳。
| utc | 基准时间戳 |
| aweek | 星期几(1-7,1为周一) |
| hour | 小时 |
| minute | 分钟 |
| sec | 秒 |
计算本周指定星期几的指定时刻
| utc | 基准时间戳 |
| aweek | 星期几(1-7,1表示星期一) |
| hour | 小时(0-23) |
| minute | 分钟(0-59) |
| sec | 秒(0-59) |
| int ngl::tools::time::getyear | ( | ) |
获取当前年份
从当前逻辑时间提取年份组件。
| int ngl::tools::time::getyear | ( | const tm * | atm | ) |
从tm结构获取年份
提取年份组件。
| atm | tm结构指针 |
tm_year是从1900开始的偏移量
| atm | tm结构指针 |
| int ngl::tools::time::getyear | ( | time_t | curr | ) |
获取时间戳的年份
从时间戳提取年份组件。
| curr | 时间戳 |
| curr | 时间戳 |
| bool ngl::tools::time::isleapyear | ( | int | year | ) |
判断是否为闰年
检查是否为闰年。
| year | 年份 |
闰年规则:能被4整除但不能被100整除,或者能被400整除
| year | 年份 |
| bool ngl::tools::time::issameday | ( | time_t | a1, |
| time_t | a2 ) |
判断两个时间戳是否在同一天
检查两个时间戳是否在同一天。
| a1 | 第一个时间戳 |
| a2 | 第二个时间戳 |
将两个时间戳都归一化到当天0点进行比较
| a1 | 第一个时间戳 |
| a2 | 第二个时间戳 |
| bool ngl::tools::time::issameday | ( | time_t | autc | ) |
判断时间戳是否与当前时间在同一天
检查给定时间戳是否与当前逻辑日期在同一天。
| autc | 要判断的时间戳 |
| autc | 要检查的时间戳 |
| bool ngl::tools::time::issameweek | ( | time_t | timestamp1, |
| time_t | timestamp2 ) |
判断两个时间戳是否在同一周
检查两个时间戳是否在同一周内。
| timestamp1 | 第一个时间戳 |
| timestamp2 | 第二个时间戳 |
将两个时间戳都归一化到其所在周一等效周窗口的起始时刻进行比较
| timestamp1 | 第一个时间戳 |
| timestamp2 | 第二个时间戳 |
| bool ngl::tools::time::monthday | ( | int | year, |
| int | month, | ||
| int | aday ) |
检查指定年月日是否有效
检查年月日组合是否有效。
| year | 年份 |
| month | 月份(1-12) |
| aday | 日(1-31) |
考虑闰年和每月天数
| year | 年份 |
| month | 月份(1-12) |
| aday | 日期(1-31) |
| void ngl::tools::time::printf_time2str | ( | time_t | anow, |
| const char * | format ) |
打印格式化时间字符串到标准输出
将时间戳格式化并打印到标准输出。
| anow | 时间戳 |
| format | 格式字符串,默认"%Y-%m-%d %H:%M:%S" |
用于调试输出
| anow | 时间戳 |
| format | 格式字符串(默认"%Y-%m-%d %H:%M:%S") |
| bool ngl::tools::time::settime | ( | time_t | sti | ) |
设置模拟时间
| sti | 目标时间戳 |
计算时间偏移量,使gettime()返回目标时间 用于测试环境的时间模拟
| time_t ngl::tools::time::str2time | ( | const char * | astr, |
| const char * | format ) |
将时间字符串解析为时间戳
将格式化的时间字符串解析为时间戳。
| astr | 时间字符串 |
| format | 格式字符串,默认"%Y-%m-%d %H:%M:%S" |
使用std::get_time进行解析
| astr | 时间字符串 |
| format | 格式字符串(默认"%Y-%m-%d %H:%M:%S") |
| void ngl::tools::time::test_isweek | ( | ) |
测试issameweek函数
测试issameweek函数。
打印最近20天与今天是否在同一周的测试结果
| size_t ngl::tools::time::time2str | ( | char * | str, |
| int | len, | ||
| time_t | anow, | ||
| const char * | format ) |
将时间戳格式化为字符串
将时间戳格式化为字符串(写入缓冲区)。
| str | [out] 存储结果的字符缓冲区 |
| len | 缓冲区长度 |
| anow | 时间戳(负值使用当前时间) |
| format | 格式字符串,默认"%Y-%m-%d %H:%M:%S" |
使用strftime进行格式化,支持跨平台
| str | 输出缓冲区 |
| len | 缓冲区长度 |
| anow | 时间戳 |
| format | 格式字符串(默认"%Y-%m-%d %H:%M:%S") |
| std::string ngl::tools::time::time2str | ( | const char * | format | ) |
将当前时间格式化为字符串
将当前逻辑时间格式化为字符串。
| format | 格式字符串 |
| format | 格式字符串(默认"%Y-%m-%d %H:%M:%S") |
| std::string ngl::tools::time::time2str | ( | time_t | anow, |
| const char * | format ) |
将时间戳格式化为std::string
将时间戳格式化为字符串。
| anow | 时间戳 |
| format | 格式字符串 |
| anow | 时间戳 |
| format | 格式字符串(默认"%Y-%m-%d %H:%M:%S") |
| bool ngl::tools::time::timeout | ( | time_t | abeg, |
| int32_t | atimeoutms ) |
检查是否超时
| abeg | 开始时间戳(毫秒) |
| atimeoutms | 超时时间(毫秒) |