time date newbie problem
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? -- Cheers, Michał Nowotka
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
[Please do not mail me a copy of your followup] boost-users@lists.boost.org spake the secret code <47729C6C.3090409@crystalclearsoftware.com> thusly:
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 }
I am always amazed when people superfluously use new. I am willing to bet that superfluous use of new is responsible for quite a number of C++ memory leaks and heap thrashing. Try: int main() { ptime t1(date(2007, Dec, 26), hours(1) + seconds(5) + milliseconds(9)); time_facet timefacet("%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 } -- "The Direct3D Graphics Pipeline" -- DirectX 9 draft available for download http://www.xmission.com/~legalize/book/download/index.html Legalize Adulthood! http://blogs.xmission.com/legalize/
Richard wrote:
I am always amazed when people superfluously use new. I am willing to bet that superfluous use of new is responsible for quite a number of C++ memory leaks and heap thrashing. Try:
The new is NOT superfluous. The constructor of std::locale takes a pointer to a reference counted facet -- effectively taking ownership of the memory. It will attempt to delete the memory associated with the facet when it goes out of scope....likely corrupting the stack if written as you suggest. Here's the best online reference that discusses this topic. http://www.angelikalanger.com/Articles/C++Report/LocaleFramework/LocaleFrame... Search for Memory Management to get to the related details. Jeff
In article <4772F58B.8080908@crystalclearsoftware.com>,
Jeff Garland
The new is NOT superfluous. The constructor of std::locale takes a pointer to a reference counted facet -- effectively taking ownership of the memory.
Does it call delete on it? -- "The Direct3D Graphics Pipeline" -- DirectX 9 draft available for download http://www.xmission.com/~legalize/book/download/index.html Legalize Adulthood! http://blogs.xmission.com/legalize/
Richard wrote:
In article <4772F58B.8080908@crystalclearsoftware.com>, Jeff Garland
writes: The new is NOT superfluous. The constructor of std::locale takes a pointer to a reference counted facet -- effectively taking ownership of the memory.
Does it call delete on it?
Yes -- there's an internal reference count in the facet so that facets can be shared by multiple streams. Jeff
In article <4773AFA2.2080201@crystalclearsoftware.com>,
Jeff Garland
Richard wrote:
In article <4772F58B.8080908@crystalclearsoftware.com>, Jeff Garland
writes: The new is NOT superfluous. The constructor of std::locale takes a pointer to a reference counted facet -- effectively taking ownership of the memory.
Does it call delete on it?
Yes -- there's an internal reference count in the facet so that facets can be shared by multiple streams.
And No -- if you pass a positive value for the reference count to the facet c'tor then it doesn't call delete on the pointer. So you could do that code without calling new as follows: int main() { ptime t1(date(2007, Dec, 26), hours(1) + seconds(5) + milliseconds(9)); time_facet timefacet("%a, %d %b %Y %H:%M:%S %z", time_facet::period_formatter_type(), time_facet::specialvalues_formatter_type(), time_facet::date_gen_formatter_type(), 1); std::cout.imbue(std::locale(std::locale::classic(), &timefacet)); std::cout << t1 << std::endl; //Wed, 26 Dec 2007 01:00:05 } Which is admittedly more clunky to work with, but then again all the locale stuff feels that way to me. I'm not sure if you need to reverse the effects of the imbue before you leave main() either, that might be something else that locales require for this to work properly. -- "The Direct3D Graphics Pipeline" -- DirectX 9 draft available for download http://www.xmission.com/~legalize/book/download/index.html Legalize Adulthood! http://blogs.xmission.com/legalize/
participants (4)
-
Jeff Garland
-
legalize+jeeves@mail.xmission.com
-
Michał Nowotka
-
Richard