ngl_server 1.0
基于 Actor 模型的 C++ 服务器框架
ngl::tools::time Namespace Reference

时间工具命名空间,提供时间相关的工具函数。 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 }
 时间偏移量(秒),用于模拟时间

Detailed Description

时间工具命名空间,提供时间相关的工具函数。

该命名空间包含以下功能:

  • 时间单位转换和计算
  • 日历组件提取和验证
  • 时间格式化和解析
  • 日期比较和边界计算
  • 服务器时间同步支持

所有时间函数都支持逻辑服务器时间,可以设置时间偏移以实现服务器时间同步。

Enumeration Type Documentation

◆ anonymous enum

anonymous enum

时间单位常量定义。

定义了常用的时间单位换算关系,便于时间计算。 所有常量都是编译期常量,可用于模板参数和编译期计算。

Function Documentation

◆ check_hour()

bool ngl::tools::time::check_hour ( int hour)

检查小时是否有效

小时校验函数。

Parameters
hour小时(0-23)
Returns
有效返回true,无效返回false
Parameters
hour小时值(0-23)
Returns
bool 小时值是否有效
Examples
E:/GitHub/ngl_server/public/cpp/tools/tools/tools_time.h.

◆ check_minute()

bool ngl::tools::time::check_minute ( int minute)

检查分钟是否有效

分钟校验函数。

Parameters
minute分钟(0-59)
Returns
有效返回true,无效返回false
Parameters
minute分钟值(0-59)
Returns
bool 分钟值是否有效
Examples
E:/GitHub/ngl_server/public/cpp/tools/tools/tools_time.h.

◆ check_month()

bool ngl::tools::time::check_month ( int amonth)

检查月份是否有效

月份校验函数。

Parameters
amonth月份(1-12)
Returns
有效返回true,无效返回false
Parameters
amonth月份值(1-12)
Returns
bool 月份值是否有效
Note
用于验证用户输入的月份值
Examples
E:/GitHub/ngl_server/public/cpp/tools/tools/tools_time.h.

◆ check_monthday()

bool ngl::tools::time::check_monthday ( int amonthday)

检查日是否有效

日期校验函数。

Parameters
amonthday日(1-31)
Returns
有效返回true,无效返回false
Parameters
amonthday日期值(1-31)
Returns
bool 日期值是否有效
Note
不考虑月份天数差异,仅检查范围
Examples
E:/GitHub/ngl_server/public/cpp/tools/tools/tools_time.h.

◆ check_sec()

bool ngl::tools::time::check_sec ( int sec)

检查秒是否有效

秒校验函数。

Parameters
sec秒(0-59)
Returns
有效返回true,无效返回false
Parameters
sec秒值(0-59)
Returns
bool 秒值是否有效
Examples
E:/GitHub/ngl_server/public/cpp/tools/tools/tools_time.h.

◆ check_week()

bool ngl::tools::time::check_week ( int aweek)

检查星期是否有效

星期校验函数。

Parameters
aweek星期(1-7,1为周一)
Returns
有效返回true,无效返回false
Parameters
aweek星期值(1-7,1表示星期一)
Returns
bool 星期值是否有效
Note
使用项目约定的1-7范围,1表示星期一
Examples
E:/GitHub/ngl_server/public/cpp/tools/tools/tools_time.h.

◆ checkutc() [1/2]

bool ngl::tools::time::checkutc ( time_t autc)

检查时间戳是否晚于当前时间

检查时间戳是否在当前UTC时间单位内。

Parameters
autc要检查的时间戳
Returns
晚于当前时间返回true
Parameters
autc时间戳
Returns
bool 是否在当前UTC时间单位内

◆ checkutc() [2/2]

bool ngl::tools::time::checkutc ( time_t autc1,
time_t autc2 )

检查第一个时间戳是否晚于第二个

检查两个时间戳是否在同一个UTC时间单位内。

Parameters
autc1第一个时间戳
autc2第二个时间戳
Returns
autc1晚于autc2返回true
Parameters
autc1第一个时间戳
autc2第二个时间戳
Returns
bool 是否在同一个UTC时间单位内
Examples
E:/GitHub/ngl_server/public/cpp/tools/tools/tools_time.h.

◆ getday() [1/3]

int ngl::tools::time::getday ( )

获取当前日

从当前逻辑时间提取日期组件。

