You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Boost.DateTime:如何在任意时区创建指定时间的当日local_date_time对象?

使用Boost.DateTime构造指定时区当日指定时间的local_date_time对象

我正在使用Boost.DateTime,需要创建一个local_date_time对象,用来表示特定时区下当日的指定时间——该时区可以是任意时区,不限于UTC或系统时区。

举个具体场景:

  • 当America/New_York时区当前时间为2025年1月10日23:30:00时,我需要生成该时区当日16:30的对象,即2025年1月10日16:30:00;
  • 一小时后该时区时间变为2025年1月11日00:30:00,用同样逻辑要得到2025年1月11日16:30:00。

我原本尝试通过boost::local_time::local_sec_clock()获取目标时区的当前时间,再用它的日期部分加目标时间构造新对象,代码如下:

local_date_time TodayAtTime(time_zone_ptr timezone, time_duration targetTime)
{
  local_date_time now = local_sec_clock::local_time(timezone);
  return local_date_time(now.date(), targetTime, timezone, local_date_time::EXCEPTION_ON_ERROR);
}

但这个方法行不通,因为local_date_time::date()返回的是UTC的当日日期,而非该对象关联时区的本地日期。

目前我只能通过把now序列化到std::ostringstream,再解析字符串提取年月日来构造对象,这种方法过于繁琐。想问下Boost.DateTime有没有原生API可以实现这个需求?

内容的提问来源于stack exchange,提问作者rysto

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 00:12:14