Thread: [Cppcms-users] boost.locale and date formatting
Brought to you by:
artyom-beilis
From: augustin <aug...@ov...> - 2011-05-04 00:27:04
|
(second try to send this to the list) Hello, Speaking of boost(er).locale, I am having problems formatting dates. I tried the simple example from here: http://cppcms.sourceforge.net/boost_locale/html/formatting_and_parsing.html #include <iostream> #include <sstream> #include <boost/locale.hpp> using std::cout; using std::endl; using namespace boost::locale; int main() { double now=time(0); cout << "Today is "<< as::date << now << " and tommorrow is " << now+24*3600 << endl; cout << "Current time is "<< as::time << now << endl; cout << "The current weekday is "<< as::ftime("%A") << now << endl; } g++ main.cpp -o run.main -lboost_locale and the output is: Today is 1.30439e+09 and tommorrow is 1.30448e+09 Current time is 1.30439e+09 The current weekday is 1.30439e+09 I tried with booster.locale that comes with cppcms, and I downloaded from svn and compiled the very latest boost.locale. I get the same result with both. I'm sure I am missing something simple. Probably, I need to set the locale first, but it's not clear to me how to do it. Also, a grip I have with most/all boost libraries, is that one never knows what option to pass to the linker: http://stackoverflow.com/questions/3467153/is-there-an-intelligent-way-to-know- the-name-of-the-library-to-link-to-at-compile Is this platform/compiler dependent? Can't this kind of thing be documented? Anyway, in this case, I found it was -lboost_locale. For boost.locale, more, fully working, compilable examples would help. - Most boost libraries documentation are written for very experienced C++ developers. From a newbie perspective, I find most documentation very obtuse. I checked boost.date_time for a long while, and still couldn't understand enough to actually make use of it. I still have much to learn before I can understand a lot of the boost documentation. - Generally speaking, within the context of a cppcms application, what is the best/recommended way to format dates, times and such? Basically, I have a unix timestamp, and I want to output it in human readable form, both in the UTC and locale timezone. Thanks, Augustin. -- Friends: http://www.reuniting.info/ My projects: http://astralcity.org/ http://3enjeux.overshoot.tv/ http://linux.overshoot.tv/ http://overshoot.tv/ http://charityware.info/ http://masquilier.org/ http://openteacher.info/ http://minguo.info/ http://www.wechange.org/ http://searching911.info/ . ------------------------------------------------------- -- Friends: http://www.reuniting.info/ My projects: http://astralcity.org/ http://3enjeux.overshoot.tv/ http://linux.overshoot.tv/ http://overshoot.tv/ http://charityware.info/ http://masquilier.org/ http://openteacher.info/ http://minguo.info/ http://www.wechange.org/ http://searching911.info/ . |
From: Artyom <art...@ya...> - 2011-05-04 07:35:44
|
> From: augustin <aug...@ov...> > > (second try to send this to the list) > > Hello, > > Speaking of boost(er).locale, I am having problems formatting dates. > > I tried the simple example from here: > [...] See: http://thread.gmane.org/gmane.comp.lib.boost.devel/218008 > I'm sure I am missing something simple. > Probably, I need to set the locale first, but it's not clear to me how to do > it. See section about locale generation. > > Also, a grip I have with most/all boost libraries, is that one never knows > what option to pass to the linker: > http://stackoverflow.com/questions/3467153/is-there-an-intelligent-way-to-know- > the-name-of-the-library-to-link-to-at-compile > Is this platform/compiler dependent? Can't this kind of thing be documented? > Anyway, in this case, I found it was -lboost_locale. > This is something Boost general not related to Boost.Locale specifically. > For boost.locale, more, fully working, compilable examples would help. > Take a look on libs/locale/examples with full code to see what you miss. > - > > Most boost libraries documentation are written for very experienced C++ > developers. From a newbie perspective, I find most documentation very obtuse. >I > > checked boost.date_time for a long while, and still couldn't understand >enough > > to actually make use of it. I still have much to learn before I can understand > > a lot of the boost documentation. > > - This specific "problem" would be added to FAQ. > > Generally speaking, within the context of a cppcms application, what is the > best/recommended way to format dates, times and such? > Basically, I have a unix timestamp, and I want to output it in human readable > form, both in the UTC and locale timezone. > Read the tutorial there are an examples how to make UTC, local timezone (default) or custom time zone output (there is a manipulator for this purpose and formatting flags). Also the way you want to display dates is application specific and dependent on use case. Best, Artyom |
From: augustin <aug...@ov...> - 2011-05-04 12:40:38
|
On Wednesday 04 May 2011 03:35:35 pm Artyom wrote: > See: http://thread.gmane.org/gmane.comp.lib.boost.devel/218008 > [...] > See section about locale generation. Thanks. The discussion thread you link to, and your mention prompted me to do some further research and I finally figured out that I was missing yet another whole chapter of this very complex, yet fascinating language (C++). I hadn't grasped how the concept of locale was deeply embedded within the language itself: http://www.cplusplus.com/reference/std/locale/ http://stdcxx.apache.org/doc/stdlibug/VII.html chapter 22 of c++0x, chapter 27 of c++03. With only 400+ hours of experience with C++, I still count as a very beginner. It's been 400 hours worth of shocks at discovering how many facets there are to the language, and how many ancillary skills (compiling, linking, gnu tools, Makefile, cmake, etc.) and libraries (std, STL, std::locale, boost, cppcms, cppdb and others I am using...) I have yet to master to become, hopefully one day, a decent C++ programmer. In the boost.locale documentation, as part of the introduction, you ought to make a list of assumed knowledge, with some links like those I provided above, to redirect people like me to background information. I'm going to be reading about all this now... > http://stackoverflow.com/questions/3467153/is-there-an-intelligent-way-to-k > now- > > > the-name-of-the-library-to-link-to-at-compile > > Is this platform/compiler dependent? Can't this kind of thing be > > documented? Anyway, in this case, I found it was -lboost_locale. > > This is something Boost general not related > to Boost.Locale specifically. Yes, but it affects every single boost library, including yours. Maybe you can add such necessary information to the boost.locale documentation. > Take a look on libs/locale/examples > with full code to see what you miss. Oh, yes, I did miss that! Very cool, thanks. Thanks again for your constant support. Your answers were very helpful in pointing me in the right direction. I have much study to do, now... ;) Btw, since I have a svn checkout (ever considered using git? Yet another thing I need to learn: how to link a local git working directory to a svn upstream source. I know there are tutorials about it, that I can find easily... there is so much to do and to learn!)... I was saying: since I checked out boost.locale from svn, let me know if you'd like me to prepare a patch for the documentation.... (I am still waiting for your review of my form.h patch, though...). Blessings, Augustin. -- Friends: http://www.reuniting.info/ My projects: http://astralcity.org/ http://3enjeux.overshoot.tv/ http://linux.overshoot.tv/ http://overshoot.tv/ http://charityware.info/ http://masquilier.org/ http://openteacher.info/ http://minguo.info/ http://www.wechange.org/ http://searching911.info/ . |
From: augustin <aug...@ov...> - 2011-05-04 12:46:13
|
On Wednesday 04 May 2011 08:37:33 pm augustin wrote: > In the boost.locale documentation, as part of the introduction, you ought > to make a list of assumed knowledge, with some links like those I provided > above, to redirect people like me to background information. Oops. You actually already do: http://cppcms.sourceforge.net/boost_locale/html/std_locales.html I read it too quickly the first time and didn't understand what I was reading. It's slowly sinking in... Sorry for the noise. Augustin. -- Friends: http://www.reuniting.info/ My projects: http://astralcity.org/ http://3enjeux.overshoot.tv/ http://linux.overshoot.tv/ http://overshoot.tv/ http://charityware.info/ http://masquilier.org/ http://openteacher.info/ http://minguo.info/ http://www.wechange.org/ http://searching911.info/ . |
From: Artyom <art...@ya...> - 2011-05-04 13:29:47
|
> > > Is this platform/compiler dependent? Can't this kind of thing be > > > documented? Anyway, in this case, I found it was -lboost_locale. > > > > This is something Boost general not related > > to Boost.Locale specifically. > > Yes, but it affects every single boost library, including yours. Maybe you can > > add such necessary information to the boost.locale documentation. > It is not something trivial especially when you use MSVC with its endless incompatible build variants. See: http://www.boost.org/doc/libs/1_46_1/more/getting_started/unix-variants.html http://www.boost.org/doc/libs/1_46_1/more/getting_started/windows.html > ever considered using git? Subversion has a huge advantage of working over https behind firewalls and in various even untrusted networks. So if you work from University network or just some public network there is no replacement for https support. GIT does not allow to perform commits over https protocol like SVN. > (I am still waiting for your review of my form.h patch, > though...). I'm sorry, you are right. Was too damn busy, I'll try to get to it this week. Drop me a mail if I forget once again. Regards, Artyom |
From: augustin <aug...@ov...> - 2011-05-04 15:32:48
|
On Wednesday 04 May 2011 09:29:39 pm Artyom wrote: > http://www.boost.org/doc/libs/1_46_1/more/getting_started/unix- variants.html > http://www.boost.org/doc/libs/1_46_1/more/getting_started/windows.html Ha! Even by reading the official boost documentation page about linking, I wouldn't have guessed what linker option to chose. For Linux/g++, at least, it wouldn't hurt mentioning the -lboost_locale in the docs. > Was too damn busy, I'll try to get to it this week. > Drop me a mail if I forget once again. There's no actual hurry as far as I am concerned, but something just came up today and I may have to travel to Europe for two weeks later this month, so I won't be available then, if I do go. BTW, thanks to your help, I got the missing piece, so that I finally managed to do what I wanted to do: #include <sstream> #include <boost/locale.hpp> std::string timestamp_to_string(time_t const& tt) { using namespace boost::locale; generator gen; std::locale::global(gen("en_US.UTF-8")); std::stringstream ss; ss.imbue(std::locale()); ss << as::date << tt << " "; ss << as::time << tt << std::endl; std::string r = ""; getline(ss, r); return r; } It took me 10-20 hours to research, read and understand enough to come up with the seemingly simple piece of code above. :-/ I use it to format unix timestamps before pushing it to my cppcms::base_content object. I don't know if it's the right approach, but it works... Augustin. -- Friends: http://www.reuniting.info/ My projects: http://astralcity.org/ http://3enjeux.overshoot.tv/ http://linux.overshoot.tv/ http://overshoot.tv/ http://charityware.info/ http://masquilier.org/ http://openteacher.info/ http://minguo.info/ http://www.wechange.org/ http://searching911.info/ . |
From: Artyom <art...@ya...> - 2011-05-04 16:52:53
|
> > BTW, thanks to your help, I got the missing piece, so that I finally managed >to > > do what I wanted to do: > > #include <sstream> > #include <boost/locale.hpp> > > std::string timestamp_to_string(time_t const& tt) { > using namespace boost::locale; > generator gen; > std::locale::global(gen("en_US.UTF-8")); > std::stringstream ss; > ss.imbue(std::locale()); > ss << as::date << tt << " "; > ss << as::time << tt << std::endl; > std::string r = ""; > getline(ss, r); > return r; > } > Few points: > timestamp_to_string(time_t const& tt) It is better to write timestamp_to_string(time_t tt) As time_t is integral type and it is better to pass it by value. > using namespace boost::locale; > generator gen; > std::locale::global(gen("en_US.UTF-8")); > ss.imbue(std::locale()); Is very inefficient as locale generation is very heavy procedure. In CppCMS context you have this member function: http://cppcms.sourceforge.net/cppcms_ref_v0_99/classcppcms_1_1http_1_1context.html#39fde738210daf84deee691f45260e5b So basically inside cppcms::application based class just write std::locale loc = context().locale(); or std::locale loc = context().locale("en_US.UTF-8"); It returns the locale defined in configuration file for you and it would cache the locale automatically for future use. > std::locale::global(gen("en_US.UTF-8")); > ss.imbue(std::locale()); Even if you use it, it is better to write ss.imbue(gen("en_US.UTF-8")); And not set global locale and then create a locale instance from global one. > ss << as::date << tt << " "; > ss << as::time << tt << std::endl; It is better to write ss << as::datetime << tt; > std::string r = ""; r is empty by default, no need r=""; > getline(ss, r); > return r; It is better to call return ss.str() Of course without "<<std::endl" > It took me 10-20 hours to research, read and understand enough to come up with > > the seemingly simple piece of code above. :-/ > > I use it to format unix timestamps before pushing it to my > cppcms::base_content object. > > I don't know if it's the right approach, but it works... > It is much simpler, you have filers namespace http://cppcms.sourceforge.net/cppcms_ref_v0_99/namespacecppcms_1_1filters.html So just write in the template <% tt | datetime %> Or even with more fine grained control <% tt | strftime("%Y-%m-%d %H:%M:%S %Z") %> where tt is just time_t in your context :-) > > > Augustin. Best, Artyom |
From: augustin <aug...@ov...> - 2011-05-05 13:31:47
|
On Thursday 05 May 2011 12:52:44 am Artyom wrote: > > getline(ss, r); > > return r; > > It is better to call > > return ss.str() Oops! This one is very embarrassing given the time I wasted trying to figure out a simple way to extract the string from the stream! Thank you very much for all your comments on my code. They're duly noted. I appreciate it. > http://cppcms.sourceforge.net/cppcms_ref_v0_99/namespacecppcms_1_1filters. > html > > So just write in the template > > > <% tt | datetime %> > > Or even with more fine grained control > > <% tt | strftime("%Y-%m-%d %H:%M:%S %Z") %> > > > where tt is just time_t in your context :-) Ah, yes! You just answered my earlier question about locales in a cppcms context. This is actually what I meant. I knew my approach didn't feel right. It works as it should, now. Thanks again, augustin. -- Friends: http://www.reuniting.info/ My projects: http://astralcity.org/ http://3enjeux.overshoot.tv/ http://linux.overshoot.tv/ http://overshoot.tv/ http://charityware.info/ http://masquilier.org/ http://openteacher.info/ http://minguo.info/ http://www.wechange.org/ http://searching911.info/ . |