使用 boost date_time 解析和创建 HTTP 日期

发布于 2024-09-01 14:12:07 字数 2739 浏览 7 评论 0原文

我正在编写一种 HTTP 代理,因此我需要能够做 3 件事: 根据

  1. RFC 2616, sec 3.3
  2. 将文件日期时间转换为 HTTP 日期字符串,并将
  3. 日期输出为字符串。

作为参考,这些是我需要解析的日期时间的示例。我将只输出第一种格式:

  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 可以做到所有这些,但我在数字 1 上遇到了一些麻烦。有人已经有代码可以做到这一点吗?也许我没有熟练使用谷歌,但我在任何地方都找不到如何使用 boost 来做到这一点的示例。

感谢您的帮助!

更新:我有一个解决方案,但第二个解析器的年份错误(我猜是因为它是两位数的年份),最后一个解析器给出了异常(参见下面的输出)。

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 << std::endl;

  // get a file mod time into the correct format
  boost::filesystem::path p("../code/main.cpp");
  boost::posix_time::ptime pt = boost::posix_time::from_time_t(
    boost::filesystem::last_write_time(p));
  boost::local_time::local_date_time t2(pt, boost::local_time::time_zone_ptr());
  std::cout << t2 << std::endl;

  std::stringstream ss;
  ss.exceptions(std::ios_base::failbit);

  // input date-time
  boost::local_time::local_time_input_facet* lif1(new boost::local_time::local_time_input_facet("%a, %d %b %Y %H:%M:%S GMT"));
  ss.imbue(std::locale(std::locale::classic(), lif1));
  ss.str("Sun, 06 Nov 1994 08:49:37 GMT");
  ss >> t;
  std::cout << t << std::endl;

  boost::local_time::local_time_input_facet* lif2(new boost::local_time::local_time_input_facet("%A, %d-%b-%y %H:%M:%S GMT"));
  ss.imbue(std::locale(std::locale::classic(), lif2));
  ss.str("Sunday, 06-Nov-94 08:49:37 GMT");
  ss >> t;
  std::cout << t << std::endl;

  boost::local_time::local_time_input_facet* lif3(new boost::local_time::local_time_input_facet("%a %b %e %H:%M:%S %Y"));
  ss.imbue(std::locale(std::locale::classic(), lif3));
  ss.str("Sun Nov  6 08:49:37 1994");
  ss >> t;
  std::cout << t << std::endl;
}
catch (std::exception& e)
{
  std::cout << "Exception: " << e.what() << std::endl;
}

输出:

Sat, 15 May 2010 03:01:13 GMT
Sat, 15 May 2010 03:01:01 GMT
Sun, 06 Nov 1994 08:49:37 GMT
Sat, 06 Nov 2094 08:49:37 GMT
Exception: Parse failed. No match found for ''

I'm writing a kind of HTTP proxy, so I need to be able to do 3 things:

  1. Parse an HTTP-date given any of the 3 formats specified in RFC 2616, sec 3.3,
  2. Convert a file date-time to an HTTP-date string, and
  3. Output the date to a string.

For reference, theses are examples of the date-times I need to parse. I will output only the first format:

  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

I'm pretty sure Boost date_time can do all of this, but I'm having some trouble with number 1. Does anyone already have code to do this? Perhaps I'm not using google proficiently, but I can't find an example of how to do this with boost anywhere.

Thanks for any help!

