5

私は一種の HTTP プロキシを作成しているので、次の 3 つのことを実行できる必要があります。

  1. RFC 2616, sec 3.3で指定された 3 つの形式のいずれかを指定して、HTTP 日付を解析します。
  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 で問題が発生しています。これを行うコードを既に持っている人はいますか? おそらく私はグーグルを上手に使用していませんが、ブーストでこれを行う方法の例はどこにも見つかりません。

助けてくれてありがとう!

更新: 解決策はありますが、2 番目のパーサーの年が間違っており (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 << 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 ''
4

2 に答える 2

6

そこまで行ってブーストに手を伸ばす必要はないと思います:)単純な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)) {..}
于 2011-02-11T12:53:54.187 に答える
4

これが、私が必要とすることを行う、私が作成した 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);
}
于 2010-05-15T09:23:57.153 に答える