C++ 使用 boost::date_time 获取当前时区当前时间的最简单方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2612938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:05:15  来源:igfitidea点击:

Simplest way to get current time in current timezone using boost::date_time?

c++boosttimezonedebianboost-date-time

提问by timday

If I do date +%H-%M-%Son the commandline (Debian/Lenny), I get a user-friendly (not UTC, not DST-less, the time a normal person has on their wristwatch) time printed.

如果我date +%H-%M-%S在命令行(Debian/Lenny)上这样做,我会打印一个用户友好的(不是 UTC,不是无 DST,正常人在手表上的时间)时间。

What's the simplest way to obtain the same thing with boost::date_time?

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

If I do this:

如果我这样做:

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;

Then msg.str()is an hour earlier than the time I want to see. I'm not sure whether this is because it's showing UTC or local timezone time without a DST correction (I'm in the UK).

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

What's the simplest way to modify the above to yield the DST corrected local timezone time ? I have an idea it involves boost::date_time:: c_local_adjustorbut can't figure it out from the examples.

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

回答by timday

This does what I want:

这做我想要的:

  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;

回答by daminetreg

While this is not using boost::date_time it's relatively easy with boost::locale, which is quite more adapted for this task. As your need is simply getting a formatted time from the current locale.

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

IMHO boost::date_time should be used when you deal with softwares like gantt/planning computations, were you have alot of date_time arithmetic. But simply for using time and doing some arithmetic on it, you will faster success with 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;
}

Right now it should output : 15-45-48. :)

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

回答by Xeverous

I haven't found other answers to be convenient enough, so here is an example that showcases how to get a local or universal time with full control of units:

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

#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();
}

Note: the time_of_daystruct has no .microseconds()or .nanoseconds()functions, there is only .fractional_seconds()which returns an integer that is a multiple of configuration-dependent unit. .num_fractional_digits()can be used to obtain precision information where 10^ frac_digitsis the number of fractional_secondsthat is equal to 1 second.

注意:time_of_day结构体没有.microseconds()or.nanoseconds()函数,只有.fractional_seconds()which 返回一个整数,它是配置相关单元的倍数。.num_fractional_digits()可以用来获得精确的信息,其中10^frac_digits是的数目fractional_seconds等于1秒。

To obtain configuration-independent sub-second units one can perform modulo with the total_ milli/micro/nano _seconds()functions as a workaround.

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