【问题标题】:Simplest way to get current time in current timezone using boost::date_time?使用 boost::date_time 在当前时区获取当前时间的最简单方法?
【发布时间】:2010-04-10 10:00:23
【问题描述】:

如果我在命令行 (Debian/Lenny) 上执行 date +%H-%M-%S,我会打印出用户友好的时间(不是 UTC,不是 DST-less,普通人在手表上的时间)。

boost::date_time 获得相同东西的最简单方法是什么?

如果我这样做:

std::ostringstream msg;

boost::local_time::local_date_time t = 
  boost::local_time::local_sec_clock::local_time(
    boost::local_time::time_zone_ptr()
  );

boost::local_time::local_time_facet* lf(
  new boost::local_time::local_time_facet("%H-%M-%S")
);

msg.imbue(std::locale(msg.getloc(),lf));
msg << t;

那么msg.str() 比我想看的时间早一个小时。我不确定这是否是因为它显示 UTC 或本地时区时间而没有 DST 更正(我在英国)。

修改上述内容以产生 DST 校正的本地时区时间的最简单方法是什么?我有一个想法,它涉及boost::date_time:: c_local_adjustor,但无法从示例中弄清楚。

【问题讨论】:

  • 我相信这是 stackoverflow.com/questions/2492775/get-local-time-with-boost/… 的副本。短版:使用boost::posix_time 从系统时钟构造一个时间对象。这适用于当地时间(C 语言环境)。您是否可以为其他时区构建时间取决于您有哪些可用的语言环境。
  • 感谢指点;根本问题是我并没有真正理解 local_time/posix_time 之间的区别。

标签: c++ boost timezone debian boost-date-time


【解决方案1】:

这就是我想要的:

  namespace pt = boost::posix_time;
  std::ostringstream msg;
  const pt::ptime now = pt::second_clock::local_time();
  pt::time_facet*const f = new pt::time_facet("%H-%M-%S");
  msg.imbue(std::locale(msg.getloc(),f));
  msg << now;

【讨论】:

  • 他说的很简单。 Boost 日期/时间没有简单的方法。他们抽象和概括太多了。
  • 我认为这有点苛刻(尽管并非完全没有道理)。以上很简单(尽管所有命名空间都很冗长)。问题通常更多的是文档告诉您如何在您只想在街区四处走走时到达 Alpha Centauri。另一方面,不久前我想在 Python 中做同样的事情,而这不需要在 StackOverflow 上发布。
  • 不要让我开始我对 boost 的“posix time”的第一次体验,我认为从中获得 POSIX 时间戳是多么容易,我错了!话虽如此,如果没有 Boost,我们都会迷失方向
  • 如果您要打印很多次,您所做的也会非常低效。这是我看到的一个非常常见的错误。创建 facet 和 locale 一次,将其存储为静态,然后在需要打印内容时从 imbue() 开始。
【解决方案2】:

虽然这没有使用 boost::date_time,但使用 boost::locale 相对容易,它更适合这项任务。因为您的需要只是从当前语言环境中获取格式化的时间。

恕我直言,当您处理甘特图/计划计算等软件时,应该使用 boost::date_time,如果您有很多 date_time 算法。但只是为了使用时间并对其进行一些算术运算,使用 boost::locale 会更快地取得成功。

#include <iostream>
#include <boost/locale.hpp>

using namespace boost;

int main(int argc, char **argv) {
   locale::generator gen;
   std::locale::global(gen(""));

   locale::date_time now;
   std::cout.imbue(std::locale());       
   std::cout << locale::as::ftime("%H-%M-%S") << now << std::endl;

   return 0;
}

现在它应该输出:15-45-48。 :)

【讨论】:

    【解决方案3】:

    我还没有找到其他足够方便的答案,所以这里有一个示例,展示了如何在完全控制单位的情况下获取本地时间或通用时间:

    #include <boost/date_time/local_time/local_time.hpp>
    #include <boost/format.hpp>
    
    #include <iostream>
    
    int main()
    {
        auto const now = boost::posix_time::microsec_clock::local_time(); // or universal_time() for GMT+0
        if (now.is_special()) {
            // some error happened
            return 1;
        }
    
        // example timestamp (eg for logging)
        auto const t = now.time_of_day();
        boost::format formater("[%02d:%02d:%02d.%06d]");
        formater % t.hours() % t.minutes() % t.seconds() % (t.total_microseconds() % 1000000);
        std::cout << formater.str();
    }
    

    注意:time_of_day 结构没有.microseconds().nanoseconds() 函数,只有.fractional_seconds() 返回一个整数,该整数是配置相关单元的倍数。 .num_fractional_digits() 可用于获取精度信息,其中10 ^ frac_digits 是等于1 秒的fractional_seconds 的数量。

    要获得与配置无关的亚秒单位,可以使用total_ milli/micro/nano _seconds() 函数执行取模作为一种解决方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-15
      • 1970-01-01
      • 2015-11-27
      • 2019-12-15
      • 2018-06-06
      • 1970-01-01
      • 1970-01-01
      • 2011-07-08
      相关资源
      最近更新 更多