开源 企业版 高校版 私有云 模力方舟 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
/
timezone.cpp
lib-zc
/
cpp_src
/
stdlib
/
timezone.cpp
timezone.cpp 11.70 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 421 422 423 424 425 426 427 428 429 430 431
/*
* ================================
* 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>
#ifdef _WIN64
#include <timezoneapi.h>
#else // _WIN64
#include <poll.h>
#include <limits.h>
#include <unistd.h>
#endif // _WIN64
#ifdef __APPLE__
#include <sys/param.h>
#include <sys/sysctl.h>
#endif
zcc_namespace_begin;
/**
* 获取当前程序的时区(IANA格式)
* 优先级:1. TZ环境变量 2. 系统配置的时区
*
* @return 时区字符串,例如 "Asia/Shanghai" 或 "America/New_York"
* 如果获取失败,返回空字符串
*/
std::string get_current_timezone(const std::string &default_tzid)
{
// ========== 优先级1:检查 TZ 环境变量 ==========
const char *tz_env = std::getenv("TZ");
if (tz_env != nullptr && tz_env[0] != '0円')
{
std::string result(tz_env);
// 处理格式 ":Asia/Shanghai" 或 ":/usr/share/zoneinfo/Asia/Shanghai"
if (result.size() > 1 && result[0] == ':')
{
result = result.substr(1);
// 尝试提取 "zoneinfo/" 后面的部分
size_t zoneinfo_pos = result.find("zoneinfo/");
if (zoneinfo_pos != std::string::npos)
{
result = result.substr(zoneinfo_pos + 9);
}
}
return result;
}
// ========== 优先级2:获取系统配置的时区 ==========
#if defined(_WIN64)
// Windows:通过注册表获取时区,然后映射到 IANA 名称
// 这是一个简化的实现,完整映射表很长,这里只返回 Windows 时区名
TIME_ZONE_INFORMATION tzInfo;
DWORD result = GetTimeZoneInformation(&tzInfo);
if (result != TIME_ZONE_ID_INVALID)
{
// 将宽字符转换为 UTF-8 字符串
std::wstring wname(tzInfo.StandardName);
std::string name(wname.begin(), wname.end());
return name; // 返回 "China Standard Time" 等 Windows 格式
}
#else
// 方法1:读取 /etc/localtime 符号链接
char link_path[PATH_MAX] = {0};
ssize_t len = ::readlink("/etc/localtime", link_path, sizeof(link_path) - 1);
if (len != -1)
{
link_path[len] = '0円';
std::string tz_path(link_path);
// 查找 "zoneinfo/" 后面的部分作为时区名
size_t pos = tz_path.find("zoneinfo/");
if (pos != std::string::npos)
{
return tz_path.substr(pos + 9);
}
// 备选:查找 "posix/" 或 "right/" 等前缀
pos = tz_path.rfind('/');
if (pos != std::string::npos && pos + 1 < tz_path.length())
{
// 返回最后一部分作为时区名(不完全准确,但作为备选)
return tz_path.substr(pos + 1);
}
}
// 方法2:尝试读取 /etc/timezone (Debian/Ubuntu 等)
FILE *fp = fopen("/etc/timezone", "r");
if (fp != nullptr)
{
char buffer[256] = {0};
if (fgets(buffer, sizeof(buffer), fp) != nullptr)
{
// 去除末尾的换行符
char *newline = strchr(buffer, '\n');
if (newline)
{
*newline = '0円';
}
fclose(fp);
if (strlen(buffer) > 0)
{
return std::string(buffer);
}
}
fclose(fp);
}
// 方法3:尝试读取 /etc/sysconfig/clock (RHEL/CentOS 等)
fp = fopen("/etc/sysconfig/clock", "r");
if (fp != nullptr)
{
char line[256];
while (fgets(line, sizeof(line), fp) != nullptr)
{
// 查找 "ZONE=" 或 "TIMEZONE=" 行
if (strncmp(line, "ZONE=", 5) == 0 || strncmp(line, "TIMEZONE=", 9) == 0)
{
char *value = strchr(line, '=');
if (value != nullptr)
{
value++; // 跳过 '='
// 去除引号
if (*value == '"' || *value == '\'')
{
value++;
char *end = strrchr(value, '"');
if (end == nullptr)
end = strrchr(value, '\'');
if (end != nullptr)
*end = '0円';
}
// 去除换行符
char *newline = strchr(value, '\n');
if (newline)
*newline = '0円';
fclose(fp);
if (strlen(value) > 0)
{
return std::string(value);
}
}
}
}
fclose(fp);
}
#endif
#ifdef __APPLE__
// ---------- 方法4:macOS 专用 - 通过 systemsetup 命令获取 ----------
FILE *fp = popen("/usr/sbin/systemsetup -gettimezone 2>/dev/null", "r");
if (fp != nullptr)
{
char buffer[256] = {0};
if (fgets(buffer, sizeof(buffer), fp) != nullptr)
{
// 输出格式:"Time Zone: Asia/Shanghai"
std::string line(buffer);
size_t colon_pos = line.find(':');
if (colon_pos != std::string::npos)
{
std::string tz_name = line.substr(colon_pos + 1);
// 去除前后空白和换行符
size_t start = tz_name.find_first_not_of(" \t\n\r");
if (start != std::string::npos)
{
tz_name = tz_name.substr(start);
}
size_t end = tz_name.find_last_not_of(" \t\n\r");
if (end != std::string::npos)
{
tz_name = tz_name.substr(0, end + 1);
}
pclose(fp);
if (!tz_name.empty())
{
return tz_name;
}
}
}
pclose(fp);
}
// ---------- 方法5:macOS 专用 - 通过 sysctl 读取内核时区 ----------
char tz_name[256] = {0};
size_t size = sizeof(tz_name);
if (sysctlbyname("kern.timezone", tz_name, &size, nullptr, 0) == 0)
{
if (strlen(tz_name) > 0)
{
return std::string(tz_name);
}
}
#endif
// ========== 无法获取 ==========
return default_tzid;
}
// 独立函数:解析 tzfile 并返回完整数据
timezone_filedata parse_tzfile(const std::string &path)
{
timezone_filedata data;
data.error = true;
std::ifstream file(path, std::ios::binary);
if (!file)
{
return data;
}
char magic[5] = {0};
file.read(magic, 4);
if (std::string(magic) != "TZif")
{
data.file_magic_not_match = true;
return data;
}
char version;
file.read(&version, 1);
file.seekg(15, std::ios::cur); // reserved
auto read_int32 = [&file]()
{
int32_t val;
file.read(reinterpret_cast<char *>(&val), 4);
val = ((val & 0xFF) << 24) | ((val & 0xFF00) << 8) | ((val & 0xFF0000) >> 8) | ((val & 0xFF000000) >> 24);
return val;
};
int32_t ttisgmtcnt = read_int32();
int32_t ttisstdcnt = read_int32();
int32_t leapcnt = read_int32();
int32_t timecnt = read_int32();
int32_t typecnt = read_int32();
int32_t charcnt = read_int32();
data.transitions.resize(timecnt);
for (int i = 0; i < timecnt; ++i)
{
data.transitions[i] = read_int32();
}
data.types.resize(timecnt);
file.read(reinterpret_cast<char *>(data.types.data()), timecnt);
data.ttinfos.resize(typecnt);
for (int i = 0; i < typecnt; ++i)
{
data.ttinfos[i].gmtoff = read_int32();
file.read(reinterpret_cast<char *>(&data.ttinfos[i].isdst), 1);
file.read(reinterpret_cast<char *>(&data.ttinfos[i].abbrind), 1);
}
if (data.ttinfos.empty())
{
return data;
}
data.error = false;
return data;
}
const timezone_filedata &get_timezone_filedata(const std::string &timezone)
{
// static std::map<std::string, timezone_filedata> timezone_data_map;
// static std::map<std::string, std::string> timezone_alias_map;
static timezone_filedata nonexistent_data = {{}, {}, {}, true, true, false};
#include "timezone_data.hpp"
auto tzname = timezone;
zcc::tolower(tzname);
auto it_map = timezone_data_map.find(tzname);
if (it_map != timezone_data_map.end())
{
return it_map->second;
}
auto it_alias = timezone_alias_map.find(tzname);
if (it_alias != timezone_alias_map.end())
{
it_map = timezone_data_map.find(it_alias->second);
if (it_map != timezone_data_map.end())
{
return it_map->second;
}
}
if (tzname.find("中国") != std::string::npos)
{
tzname = "asia/shanghai";
}
else if (tzname.find("china") != std::string::npos)
{
tzname = "asia/shanghai";
}
else if (tzname.find("北京") != std::string::npos)
{
tzname = "asia/shanghai";
}
else if (tzname.find("beijing") != std::string::npos)
{
tzname = "asia/shanghai";
}
else
{
return nonexistent_data;
}
it_map = timezone_data_map.find(tzname);
if (it_map != timezone_data_map.end())
{
return it_map->second;
}
return nonexistent_data;
}
const timezone_info &get_timezone_info(const timezone_filedata &data, int64_t unix_second)
{
static timezone_info nonexistent_info = {};
if (data.error || data.ttinfos.empty())
{
return nonexistent_info;
}
if (data.transitions.empty())
{
return data.ttinfos[0];
}
else
{
int type_index = 0;
for (size_t i = 0; i < data.transitions.size(); ++i)
{
if (data.transitions[i] <= unix_second)
{
type_index = data.types[i];
}
else
{
break;
}
}
return data.ttinfos[type_index];
}
}
const timezone_info &get_timezone_info(const std::string &timezone, int64_t unix_second)
{
return get_timezone_info(get_timezone_filedata(timezone), unix_second);
}
extern thread_local struct tm gmtime_static_buf;
struct tm *gmtime_with_timezone(int64_t unix_second, const std::string &tzid)
{
if (tzid.empty())
{
return gmtime(unix_second);
}
const timezone_filedata &data = get_timezone_filedata(tzid);
if (data.error)
{
return nullptr;
}
timezone_info tzinfo = get_timezone_info(data, unix_second);
int64_t local_time = unix_second + tzinfo.gmtoff;
time_t lt = local_time;
#ifdef _WIN64
::gmtime_s(tm, &lt);
#else
if (!::gmtime_r(&lt, &gmtime_static_buf))
{
return nullptr;
}
#endif
gmtime_static_buf.tm_isdst = tzinfo.isdst;
#ifdef _WIN64
#else
gmtime_static_buf.tm_gmtoff = tzinfo.gmtoff;
#endif
return &gmtime_static_buf;
}
bool gmtime_with_timezone(int64_t unix_second, const std::string &tzid, struct tm *tm)
{
auto *p = gmtime_with_timezone(unix_second, tzid);
if (!p)
{
return false;
}
std::memcpy(tm, p, sizeof(struct tm));
return true;
}
int timezone_0800_offset(const std::string &timezone_0800)
{
int r = 0;
if (timezone_0800.empty())
{
return r;
}
const char *ps = timezone_0800.c_str();
bool tf = true;
if (*ps == '-')
{
tf = false;
ps++;
}
else if (*ps == '+')
{
ps++;
}
int len = (int)strlen(ps);
if (len != 4 && len != 6)
{
return 0;
}
r = ((ps[0] - '0') * 10 + (ps[1] - '0')) * 3600 + (ps[2] - '0') * 10 + (ps[3] - '0') * 60;
if (!tf)
{
r = -r;
}
return r;
}
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 によって変換されたページ (->オリジナル) /