Returns
日(1-31)
int 日期(1-31)
Examples
E:/GitHub/ngl_server/public/cpp/tools/tools/tools_time.h.

◆ getday() [2/3]

int ngl::tools::time::getday ( const tm * atm)

从tm结构获取日

提取日期组件。

Parameters
atmtm结构指针
Returns
日(1-31)
Parameters
atmtm结构指针
Returns
int 日期(1-31)

◆ getday() [3/3]

int ngl::tools::time::getday ( time_t curr)

获取时间戳的日

从时间戳提取日期组件。

Parameters
curr时间戳
Returns
日(1-31)
Parameters
curr时间戳
Returns
int 日期(1-31)

◆ gethour() [1/3]

int ngl::tools::time::gethour ( )

获取当前小时

从当前逻辑时间提取小时组件。

Returns
小时(0-23)
int 小时(0-23)
Examples
E:/GitHub/ngl_server/public/cpp/tools/tools/tools_time.h.

◆ gethour() [2/3]

int ngl::tools::time::gethour ( const tm * atm)

从tm结构获取小时

提取小时组件。

Parameters
atmtm结构指针
Returns
小时(0-23)
Parameters
atmtm结构指针
Returns
int 小时(0-23)

◆ gethour() [3/3]

int ngl::tools::time::gethour ( time_t curr)

获取时间戳的小时

从时间戳提取小时组件。

Parameters
curr时间戳
Returns
小时(0-23)
Parameters
curr时间戳
Returns
int 小时(0-23)

◆ getmin() [1/3]

int ngl::tools::time::getmin ( )

获取当前分钟

从当前逻辑时间提取分钟组件。

Returns
分钟(0-59)
int 分钟(0-59)
Examples
E:/GitHub/ngl_server/public/cpp/tools/tools/tools_time.h.

◆ getmin() [2/3]

int ngl::tools::time::getmin ( const tm * atm)

从tm结构获取分钟

提取分钟组件。

Parameters
atmtm结构指针
Returns
分钟(0-59)
Parameters
atmtm结构指针
Returns
int 分钟(0-59)

◆ getmin() [3/3]

int ngl::tools::time::getmin ( time_t curr)

获取时间戳的分钟

从时间戳提取分钟组件。

Parameters
curr时间戳
Returns
分钟(0-59)
Parameters
curr时间戳
Returns
int 分钟(0-59)

◆ getmonth() [1/3]

int ngl::tools::time::getmonth ( )

获取当前月份

从当前逻辑时间提取月份组件。

Returns
月份(1-12)
int 月份(1-12)
Examples
E:/GitHub/ngl_server/public/cpp/tools/tools/tools_time.h.

◆ getmonth() [2/3]

int ngl::tools::time::getmonth ( const tm * atm)

从tm结构获取月份

提取月份组件。

Parameters
atmtm结构指针
Returns
月份(1-12)

tm_mon范围是0-11,需要加1

Parameters
atmtm结构指针
Returns
int 月份(1-12)

◆ getmonth() [3/3]

int ngl::tools::time::getmonth ( time_t curr)

获取时间戳的月份

从时间戳提取月份组件。

Parameters
curr时间戳
Returns
月份(1-12)
Parameters
curr时间戳
Returns
int 月份(1-12)

◆ getmonthday() [1/2]

std::pair< bool, time_t > ngl::tools::time::getmonthday ( int amday,
int hour,
int minute,
int sec )

获取本月指定日的指定时刻(使用当前时间)

使用当前逻辑时间构建指定日期和时间的时间戳。

Parameters
amday日(1-31)
hour小时
minute分钟
sec
Returns
pair<bool, time_t>:first表示是否有效,second为时间戳
Parameters
amday日期(1-31)
hour小时(0-23)
minute分钟(0-59)
sec秒(0-59)
Returns
std::pair<bool, time_t> 第一个元素表示是否成功,第二个元素是时间戳
Note
使用当前逻辑服务器时间作为基准

◆ getmonthday() [2/2]

std::pair< bool, time_t > ngl::tools::time::getmonthday ( time_t utc,
int amday,
int hour,
int minute,
int sec )

获取本月指定日的指定时刻

构建指定日期和时间的时间戳。

Parameters
utc基准时间戳
amday日(1-31)
hour小时
minute分钟
sec
Returns
pair<bool, time_t>:first表示是否有效,second为时间戳

检查日期和时间的有效性

