Boost logo

Boost Users :

From: Jeff Garland (jeff_at_[hidden])
Date: 2007-12-26 13:24:44


Micha³ Nowotka wrote:
> Hi,
> I'm a date time library newbie and currenty working on simple web
> server implementation in C++. I want to generate string containing
> actual date in following format:
>
> Wed, 26 Dec 2007 17:33:47 GMT
>
> Date time documentation is so complicated and I can't find clear
> answer how to do that. Can you help me?
>

Yes, it's trivial, uses format flags like strftime -- see this doc page for more:

http://www.boost.org/doc/html/date_time/date_time_io.html

But here's some example code:

#include "boost/date_time.hpp"
#include <iostream>

using namespace boost::posix_time;
using namespace boost::gregorian;

int main()
{

   ptime t1(date(2007, Dec, 26), hours(1) + seconds(5) + milliseconds(9));

   time_facet* timefacet = new time_facet("%a, %d %b %Y %H:%M:%S %z");
   std::cout.imbue(std::locale(std::locale::classic(), timefacet));

   std::cout << t1 << std::endl; //Wed, 26 Dec 2007 01:00:05
}

Only thing to note there is that this doesn't output a timezone because the
ptime type doesn't carry timezone data with it. To get the full timezone
behavior you'll need to look up boost::local_time::local_date_time and the
boost::local_time::local_time_facet. I trust you can look those up in the docs.

Jeff


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net