开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
开源项目 > 程序开发 > 常用工具包 &&
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (1)
标签 (2)
master
ZCC0.01
some-old-version
master
分支 (1)
标签 (2)
master
ZCC0.01
some-old-version
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (1)
标签 (2)
master
ZCC0.01
some-old-version
lib-zc
/
cpp_src
/
stdlib
/
time.cpp
lib-zc
/
cpp_src
/
stdlib
/
time.cpp
time.cpp 10.06 KB
一键复制 编辑 原始数据 按行查看 历史
linuxmail 提交于 2026年06月22日 11:21 +08:00 . 时间,日历,IMAP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
/*
* ================================
* eli960@qq.com
* http://linuxmail.cn/
* 2024年02月18日
* ================================
*/
#include "zcc/zcc_stdlib.h"
#include <mutex>
#include <unordered_map>
#include <chrono>
#include <ctime>
#include <fstream>
#include <vector>
#include <cstring>
#include <time.h>
#ifdef _WIN64
#include <timezoneapi.h>
#else // _WIN64
#include <poll.h>
#include <limits.h>
#include <unistd.h>
#endif // _WIN64
zcc_namespace_begin;
/**
* @brief 根据传入的星期几的数字返回对应的缩写字符串
*
* @param day 星期几的数字,范围0-6,0表示周日,6表示周六
* @return const char* 对应的星期缩写字符串
*/
const char *get_day_abbr_of_week(int day)
{
// 定义星期缩写数组
static const char *days[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
// %w
// 如果传入的数字小于0,将其置为0
if (day < 0)
{
day = 0;
}
// 如果传入的数字大于6,将其置为6
if (day > 6)
{
day = 6;
}
return days[day];
}
const char *get_day_name_of_week(int day)
{
// 定义星期缩写数组
static const char *days[] = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
// %w
// 如果传入的数字小于0,将其置为0
if (day < 0)
{
day = 0;
}
// 如果传入的数字大于6,将其置为6
if (day > 6)
{
day = 6;
}
return days[day];
}
/**
* @brief 根据传入的月份数字返回对应的缩写字符串
*
* @param month 月份数字,范围0-11,0表示一月,11表示十二月
* @return const char* 对应的月份缩写字符串
*/
const char *get_month_abbr(int month)
{
// 定义月份缩写数组
static const char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
// %m - 1
// 如果传入的数字小于0,将其置为0
if (month < 0)
{
month = 0;
}
// 如果传入的数字大于11,将其置为11
if (month > 11)
{
month = 11;
}
return months[month];
}
const char *get_month_name(int month)
{
// 定义月份名称数组
static const char *months[] = {"一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"};
// %m - 1
// 如果传入的数字小于0,将其置为0
if (month < 0)
{
month = 0;
}
// 如果传入的数字大于11,将其置为11
if (month > 11)
{
month = 11;
}
return months[month];
}
int get_weekday_between(int day1 /* 0 - 6 */, int day2)
{
int diff = abs(day2 - day1);
return diff < 7 - diff ? diff : 7 - diff;
}
/**
* @brief 获取当前时间的秒和微秒部分
*
* @return timeofday 包含秒和微秒的结构体
*/
timeofday gettimeofday()
{
timeofday r;
// 获取当前系统时间
auto now = std::chrono::system_clock::now();
// 获取从纪元开始到现在的持续时间
auto duration_since_epoch = now.time_since_epoch();
// 将持续时间转换为微秒
auto millis = std::chrono::duration_cast<std::chrono::microseconds>(duration_since_epoch).count();
r.tv_sec = millis / (1000 * 1000);
r.tv_usec = millis % (1000 * 1000);
return r;
}
int64_t millisecond()
{
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count();
}
int64_t microsecond()
{
return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count();
}
/**
* @brief 让当前线程休眠指定的毫秒数
*
* @param delay 休眠时间(毫秒)
*/
void sleep_millisecond(int64_t delay)
{
#ifdef _WIN64
// 在Windows 64位系统下,使用标准库的sleep_for函数进行休眠
std::this_thread::sleep_for(std::chrono::milliseconds(delay));
#else // _WIN64
int64_t left = delay;
// 计算休眠结束的时间戳
int64_t end = millisecond() + left + 1;
while (left > 0)
{
// 使用poll函数进行等待
poll(0, 0, (int)left);
// 计算剩余的休眠时间
left = end - millisecond();
}
#endif // _WIN64
}
/**
* @brief 计算从当前时间到指定时间戳的剩余毫秒数
*
* @param stamp 指定的时间戳(毫秒)
* @return int64_t 从当前时间到指定时间戳的剩余毫秒数
*/
int64_t millisecond_to(int64_t stamp)
{
return stamp - millisecond();
}
/**
* @brief 获取当前时间的秒级时间戳
*
* @return int64_t 当前时间的秒级时间戳
*/
int64_t second(void)
{
// 获取当前系统时间
auto const clock_now = std::chrono::system_clock::now();
// 将系统时间转换为C标准时间
std::time_t time = std::chrono::system_clock::to_time_t(clock_now);
return time;
}
/**
* @brief 让当前线程休眠指定的秒数
*
* @param delay 休眠时间(秒)
*/
void sleep(int64_t delay)
{
// 调用sleep_millisecond函数,将秒数转换为毫秒数
sleep_millisecond(delay * 1000);
}
/**
* @brief 根据指定的时间戳生成符合RFC 7231标准的时间字符串
*
* @param t 时间戳
* @return std::string 符合RFC 7231标准的时间字符串
*/
std::string rfc7231_time(int64_t t)
{
char buf[128 + 1];
if (t == var_use_current_time)
{
t = second();
}
// 将时间戳转换为UTC时间结构体
std::tm *now_tm = gmtime(t);
// 格式化时间字符串
std::strftime(buf, 128, "%a, %d %b %Y %H:%M:%S GMT", now_tm);
return buf;
}
/**
* @brief 根据指定的时间戳生成符合RFC 822标准的时间字符串
*
* @param t 时间戳,如果小于1则使用当前时间
* @return std::string 符合RFC 822标准的时间字符串
*/
std::string rfc822_time(int64_t t)
{
std::string r;
char buf[128 + 1];
if (t == var_use_current_time)
{
t = second();
}
// 将时间戳转换为本地时间结构体
auto p = localtime(t);
// FIXME %Z
// std::strftime(buf, 128, "%a, %d %b %Y %H:%M:%S %z", p);
// 添加星期缩写
r.append(get_day_abbr_of_week(p->tm_wday));
// 添加月份和日期
std::strftime(buf, 128, ", %d ", p);
r.append(buf);
// 添加月份缩写
r.append(get_month_abbr(p->tm_mon));
// 添加剩余部分
std::strftime(buf, 128, " %Y %H:%M:%S %z", p);
r.append(buf);
return r;
}
std::string get_hh_mm(int64_t t)
{
if (t == var_use_current_time)
{
t = second();
}
auto p = localtime(t);
char buf[128 + 1];
std::sprintf(buf, "%02d:%02d", p->tm_hour, p->tm_min);
return buf;
}
int get_yyyyymmdd(int64_t t)
{
if (t == var_use_current_time)
{
t = second();
}
auto p = localtime(t);
return (p->tm_year + 1900) * 10000 + (p->tm_mon + 1) * 100 + p->tm_mday;
}
ZCC_LIB_API std::string get_simple_date(int64_t t)
{
if (t == var_use_current_time)
{
t = second();
}
char buf[128 + 1];
auto p = localtime(t);
std::strftime(buf, 128, "%Y-%m-%d", p);
return buf;
}
/**
* @brief 将时间戳转换为简单的日期时间字符串
*
* @param t 时间戳,如果小于1则使用当前时间
* @return std::string 格式为" %Y %H:%M:"的日期时间字符串
*/
std::string simple_date_time(int64_t t)
{
char buf[128 + 1];
if (t == var_use_current_time)
{
t = second();
}
auto p = localtime(t);
std::strftime(buf, 128, "%Y-%m-%d %H:%M", p);
return buf;
}
std::string simple_date_time_with_second(int64_t t)
{
char buf[128 + 1];
if (t == var_use_current_time)
{
t = second();
}
auto p = localtime(t);
std::strftime(buf, 128, "%Y-%m-%d %H:%M:%S", p);
return buf;
}
int64_t day_to_unix(int year, int month, int mday, const std::string &tzid)
{
std::string use_tzid = tzid.empty() ? get_current_timezone() : tzid;
if (use_tzid.empty())
{
use_tzid = "UTC";
}
std::tm base_tm = {};
base_tm.tm_year = year - 1900;
base_tm.tm_mon = month - 1;
base_tm.tm_mday = mday;
base_tm.tm_hour = 0;
base_tm.tm_min = 0;
base_tm.tm_sec = 0;
base_tm.tm_isdst = -1;
int64_t unix_scond = zcc::timegm(&base_tm);
timezone_info tzinfo = get_timezone_info(use_tzid, unix_scond);
unix_scond -= tzinfo.gmtoff;
return unix_scond;
}
int64_t day_to_unix(int64_t yyyymmdd, const std::string &tzid)
{
int year = yyyymmdd / 10000;
int month = (yyyymmdd / 100) % 100;
int mday = yyyymmdd % 100;
if (year <= 1900 || year >= 2100)
{
return var_invalid_time;
}
if (month < 1 || month > 12)
{
return var_invalid_time;
}
if (mday < 1 || mday > 31)
{
return var_invalid_time;
}
return day_to_unix(year, month, mday, tzid);
}
/**
* @brief 将tm结构体转换为UTC时间戳
*
* @param tm 指向tm结构体的指针
* @return int64_t UTC时间戳
*/
int64_t timegm(struct tm *tm)
{
#ifdef _WIN64
// 在Windows 64位系统下,使用mktime函数转换时间
int64_t t = (int64_t)::mktime(tm);
TIME_ZONE_INFORMATION tzi;
// 获取时区信息
GetTimeZoneInformation(&tzi);
// 减去时区偏移量
t -= tzi.Bias * 60;
return t;
#else // _WIN64
// 在非Windows系统下,使用系统的timegm函数
return ::timegm(tm);
#endif // _WIN64
}
int64_t timelocal(struct tm *tm)
{
#ifdef _WIN64
// 在Windows 64位系统下,使用mktime函数转换时间
return (int64_t)::mktime(tm);
#else // _WIN64
// 在非Windows系统下,使用系统的timegm函数
return ::timelocal(tm);
#endif // _WIN64
}
thread_local struct tm gmtime_static_buf;
struct tm *localtime(const int64_t unix_second)
{
#ifdef _WIN64
localtime_s(&gmtime_static_buf, (time_t *)&unix_second);
return &gmtime_static_buf;
#else
return ::localtime_r((time_t *)&unix_second, &gmtime_static_buf);
#endif
}
struct tm *gmtime(const int64_t unix_second)
{
#ifdef _WIN64
::gmtime_s(&gmtime_buf, (time_t *)&unix_second);
return &gmtime_buf;
#else
return ::gmtime_r((time_t *)&unix_second, &gmtime_static_buf);
#endif
}
zcc_namespace_end;
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

LIB-ZC 是一个Linux平台通用C/C++扩展库。兼容 Windows/Macos。 支持 GCC/CLANG/MINGW/MSVC。 支持 X64/AMD64/ARM64。 基本的数据结构封装,master/server服务框架,异步IO,协程,JSON解析,邮件解析,字符集探测,redis、memcache客户端,imap、pop客户端,httpd,websocket,sqlite3
MIT
使用 MIT 开源许可协议
取消

发行版

暂无发行版

开源评估指数源自 OSS-Compass 评估体系,评估体系围绕以下三个维度对项目展开评估:

1. 开源生态

  • 生产力:来评估开源项目输出软件制品和开源价值的能力。
  • 创新力:用于评估开源软件及其生态系统的多样化程度。
  • 稳健性:用于评估开源项目面对多变的发展环境,抵御内外干扰并自我恢复的能力。

2. 协作、人、软件

  • 协作:代表了开源开发行为中协作的程度和深度。
  • 人:观察开源项目核心人员在开源项目中的影响力,并通过第三方视角考察用户和开发者对开源项目的评价。
  • 软件:从开源项目对外输出的制品评估其价值最终落脚点。也是开源评估最"古老"的主流方向之一"开源软件" 的具体表现。

3. 评估模型

    基于"开源生态"与"协作、人、软件"的维度,找到与该目标直接或间接相关的可量化指标,对开源项目健康与生态进行量化评估,最终形成开源评估指数。

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/linuxmail/lib-zc.git
git@gitee.com:linuxmail/lib-zc.git
linuxmail
lib-zc
lib-zc
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /