我正在编写一种HTTP代理,所以我需要做三件事:
作为参考,这些是我需要解析的日期时间的示例.我只会输出第一种格式:
  Sun, 06 Nov 1994 08:49:37 GMT  ; RFC 822, updated by RFC 1123
  Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036
  Sun Nov  6 08:49:37 1994       ; ANSI C's asctime() format
我很确定Boost date_time可以完成所有这些,但是我遇到了一些问题.有没有人有代码可以做到这一点?也许我并没有熟练使用谷歌,但我无法找到一个如何在任何地方提升的例子.
谢谢你的帮助!
更新:我有一个解决方案,但第二个解析器有错误的一年(我猜因为它是一个2位数的年份),最后一个解析器给出了一个例外(见下面的输出).
try
{
  // output time now in GMT
  // format we want: Sun, 06 Nov 1994 08:49:37 GMT
  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("%a, %d %b %Y %H:%M:%S GMT"));
  std::cout.imbue(std::locale(std::cout.getloc(), lf));
  std::cout << t …如何获取当前UTC偏移量(如时区,但只是当前时刻的UTC偏移量)?
我需要一个像"+02:00"这样的答案.
有没有一种简单的方法可以从boost Date_Time库中获取当前GMT时间(以毫秒为单位)?
这是一个使用的例子time_of_day,我不想要time_of_day但是GMT的总时间如下long long int:
boost::posix_time::ptime time =
          boost::posix_time::microsec_clock::universal_time();
boost::posix_time::time_duration duration( time.time_of_day() );  // ???
long long int timeInMilliseconds = duration.total_milliseconds();
我有一个boost :: date对象.当我在对象上调用month()接口时,它返回对象所持有的月份,但是以字符串形式返回.有没有办法可以获得与月份相关的数字?即
date mySampleDate = date_from_tm(tm_myDate) ;
cout<<mySampleDate.month() ; //Gives the output as May/Jun/Jul etc. I need 5/6/7 etc.
我需要在不将boost对象恢复到tm结构的情况下得到它.这会导致过多的转换和可能的性能损失.
我想存储boost::gregorian::date为一个键,boost::unordered_map但我无法编译代码,因为它缺少这个类的正确哈希函数.
std::string并存储它.我可能想避免这种解决方案,因为使用字符串非常昂贵.day()函数,我不确定这是否真的合适.有没有其他更好的方法来存储日期或功能导出日期作为数字?
到目前为止,我能够获取当前的语言环境,但我想获取该特定语言环境的日期格式.可以使用标准库完成此操作.
#include <locale>
int _tmain(int argc, _TCHAR* argv[])
{
   // Print the current locale
    std::cout << std::locale("").name().c_str() << "\n";
    // TODO: get the locale's date pattern, example for US it's (mm/dd/yyyy)
    std::cout << "date pattern:  \n";
}
我想date_time在boost中使用库来表示我的应用程序中的时间.此应用程序将生成Atom订阅源,后者又以RFC 3339中指定的格式强制设置时间戳,例如"1990-12-31T23:59:60Z"或"1990-12-31T15:59:60-08:00 ".
那么,如何根据此RFC格式化时间?
我一整天都在阅读日期时间输入/输出文档,我似乎无法找到如何在需要时将Z放在最后.此外,RFC支持可选的小数秒,但只支持其一位数(例如"1990-12-31T23:59:60.5Z")(*).我似乎无法找到如何做到这一点.
我总是可以编写自己的格式化例程来读出不同的需要字段,但在我看来,这似乎是在对付date_time图书馆.
有没有为这个库编写格式化程序的经验?或者我做错了什么?
(*):在我看来,RFC中给出的ABNF只允许一位数的小数秒,但同一RFC中的示例有两位数的小数秒.那是什么意思?
我这样做
 boost::gregorian::date current_date(boost::date_time::day_clock::local_day());
我收到以下错误
‘template<class date_type> class boost::date_time::day_clock’ used without template parameters  
我必须做些什么呢?
参考http://www.boost.org/doc/libs/1_47_0/doc/html/date_time/gregorian.html#date_construct_from_clock
我有以下毫/秒精度字符串来解析某种提升日期时间.
std::string cell ="20091201 00:00:04.437";
我看过关于方面的文档.像这样的东西
date_input_facet* f = new date_input_facet();
f->format("%Y%m%d %F *");
但我不知道如何使用它们.
我尝试使用从StackOverflow中清除的代码来编写这个程序,但我无法让millis显示:
#include <string>
#include <iostream>
#include <sstream>
#include <fstream>
#include <map>
#include <boost/algorithm/string.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time.hpp>
namespace bt = boost::posix_time;
const std::locale formats[] =
{
    std::locale(std::locale::classic(),new bt::time_input_facet("%Y%m%d %H:%M:%S.f")),
    std::locale(std::locale::classic(),new bt::time_input_facet("%Y-%m-%d %H:%M:%S")),
    std::locale(std::locale::classic(),new bt::time_input_facet("%Y/%m/%d %H:%M:%S")),
    std::locale(std::locale::classic(),new bt::time_input_facet("%d.%m.%Y %H:%M:%S")),
    std::locale(std::locale::classic(),new bt::time_input_facet("%Y-%m-%d"))
};
const size_t formats_n = sizeof(formats) / sizeof(formats[0]);
std::time_t pt_to_time_t(const bt::ptime& pt)
{
    bt::ptime timet_start(boost::gregorian::date(1970,1,1));
    bt::time_duration diff = pt - timet_start;
    return diff.ticks()/bt::time_duration::rep_type::ticks_per_second;
}
void …这就是人们如何使用“两天前”获得“两天前”的信息boost::date:
boost::gregorian::date today = boost::gregorian::day_clock::local_day();
boost::date_time::day_functor<boost::gregorian::date> day_offset(-2);
boost::gregorian::date modified = today + day_offset.get_offset(today);
如何计算代表“上周一”的日期?