Parameters
utc基准时间戳
amday日期(1-31)
hour小时(0-23)
minute分钟(0-59)
sec秒(0-59)
Returns
std::pair<bool, time_t> 第一个元素表示是否成功,第二个元素是时间戳
Note
如果指定日期不存在(如2月30日),返回失败
Examples
E:/GitHub/ngl_server/public/cpp/tools/tools/tools_time.h.

◆ getms()

time_t ngl::tools::time::getms ( )

获取当前毫秒数(仅亚秒部分)

获取原始系统时间(毫秒)。

Returns
当前毫秒数(0-999)

用于需要亚秒精度的场景

Returns
time_t 原始系统时间(毫秒)
Note
返回本地系统时间的毫秒表示,用于高精度时间测量
See also
gettimems()
Examples
E:/GitHub/ngl_server/public/cpp/tools/tools/tools_time.h.

◆ getsec() [1/3]

int ngl::tools::time::getsec ( )

获取当前秒

从当前逻辑时间提取秒组件。

Returns
秒(0-59)
int 秒(0-59)
Examples
E:/GitHub/ngl_server/public/cpp/tools/tools/tools_time.h.

◆ getsec() [2/3]

int ngl::tools::time::getsec ( const tm * atm)

从tm结构获取秒

提取秒组件。

Parameters
atmtm结构指针
Returns
秒(0-59)
Parameters
atmtm结构指针
Returns
int 秒(0-59)

◆ getsec() [3/3]

int ngl::tools::time::getsec ( time_t curr)

获取时间戳的秒

从时间戳提取秒组件。

Parameters
curr时间戳
Returns
秒(0-59)
Parameters
curr时间戳
Returns
int 秒(0-59)

◆ getsecond2time() [1/4]

time_t ngl::tools::time::getsecond2time ( int hour,
int minute,
int sec )

获取今天或明天的指定时刻

从当前逻辑日期构建指定时间的时间戳。

Parameters
hour小时
minute分钟
sec
Returns
如果指定时刻已过,返回明天的该时刻;否则返回今天的该时刻

用于定时任务的调度

Parameters
hour小时(0-23)
minute分钟(0-59)
sec秒(0-59)
Returns
time_t 构建的时间戳
Note
使用当前逻辑服务器时间作为基准

◆ getsecond2time() [2/4]

time_t ngl::tools::time::getsecond2time ( int minute,
int sec )

获取当前小时或下一小时的指定时刻

从当前逻辑小时构建指定时间的时间戳。

Parameters
minute分钟
sec
Returns
如果指定时刻已过,返回下一小时的该时刻;否则返回当前小时的该时刻
Parameters
minute分钟(0-59)
sec秒(0-59)
Returns
time_t 构建的时间戳
Note
使用当前逻辑服务器时间作为基准

◆ getsecond2time() [3/4]

time_t ngl::tools::time::getsecond2time ( int sec)

获取当前分钟或下一分钟的指定时刻

从当前逻辑分钟构建指定时间的时间戳。

Parameters
sec
Returns
如果指定时刻已过,返回下一分钟的该时刻;否则返回当前分钟的该时刻
Parameters
sec秒(0-59)
Returns
time_t 构建的时间戳
Note
使用当前逻辑服务器时间作为基准

◆ getsecond2time() [4/4]

time_t ngl::tools::time::getsecond2time ( time_t utc,
int hour,
int minute,
int sec )

获取指定时间当天的指定时刻

构建指定时间的时间戳。

Parameters
utc基准时间戳
hour小时(-1表示不修改)
minute分钟(-1表示不修改)
sec秒(-1表示不修改)
Returns
当天指定时刻的时间戳

将时间戳的时分秒设置为指定值

Parameters
utc基准时间戳
hour小时(0-23)
minute分钟(0-59)
sec秒(0-59)
Returns
time_t 构建的时间戳
Note
如果指定时间已过,则返回下一个匹配时间
Examples
E:/GitHub/ngl_server/public/cpp/tools/tools/tools_time.h.

◆ getspandays()

time_t ngl::tools::time::getspandays ( time_t curr,
time_t last )

计算两个时间戳之间的天数差

Parameters
curr结束时间戳
last开始时间戳
Returns
天数差(整数)

将两个时间戳都归一化到当天0点,然后计算差值

Examples
E:/GitHub/ngl_server/public/cpp/tools/tools/tools_time.h.

◆ getsystime()

time_t ngl::tools::time::getsystime ( )