UPDATE: I have a solution, but the second parser has the wrong year (I guess because it is a 2 digit year) and the last parser gives an exception (see output below).

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 << std::endl;

  // get a file mod time into the correct format
  boost::filesystem::path p("../code/main.cpp");
  boost::posix_time::ptime pt = boost::posix_time::from_time_t(
    boost::filesystem::last_write_time(p));
  boost::local_time::local_date_time t2(pt, boost::local_time::time_zone_ptr());
  std::cout << t2 << std::endl;

  std::stringstream ss;
  ss.exceptions(std::ios_base::failbit);

  // input date-time
  boost::local_time::local_time_input_facet* lif1(new boost::local_time::local_time_input_facet("%a, %d %b %Y %H:%M:%S GMT"));
  ss.imbue(std::locale(std::locale::classic(), lif1));
  ss.str("Sun, 06 Nov 1994 08:49:37 GMT");
  ss >> t;
  std::cout << t << std::endl;

  boost::local_time::local_time_input_facet* lif2(new boost::local_time::local_time_input_facet("%A, %d-%b-%y %H:%M:%S GMT"));
  ss.imbue(std::locale(std::locale::classic(), lif2));
  ss.str("Sunday, 06-Nov-94 08:49:37 GMT");
  ss >> t;
  std::cout << t << std::endl;

  boost::local_time::local_time_input_facet* lif3(new boost::local_time::local_time_input_facet("%a %b %e %H:%M:%S %Y"));
  ss.imbue(std::locale(std::locale::classic(), lif3));
  ss.str("Sun Nov  6 08:49:37 1994");
  ss >> t;
  std::cout << t << std::endl;
}
catch (std::exception& e)
{
  std::cout << "Exception: " << e.what() << std::endl;
}

Output:

Sat, 15 May 2010 03:01:13 GMT
Sat, 15 May 2010 03:01:01 GMT
Sun, 06 Nov 1994 08:49:37 GMT
Sat, 06 Nov 2094 08:49:37 GMT
Exception: Parse failed. No match found for ''

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鸵鸟症 2024-09-08 14:12:07

我不认为你需要走那么远去获得Boost:)
您可以使用简单的 C 代码:

static const char format[] = "%a, %d %b %Y %H:%M:%S %Z"; // rfc 1123
struct tm tm;
bzero(&tm, sizeof(tm));
if (strptime(str, format, &tm)) {..}

I don't think that you need to go that far and reach for the Boost :)
You can get away with simple C code:

static const char format[] = "%a, %d %b %Y %H:%M:%S %Z"; // rfc 1123
struct tm tm;
bzero(&tm, sizeof(tm));
if (strptime(str, format, &tm)) {..}
后来的我们 2024-09-08 14:12:07

这是我创建的 DateTime 类,它可以满足我的需要。也许其他人会使用它。该代码在公共领域。我欢迎任何评论。

顺便说一句,显然 std::locale 接受一个指向构面的指针,然后在使用完毕后将其删除,因此不需要删除它(事实上,删除它会破坏事情)。

日期时间.h:

// $Id$

#ifndef _DATETIME_H_
#define _DATETIME_H_

#include <string>
#include "common.h"
#include <boost/date_time/local_time/local_time.hpp>

class DateTime
{
public:
  DateTime();
  DateTime(const std::string& path);

  // return datetime string
  std::string str();

  // update datetime from file mod date
  std::string from_file(const std::string& path);

  // parse datetime string
  void parse(const std::string& dt);

  // boolean equal operator
  friend bool operator==(const DateTime& left, const DateTime& right);

private:
  boost::local_time::local_date_time m_dt;
};

#endif // _DATETIME_H_

日期时间.cpp:

// $Id$

#include <sstream>
#include "common.h"
#include <boost/date_time.hpp>
#include <boost/date_time/local_time/local_time.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/filesystem/operations.hpp>
#include "datetime.h"

DateTime::DateTime()
:m_dt(boost::local_time::local_sec_clock::local_time(boost::local_time::time_zone_ptr()))
{
}

DateTime::DateTime(const std::string& path)
:m_dt(boost::local_time::local_sec_clock::local_time(boost::local_time::time_zone_ptr()))
{
  from_file(path);
}

std::string DateTime::str()
{
  std::string result;
  boost::local_time::local_time_facet* lf(new boost::local_time::local_time_facet("%a, %d %b %Y %H:%M:%S GMT"));
  try
  {
    std::stringstream ss;
    ss.imbue(std::locale(ss.getloc(), lf));
    ss << m_dt;
    result = ss.str();
  }
  catch (std::exception& e)
  {
    std::cout << "Exception: " << e.what() << std::endl;
  }
  return result;
}

