给定time_t或struct timeval,如何在当天获得EST/EDT午夜(当地时区)的timeval或time_t?假设当地时区为EST/EDT,给定time_t对应于美国东部时间/美国东部时间2010-11-30 08:00:00,预期答案为time_t,对应于2010-11-30 00:00:00 EST /美东时间
尝试1(错误:因为它不处理DST,并假设EST/EDT总是落后于UTC 5小时):
time_t RewindToMidnight ( const time_t temp_time_t_ )
{
  return ( (5*3600) + ((( temp_time_t_ - 5*3600 )/86400 ) * 86400) );
}
尝试2(错误:因为它返回的time_t对应于午夜时间而不是EST/EDT,本地时区):
time_t RewindToMidnight ( const time_t temp_time_t_ )
{
   boost::posix_time::ptime temp_ptime_ = boost::posix_time::from_time_t ( temp_time_t_ );
   boost::gregorian::date temp_date_ = temp_ptime_.date();
   boost::posix_time::ptime temp_ptime_midnight_ ( temp_date_,
                                                   boost::posix_time::time_duration ( 0, 0, 0 ) );
   return to_time_t ( temp_ptime_midnight_ );
}
time_t to_time_t ( const boost::posix_time::ptime & temp_ptime_ )
{
   boost::posix_time::ptime temp_epoch_ptime_(boost::gregorian::date(1970,1,1));
   boost::posix_time::time_duration::sec_type temp_sec_type_ …在Win32系统boost::date_time::microsec_clock()上实现使用ftime,它只提供毫秒分辨率:链接到doc
Stackoverflow上有一些问题/答案说明这一点并链接文档,但没有解释为什么会这样:
似乎有办法在Windows上实现微秒分辨率:
我感兴趣的是为什么Boost以这种方式实现它,反过来又有可能解决方案更合适?
我班上有一个成员变量:
class Foo
{
// ...
private:
  boost::posix_time::ptime t;
}
我想在构造函数中将其初始化为一个众所周知的值,这样我就知道程序尚未设置它:
Foo::Foo()
   : t(NULL) // doesnt work
{}
但是将其设置为NULL无效,因为它不是指针。
如何初始化boost::posix_time::ptime为众所周知的值?
有没有办法只得到没有。2 个提升日期之间的工作日。
在下文中,我只获取日历日。
date begin_dt(2011,Aug,3);
date end_dt(day_clock::local_day());
days duration=end_dt-begin_dt;
std::cout<<"calendar days between begin & end date are: "<<duration<<std::endl;
我正在努力弄清楚如何使用boost :: date_time进行转换.我想将从Unix时代(1970年1月1日00:00)测量的毫秒值转换为人类可读的字符串 - 类似于:2/13/2012 15:20:11将是理想的.
我已经尝试了一些我见过的std C++/boost建议,但还没有运气.这是我使用的代码:
    long long ticksFromEpoch = 1329117220501;
    std::time_t tt = static_cast<time_t>(ticksFromEpoch);
    boost::posix_time::ptime dt = boost::posix_time::from_time_t(tt);
    // Create a time_zone_ptr for the desired time zone and use it to create a local_date_time
    boost::local_time::time_zone_ptr zone(new boost::local_time::posix_time_zone("UTC"));
    boost::local_time::local_date_time dt_with_zone(dt, zone);
    std::stringstream strm;
    strm.imbue(std::locale(std::cout.getloc(), new boost::local_time::local_time_facet("%Y-%m-%d %H:%M:%S"))); // 15:14
    strm << dt_with_zone;
    // Print the stream's content to the console
    std::cout << strm.str() << std::endl;
输出是:2032-07-01 20:20:37这显然不正确.我怀疑我没有ticksFromEpoch正确构造变量,但我不确定为什么.谁能指出我正确的方向?任何帮助深表感谢!!
我尝试使用 C++ Boost date_time 库将格式为“16:43 December 12, 2012”的字符串加载到日期输入方面为“%H:%M %B %d, %Y”的字符串流中。接下来,我想从 stringstream 创建一个 Boost ptime 对象,以便我可以进行日期/时间数学运算。我无法让它工作 - 下面是代码:
std::string autoMatchTimeStr(row["message_time"]);
ptime autoMatchTime(time_from_string(autoMatchTimeStr));
date_input_facet* fin = new date_input_facet("%H:%M %B %d, %Y");
stringstream dtss;
dtss.imbue(std::locale(std::locale::classic(), fin));
dtss << msg.getDate(); //msg.getDate() returns “16:43 December 12, 2012”
ptime autoMatchReplyTime;
dtss >> autoMatchReplyTime;
if( autoMatchReplyTime < autoMatchTime + minutes(15)) {
    stats[ "RespTimeLow" ] = "increment";
    sysLog << "RespTimeLow" << flush;
}
autoMatchTime 包含有效的日期/时间值,但 autoMatchReplyTime 不包含。我想了解这应该如何工作,但是如果我必须使用 C strptime 来初始化 ptime 构造函数的 struct tm ,我可以这样做。我花了很多时间用 gdb 研究、编码、调试,但无法弄清楚。任何帮助将不胜感激。
我正在使用boost :: date_time并且我得到了一个time_t,它是由库使用C标准库中的time()函数生成的.
我正在寻找从那个时间开始获取当地时间的方法.我正在阅读文档,如果没有提供时区就找不到任何方法,我不知道因为它取决于机器的语言环境,我找不到任何方法从中获取一个.
我错过了什么?
我试图以这种方式将std :: string转换为boost :: gregorian :: date:
#include <iostream>
#include "boost/date_time/gregorian/gregorian.hpp"
int main(int argc, char *argv[])
{
  std::string s_date = "1922-02-29";
  std::stringstream ss(s_date);
  boost::gregorian::date_input_facet *df = new boost::gregorian::date_input_facet("%Y-%m-%d");
  ss.imbue(std::locale(ss.getloc(), df));
  date d;
  ss>>d;
  std::cout<<d<<std::endl;
}
但不幸的是,我只收到了"非约会时间".我做错了什么?有没有更简单的方法将这种流行形式的字符串转换为boost :: gregorian:date?
我正在missing template arguments编译一个简单的日期解析测试使用boost,这是代码:
#include "boost/date_time/gregorian/gregorian.hpp"
#include "boost/date_time/gregorian/parsers.hpp"
boost::date_time::date test = boost::gregorian::from_us_string("07-Sep-2010");
并且编译器抱怨
error: missing template arguments before ‘test’
  boost::date_time::date test = boost::gregorian::from_us_string("07-Sep-2010");
我不明白我应该提供什么模板参数或者为什么我应该首先提供模板参数.对我来说似乎有点太多锅炉板代码:)
我有一个可以在GMT中运行的服务器应用程序。我的应用程序与柏林时区的服务器通信,我不时从该服务器读取一些文件。我需要比较这些文件的时间。考虑到夏,冬时间的变化,如何将远程服务器时间从柏林时区转换为格林尼治标准时间?您知道boost.date_time提供的一些信息可以帮助我轻松地做到这一点吗?
我不是要从当地时间转换为格林尼治标准时间。我正在从特定时区转换为gmt。我需要有一个健壮的方法来在不同时区进行操作。