获取系统真实时间(不带偏移)

获取原始系统时间(不含偏移)。

Returns
系统时间戳(秒)

忽略m_offset,返回真实系统时间

Returns
time_t 原始系统时间(秒)
Note
返回本地系统时间,不包含任何偏移
See also
gettime()
Examples
E:/GitHub/ngl_server/public/cpp/tools/tools/tools_time.h.

◆ gettime()

time_t ngl::tools::time::gettime ( )

获取当前时间(带偏移)

获取逻辑服务器时间。

Returns
当前时间戳(秒)

返回系统时间加上m_offset偏移量 用于测试环境的时间模拟

Returns
time_t 逻辑服务器时间(秒)
Note
返回的时间包含通过settime()设置的偏移,用于统一服务器时间
See also
settime(), getsystime()
Examples
E:/GitHub/ngl_server/public/cpp/tools/tools/tools_time.h.

◆ gettimems()

time_t ngl::tools::time::gettimems ( )

获取当前时间戳(毫秒)

获取逻辑服务器时间(毫秒)。

Returns
当前时间戳(毫秒)

使用system_clock获取高精度时间

Returns
time_t 逻辑服务器时间(毫秒)
Note
返回的时间包含通过settime()设置的偏移,用于高精度时间测量
See also
gettime(), settime()
Examples
E:/GitHub/ngl_server/public/cpp/tools/tools/tools_time.h.

◆ gettm()

void ngl::tools::time::gettm ( time_t curr,
tm & atm )

将时间戳转换为tm结构

将时间戳转换为tm结构。

Parameters
curr时间戳
atm[out] 存储结果的tm结构

跨平台封装,Windows使用localtime_s,其他平台使用localtime_r

Parameters
curr时间戳
atm输出参数,tm结构引用
Note
使用本地时间转换
Examples
E:/GitHub/ngl_server/public/cpp/tools/tools/tools_time.h.

◆ getutcbyhour()

int ngl::tools::time::getutcbyhour ( time_t utc,
int hour )

获取指定小时数后的整点时间

获取指定小时的下一个时间戳。

Parameters
utc基准时间戳
hour小时数(可为负数)
Returns
整点时间戳

将时间戳加上指定小时数,然后归一化到整点

Parameters
utc基准时间戳
hour小时(0-23)
Returns
int 时间戳
Note
返回在utc之后且具有请求小时的时间戳
Examples
E:/GitHub/ngl_server/public/cpp/tools/tools/tools_time.h.

◆ getutcbymin()

int ngl::tools::time::getutcbymin ( time_t utc,
int amin )

获取指定分钟数后的整分时间

获取指定分钟的下一个时间戳。

Parameters
utc基准时间戳
amin分钟数(可为负数)
Returns
整分时间戳

将时间戳加上指定分钟数,然后归一化到整分

Parameters
utc基准时间戳
amin分钟(0-59)
Returns
int 时间戳
Note
返回在utc之后且具有请求分钟的时间戳
Examples
E:/GitHub/ngl_server/public/cpp/tools/tools/tools_time.h.

◆ getweekday() [1/6]

int ngl::tools::time::getweekday ( )

获取当前星期几

从当前逻辑时间提取星期组件。

Returns
星期几(0-6,0为周日)
int 星期几(0-6,0表示星期日)
Examples
E:/GitHub/ngl_server/public/cpp/tools/tools/tools_time.h.

◆ getweekday() [2/6]

int ngl::tools::time::getweekday ( const tm * atm)

从tm结构获取星期几

提取星期组件。

Parameters
atmtm结构指针
Returns
星期几(0-6,0为周日)
Parameters
atmtm结构指针
Returns
int 星期几(0-6,0表示星期日)

◆ getweekday() [3/6]

time_t ngl::tools::time::getweekday ( int aweek,
int hour,
int minute,
int sec )

获取本周指定星期几的指定时刻

构建请求的星期和时间的下一个时间戳。

Parameters
aweek星期几(1-7)
hour小时
minute分钟
sec
Returns
指定星期几的指定时刻时间戳
Parameters
aweek星期几(0-6,0表示星期日)
hour小时(0-23)
minute分钟(0-59)
sec秒(0-59)
Returns
time_t 构建的时间戳
Note
使用当前逻辑服务器时间作为基准

◆ getweekday() [4/6]

int ngl::tools::time::getweekday ( time_t curr)

获取时间戳的星期几

