ngl_server 1.0
基于 Actor 模型的 C++ 服务器框架
tools_time.cpp File Reference

实现时间工具:纪元转换和格式化时间戳 More...

#include "tools/tools/tools_time.h"
#include <iostream>
#include <atomic>
#include <chrono>
#include <ctime>

Namespaces

namespace  ngl::tools
 工具函数命名空间。
namespace  ngl::tools::time
 时间工具命名空间,提供时间相关的工具函数。

Functions

bool ngl::tools::time::check_month (int amonth)
 检查月份是否有效
bool ngl::tools::time::check_monthday (int amonthday)
 检查日是否有效
bool ngl::tools::time::check_week (int aweek)
 检查星期是否有效
bool ngl::tools::time::check_hour (int hour)
 检查小时是否有效
bool ngl::tools::time::check_minute (int minute)
 检查分钟是否有效
bool ngl::tools::time::check_sec (int sec)
 检查秒是否有效
bool ngl::tools::time::timeout (time_t abeg, int32_t atimeoutms)
 检查是否超时
bool ngl::tools::time::settime (time_t sti)
 设置模拟时间
time_t ngl::tools::time::gettime ()
 获取当前时间(带偏移)
time_t ngl::tools::time::getsystime ()
 获取系统真实时间(不带偏移)
time_t ngl::tools::time::getms ()
 获取当前毫秒数(仅亚秒部分)
time_t ngl::tools::time::gettimems ()
 获取当前时间戳(毫秒)
bool ngl::tools::time::issameday (time_t a1, time_t a2)
 判断两个时间戳是否在同一天
bool ngl::tools::time::issameday (time_t autc)
 判断时间戳是否与当前时间在同一天
void ngl::tools::time::printf_time2str (time_t anow, const char *format)
 打印格式化时间字符串到标准输出
size_t ngl::tools::time::time2str (char *str, int len, time_t anow, const char *format)
 将时间戳格式化为字符串
std::string ngl::tools::time::time2str (time_t anow, const char *format)
 将时间戳格式化为std::string
std::string ngl::tools::time::time2str (const char *format)
 将当前时间格式化为字符串
time_t ngl::tools::time::str2time (const char *astr, const char *format)
 将时间字符串解析为时间戳
time_t ngl::tools::time::getsecond2time (time_t utc, int hour, int minute, int sec)
 获取指定时间当天的指定时刻
time_t ngl::tools::time::getsecond2time (int hour, int minute, int sec)
 获取今天或明天的指定时刻
time_t ngl::tools::time::getsecond2time (int minute, int sec)
 获取当前小时或下一小时的指定时刻
time_t ngl::tools::time::getsecond2time (int sec)
 获取当前分钟或下一分钟的指定时刻
int ngl::tools::time::getutcbyhour (time_t utc, int hour)
 获取指定小时数后的整点时间
int ngl::tools::time::getutcbymin (time_t utc, int amin)
 获取指定分钟数后的整分时间
time_t ngl::tools::time::getweekday (time_t utc, int aweek, int hour, int minute, int sec)
 获取指定星期几的指定时刻
time_t ngl::tools::time::getweekday (int aweek, int hour, int minute, int sec)
 获取本周指定星期几的指定时刻
bool ngl::tools::time::isleapyear (int year)
 判断是否为闰年
bool ngl::tools::time::monthday (int year, int month, int aday)
 检查指定年月日是否有效
std::pair< bool, time_t > ngl::tools::time::getmonthday (time_t utc, int amday, int hour, int minute, int sec)
 获取本月指定日的指定时刻
std::pair< bool, time_t > ngl::tools::time::getmonthday (int amday, int hour, int minute, int sec)
 获取本月指定日的指定时刻(使用当前时间)
time_t ngl::tools::time::getspandays (time_t curr, time_t last)
 计算两个时间戳之间的天数差
void ngl::tools::time::gettm (time_t curr, tm &atm)
 将时间戳转换为tm结构
void ngl::tools::time::getweekday (time_t curr, int &weekday, int &hour, int &minute)
 获取时间戳的星期、小时和分钟
int ngl::tools::time::getweekday (const tm *atm)
 从tm结构获取星期几
int ngl::tools::time::getweekday (time_t curr)
 获取时间戳的星期几
int ngl::tools::time::getday (const tm *atm)
 从tm结构获取日
int ngl::tools::time::getday (time_t curr)
 获取时间戳的日
int ngl::tools::time::getmonth (const tm *atm)
 从tm结构获取月份
int ngl::tools::time::getmonth (time_t curr)
 获取时间戳的月份
int ngl::tools::time::getyear (const tm *atm)
 从tm结构获取年份
int ngl::tools::time::getyear (time_t curr)
 获取时间戳的年份
int ngl::tools::time::gethour (const tm *atm)
 从tm结构获取小时
int ngl::tools::time::gethour (time_t curr)
 获取时间戳的小时
int ngl::tools::time::getmin (const tm *atm)
 从tm结构获取分钟
int ngl::tools::time::getmin (time_t curr)
 获取时间戳的分钟
int ngl::tools::time::getsec (const tm *atm)
 从tm结构获取秒
int ngl::tools::time::getsec (time_t curr)
 获取时间戳的秒
int ngl::tools::time::getweekday ()
 获取当前星期几
int ngl::tools::time::gethour ()
 获取当前小时
int ngl::tools::time::getmin ()
 获取当前分钟
int ngl::tools::time::getsec ()
 获取当前秒
int ngl::tools::time::getyear ()
 获取当前年份
int ngl::tools::time::getmonth ()
 获取当前月份
int ngl::tools::time::getday ()
 获取当前日
bool ngl::tools::time::checkutc (time_t autc1, time_t autc2)
 检查第一个时间戳是否晚于第二个
bool ngl::tools::time::checkutc (time_t autc)
 检查时间戳是否晚于当前时间
bool ngl::tools::time::issameweek (time_t timestamp1, time_t timestamp2)
 判断两个时间戳是否在同一周
void ngl::tools::time::test_isweek ()
 测试issameweek函数

Variables

std::atomic< time_t > ngl::tools::time::m_offset { 0 }
 时间偏移量(秒),用于模拟时间

Detailed Description

实现时间工具:纪元转换和格式化时间戳

提供完整的时间处理功能,包括:

  • 时间戳获取和转换
  • 时间格式化和解析
  • 日期计算(星期、月份、闰年等)
  • 时间比较和判断
    Note
    所有时间函数都支持时间偏移,用于测试环境的时间模拟