std::string DateTime::from_file(const std::string& path)
{
  try
  {
    boost::filesystem::path p(path);
    boost::posix_time::ptime pt = boost::posix_time::from_time_t(
      boost::filesystem::last_write_time(p));
    m_dt = boost::local_time::local_date_time(pt, boost::local_time::time_zone_ptr());
  }
  catch (std::exception& e)
  {
    std::cout << "Exception: " << e.what() << std::endl;
  }
  return str();
}

void DateTime::parse(const std::string& dt)
{
  boost::local_time::local_time_input_facet* lif(new boost::local_time::local_time_input_facet("%a, %d %b %Y %H:%M:%S GMT"));
  std::stringstream ss(dt);
  ss.imbue(std::locale(std::locale::classic(), lif));
  ss >> m_dt;
}

bool operator==(const DateTime& left, const DateTime& right)
{
  return (left.m_dt == right.m_dt);
}

Here is the DateTime class I create that does what I needed. Maybe someone else will use it. This code in the public domain. I welcome any comments.

By the way, apparently std::locale takes a pointer to a facet and then deletes it when it is done with it, so there is no need to delete it (in fact, deleting it breaks things).

datetime.h:

// $Id$

#ifndef _DATETIME_H_
#define _DATETIME_H_

#include <string>
#include "common.h"
#include <boost/date_time/local_time/local_time.hpp>

class DateTime
{
public:
  DateTime();
  DateTime(const std::string& path);

  // return datetime string
  std::string str();

  // update datetime from file mod date
  std::string from_file(const std::string& path);

  // parse datetime string
  void parse(const std::string& dt);

  // boolean equal operator
  friend bool operator==(const DateTime& left, const DateTime& right);

private:
  boost::local_time::local_date_time m_dt;
};

#endif // _DATETIME_H_

datetime.cpp:

// $Id$

#include <sstream>
#include "common.h"
#include <boost/date_time.hpp>
#include <boost/date_time/local_time/local_time.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/filesystem/operations.hpp>
#include "datetime.h"

DateTime::DateTime()
:m_dt(boost::local_time::local_sec_clock::local_time(boost::local_time::time_zone_ptr()))
{
}

DateTime::DateTime(const std::string& path)
:m_dt(boost::local_time::local_sec_clock::local_time(boost::local_time::time_zone_ptr()))
{
  from_file(path);
}

std::string DateTime::str()
{
  std::string result;
  boost::local_time::local_time_facet* lf(new boost::local_time::local_time_facet("%a, %d %b %Y %H:%M:%S GMT"));
  try
  {
    std::stringstream ss;
    ss.imbue(std::locale(ss.getloc(), lf));
    ss << m_dt;
    result = ss.str();
  }
  catch (std::exception& e)
  {
    std::cout << "Exception: " << e.what() << std::endl;
  }
  return result;
}

std::string DateTime::from_file(const std::string& path)
{
  try
  {
    boost::filesystem::path p(path);
    boost::posix_time::ptime pt = boost::posix_time::from_time_t(
      boost::filesystem::last_write_time(p));
    m_dt = boost::local_time::local_date_time(pt, boost::local_time::time_zone_ptr());
  }
  catch (std::exception& e)
  {
    std::cout << "Exception: " << e.what() << std::endl;
  }
  return str();
}

void DateTime::parse(const std::string& dt)
{
  boost::local_time::local_time_input_facet* lif(new boost::local_time::local_time_input_facet("%a, %d %b %Y %H:%M:%S GMT"));
  std::stringstream ss(dt);
  ss.imbue(std::locale(std::locale::classic(), lif));
  ss >> m_dt;
}

bool operator==(const DateTime& left, const DateTime& right)
{
  return (left.m_dt == right.m_dt);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文