从时间戳提取星期组件。

Parameters
curr时间戳
Returns
星期几(0-6,0为周日)
Parameters
curr时间戳
Returns
int 星期几(0-6,0表示星期日)

◆ getweekday() [5/6]

void ngl::tools::time::getweekday ( time_t curr,
int & weekday,
int & hour,
int & minute )

获取时间戳的星期、小时和分钟

同时提取星期、小时和分钟组件。

Parameters
curr时间戳
weekday[out] 星期几(0-6,0为周日)
hour[out] 小时(0-23)
minute[out] 分钟(0-59)
curr时间戳
weekday输出参数,星期几(0-6)
hour输出参数,小时(0-23)
minute输出参数,分钟(0-59)

◆ getweekday() [6/6]

time_t ngl::tools::time::getweekday ( time_t utc,
int aweek,
int hour,
int minute,
int sec )

获取指定星期几的指定时刻

构建特定星期的时间戳。

Parameters
utc基准时间戳
aweek星期几(1-7,1为周一)
hour小时
minute分钟
sec
Returns
指定星期几的指定时刻时间戳

计算本周指定星期几的指定时刻

Parameters
utc基准时间戳
aweek星期几(1-7,1表示星期一)
hour小时(0-23)
minute分钟(0-59)
sec秒(0-59)
Returns
time_t 构建的时间戳
Note
如果指定时间已过,则返回下一个匹配时间

◆ getyear() [1/3]

int ngl::tools::time::getyear ( )

获取当前年份

从当前逻辑时间提取年份组件。

Returns
年份
int 年份
Examples
E:/GitHub/ngl_server/public/cpp/tools/tools/tools_time.h.

◆ getyear() [2/3]

int ngl::tools::time::getyear ( const tm * atm)

从tm结构获取年份

提取年份组件。

Parameters
atmtm结构指针
Returns
年份(如2024)

tm_year是从1900开始的偏移量

Parameters
atmtm结构指针
Returns
int 年份

◆ getyear() [3/3]

int ngl::tools::time::getyear ( time_t curr)

获取时间戳的年份

从时间戳提取年份组件。

Parameters
curr时间戳
Returns
年份
Parameters
curr时间戳
Returns
int 年份

◆ isleapyear()

bool ngl::tools::time::isleapyear ( int year)

判断是否为闰年

检查是否为闰年。

Parameters
year年份
Returns
闰年返回true,否则返回false

闰年规则:能被4整除但不能被100整除,或者能被400整除

Parameters
year年份
Returns
bool 是否为闰年
Note
闰年规则:能被4整除但不能被100整除,或者能被400整除
Examples
E:/GitHub/ngl_server/public/cpp/tools/tools/tools_time.h.

◆ issameday() [1/2]

bool ngl::tools::time::issameday ( time_t a1,
time_t a2 )

判断两个时间戳是否在同一天

检查两个时间戳是否在同一天。

Parameters
a1第一个时间戳
a2第二个时间戳
Returns
在同一天返回true,否则返回false

将两个时间戳都归一化到当天0点进行比较

Parameters
a1第一个时间戳
a2第二个时间戳
Returns
bool 是否在同一天
Note
比较时考虑逻辑服务器时间偏移
Examples
E:/GitHub/ngl_server/public/cpp/tools/tools/tools_time.h.

◆ issameday() [2/2]

bool ngl::tools::time::issameday ( time_t autc)

判断时间戳是否与当前时间在同一天

检查给定时间戳是否与当前逻辑日期在同一天。

Parameters
autc要判断的时间戳
Returns
在同一天返回true,否则返回false
Parameters
autc要检查的时间戳
Returns
bool 是否在同一天
Note
使用当前逻辑服务器时间作为比较基准

◆ issameweek()

bool ngl::tools::time::issameweek ( time_t timestamp1,
time_t timestamp2 )

判断两个时间戳是否在同一周

检查两个时间戳是否在同一周内。

Parameters
timestamp1第一个时间戳
timestamp2第二个时间戳
Returns
在同一周返回true,否则返回false

将两个时间戳都归一化到其所在周一等效周窗口的起始时刻进行比较

Parameters
timestamp1第一个时间戳
timestamp2第二个时间戳
Returns
bool 是否在同一周内
Examples
E:/GitHub/ngl_server/public/cpp/tools/tools/tools_time.h.

◆ monthday()

bool ngl::tools::time::monthday ( int year,
int month,
int aday )

检查指定年月日是否有效

检查年月日组合是否有效。

Parameters
year年份
month月份(1-12)
aday日(1-31)
Returns
有效返回true,无效返回false

考虑闰年和每月天数

Parameters
year年份
month月份(1-12)
aday日期(1-31)
Returns
bool 日期是否有效
Note
考虑月份天数和闰年规则
Examples
E:/GitHub/ngl_server/public/cpp/tools/tools/tools_time.h.

◆ printf_time2str()

void ngl::tools::time::printf_time2str ( time_t anow,
const char * format )

打印格式化时间字符串到标准输出

将时间戳格式化并打印到标准输出。

Parameters
anow时间戳
format格式字符串,默认"%Y-%m-%d %H:%M:%S"

用于调试输出

Parameters
anow时间戳
format格式字符串(默认"%Y-%m-%d %H:%M:%S")
Note
主要用于调试输出
Examples
E:/GitHub/ngl_server/public/cpp/tools/tools/tools_time.h.

◆ settime()

bool ngl::tools::time::settime ( time_t sti)

设置模拟时间

Parameters
sti目标时间戳
Returns
成功设置返回true,目标时间早于当前时间返回false

计算时间偏移量,使gettime()返回目标时间 用于测试环境的时间模拟

Examples
E:/GitHub/ngl_server/public/cpp/tools/tools/tools_time.h.

◆ str2time()

time_t ngl::tools::time::str2time ( const char * astr,
const char * format )

将时间字符串解析为时间戳

将格式化的时间字符串解析为时间戳。

Parameters
astr时间字符串
format格式字符串,默认"%Y-%m-%d %H:%M:%S"
Returns
解析后的时间戳,失败返回-1

使用std::get_time进行解析

Parameters
astr时间字符串
format格式字符串(默认"%Y-%m-%d %H:%M:%S")
Returns
time_t 解析后的时间戳
Note
如果解析失败返回0
Examples
E:/GitHub/ngl_server/public/cpp/tools/tools/tools_time.h.

◆ test_isweek()

void ngl::tools::time::test_isweek ( )

测试issameweek函数

测试issameweek函数。

打印最近20天与今天是否在同一周的测试结果

Note
仅用于单元测试
Examples
E:/GitHub/ngl_server/public/cpp/tools/tools/tools_time.h.

◆ time2str() [1/3]

size_t ngl::tools::time::time2str ( char * str,
int len,
time_t anow,
const char * format )

将时间戳格式化为字符串

将时间戳格式化为字符串(写入缓冲区)。

Parameters
str[out] 存储结果的字符缓冲区
len缓冲区长度
anow时间戳(负值使用当前时间)
format格式字符串,默认"%Y-%m-%d %H:%M:%S"
Returns
写入的字符数,失败返回0

使用strftime进行格式化,支持跨平台

Parameters
str输出缓冲区
len缓冲区长度
anow时间戳
format格式字符串(默认"%Y-%m-%d %H:%M:%S")
Returns
size_t 写入的字符数(不包括终止符)
Note
如果缓冲区不足,会截断输出
Examples
E:/GitHub/ngl_server/public/cpp/tools/tools/tools_time.h.

◆ time2str() [2/3]

std::string ngl::tools::time::time2str ( const char * format)

将当前时间格式化为字符串

将当前逻辑时间格式化为字符串。

Parameters
format格式字符串
Returns
格式化后的时间字符串
Parameters
format格式字符串(默认"%Y-%m-%d %H:%M:%S")
Returns
std::string 格式化后的时间字符串
Note
使用当前逻辑服务器时间

◆ time2str() [3/3]

std::string ngl::tools::time::time2str ( time_t anow,
const char * format )

将时间戳格式化为std::string

将时间戳格式化为字符串。

Parameters
anow时间戳
format格式字符串
Returns
格式化后的时间字符串,失败返回空字符串
Parameters
anow时间戳
format格式字符串(默认"%Y-%m-%d %H:%M:%S")
Returns
std::string 格式化后的时间字符串

◆ timeout()

bool ngl::tools::time::timeout ( time_t abeg,
int32_t atimeoutms )

检查是否超时

Parameters
abeg开始时间戳(毫秒)
atimeoutms超时时间(毫秒)
Returns
超时返回true,否则返回false
Examples
E:/GitHub/ngl_server/public/cpp/tools/tools/tools_time.h.