Thread: [Cppcms-users] How to store/manipulate dates?
Brought to you by:
artyom-beilis
From: <ele...@ex...> - 2012-02-06 04:36:38
|
Hi, Essentially my problem is this - I need to load timestamp from a database table, what data type should I use to store it? (I see booster::locale::date_time, is that it?) Cheers, Petr Janda |
From: <ele...@ex...> - 2012-02-06 05:31:21
|
A little update, using just <ctime>, by fetching the timestamp as std::tm and then converting it to time_t works. However, I noticed another issue. struct topic{ topic() : id(0) {} std::string title; std::string body; booster::locale::date_time created_at; int id; }; Using date_time hits exception - std::bad_cast. I've actually removed every reference to created_at in the code and it seems that by merely including in the struct, it causes the server to error 500. The struct gets copied into a vector that holds all the topics. I assume this is a bug. Petr > Hi, > > Essentially my problem is this - I need to load timestamp from a database > table, what data type should I use to store it? (I see > booster::locale::date_time, is that it?) > > Cheers, > Petr Janda > > > ------------------------------------------------------------------------------ > Try before you buy = See our experts in action! > The most comprehensive online learning library for Microsoft developers > is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3, > Metro Style Apps, more. Free future releases when you subscribe now! > http://p.sf.net/sfu/learndevnow-dev2 > _______________________________________________ > Cppcms-users mailing list > Cpp...@li... > https://lists.sourceforge.net/lists/listinfo/cppcms-users > |
From: <ele...@ex...> - 2012-02-06 06:43:16
|
Another update - reading the locale docs, i found out I have to install a default global locale to use date_time Right now what I do is this - std::tm created_at = r.get<std::tm>("created_at"); c.topics[i].created_at = mktime(&created_at); created_at is of data type date_time. Is there any library function to make it less complicated so I dont have to first convert tm to time_t and then to date_time? Thanks Petr > A little update, using just <ctime>, by fetching the timestamp as std::tm > and then converting it to time_t works. However, I noticed another issue. > > struct topic{ > topic() : id(0) {} > std::string title; > std::string body; > booster::locale::date_time created_at; > int id; > }; > > Using date_time hits exception - std::bad_cast. I've actually removed > every reference to created_at in the code and it seems that by merely > including in the struct, it causes the server to error 500. > > The struct gets copied into a vector that holds all the topics. > > I assume this is a bug. > > Petr > >> Hi, >> >> Essentially my problem is this - I need to load timestamp from a >> database >> table, what data type should I use to store it? (I see >> booster::locale::date_time, is that it?) >> >> Cheers, >> Petr Janda >> >> >> ------------------------------------------------------------------------------ >> Try before you buy = See our experts in action! >> The most comprehensive online learning library for Microsoft developers >> is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3, >> Metro Style Apps, more. Free future releases when you subscribe now! >> http://p.sf.net/sfu/learndevnow-dev2 >> _______________________________________________ >> Cppcms-users mailing list >> Cpp...@li... >> https://lists.sourceforge.net/lists/listinfo/cppcms-users >> > > > > ------------------------------------------------------------------------------ > Try before you buy = See our experts in action! > The most comprehensive online learning library for Microsoft developers > is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3, > Metro Style Apps, more. Free future releases when you subscribe now! > http://p.sf.net/sfu/learndevnow-dev2 > _______________________________________________ > Cppcms-users mailing list > Cpp...@li... > https://lists.sourceforge.net/lists/listinfo/cppcms-users > |
From: Marcel H. <ke...@co...> - 2012-02-06 07:08:27
|
Sorry for the "late" reply. What you are looking for is this http://cppcms.com/cppcms_ref_v0_99/classbooster_1_1ptime.html greetings ele...@ex... schrieb: Another update - reading the locale docs, i found out I have to install a default global locale to use date_time Right now what I do is this - std::tm created_at = r.get<std::tm>("created_at"); c.topics[i].created_at = mktime(&created_at); created_at is of data type date_time. Is there any library function to make it less complicated so I dont have to first convert tm to time_t and then to date_time? Thanks Petr > A little update, using just <ctime>, by fetching the timestamp as std::tm > and then converting it to time_t works. However, I noticed another issue. > > struct topic{ > topic() : id(0) {} > std::string title; > std::string body; > booster::locale::date_time created_at; > int id; > }; > > Using date_time hits exception - std::bad_cast. I've actually removed > every reference to created_at in the code and it seems that by merely > including in the struct, it causes the server to error 500. > > The struct gets copied into a vector that holds all the topics. > > I assume this is a bug. > > Petr > >> Hi, >> >> Essentially my problem is this - I need to load timestamp from a >> database >> table, what data type should I use to store it? (I see >> booster::locale::date_time, is that it?) >> >> Cheers, >> Petr Janda >> >> >>_____________________________________________ >> Try before you buy = See our experts in action! >> The most comprehensive online learning library for Microsoft developers >> is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3, >> Metro Style Apps, more. Free future releases when you subscribe now! >> http://p.sf.net/sfu/learndevnow-dev2 >>_____________________________________________ >> Cppcms-users mailing list >> Cpp...@li... >> https://lists.sourceforge.net/lists/listinfo/cppcms-users >> > > > >_____________________________________________ > Try before you buy = See our experts in action! > The most comprehensive online learning library for Microsoft developers > is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3, > Metro Style Apps, more. Free future releases when you subscribe now! > http://p.sf.net/sfu/learndevnow-dev2 >_____________________________________________ > Cppcms-users mailing list > Cpp...@li... > https://lists.sourceforge.net/lists/listinfo/cppcms-users > _____________________________________________ Try before you buy = See our experts in action! The most comprehensive online learning library for Microsoft developers is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3, Metro Style Apps, more. Free future releases when you subscribe now! http://p.sf.net/sfu/learndevnow-dev2 _____________________________________________ Cppcms-users mailing list Cpp...@li... https://lists.sourceforge.net/lists/listinfo/cppcms-users |
From: <ele...@ex...> - 2012-02-06 07:36:27
|
Ah yes, thank you. This is what im doing now - c.topics[i].created_at = booster::ptime::local_time(r.get<std::tm>("created_at")); Next question is - what data type to use for storing only date? Thanks again Petr > Sorry for the "late" reply. > What you are looking for is this > > http://cppcms.com/cppcms_ref_v0_99/classbooster_1_1ptime.html > > greetings > |
From: Marcel H. <ke...@co...> - 2012-02-06 08:05:30
|
hmm. I don't actually know what do you want. You have a database with some some unix timestamps in it, right? Next thing you want to do is to covert them into a human readable format? Read the doc from the date_time class. Maybe in fact that is what you are really looking for. Maybe you combine both, DateTime and ptime. http://cppcms.com/cppcms_ref_v0_99/classbooster_1_1locale_1_1calendar__facet.html http://cppcms.com/cppcms_ref_v0_99/group__date__time.html There is also a note when he throws an exception. Maybe you look into the messageboard example. I think there is what you want. Also i don't know what do you mean with "storing only date" I think what you want is to make it const. Or you define a class and make it private with a "getter" (: Happy trying. ele...@ex... schrieb: Ah yes, thank you. This is what im doing now - c.topics[i].created_at = booster::ptime::local_time(r.get<std::tm>("created_at")); Next question is - what data type to use for storing only date? Thanks again Petr > Sorry for the "late" reply. > What you are looking for is this > > http://cppcms.com/cppcms_ref_v0_99/classbooster_1_1ptime.html > > greetings > _____________________________________________ Try before you buy = See our experts in action! The most comprehensive online learning library for Microsoft developers is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3, Metro Style Apps, more. Free future releases when you subscribe now! http://p.sf.net/sfu/learndevnow-dev2 _____________________________________________ Cppcms-users mailing list Cpp...@li... https://lists.sourceforge.net/lists/listinfo/cppcms-users |
From: Artyom B. <art...@ya...> - 2012-02-06 08:04:26
|
Let's make an order: std::time_t - number POSIX time general time representation, time zone independent booster::ptime - POSIX time - general time representation as a number similar to time_t but with higher precision. Time zone independent. booster::locale::date_time - localized time representation. Internally similar to time_t but allows operations according to current locale for example what is the first day of this week which maybe Sunday or Monday according to the locale (US/FR) Internally time zone independent but operations are time zone dependent. Requires special locale for operations (not necessary global) std::tm Standard C++ date-time representation. Does not hold and information about time zone. So which one do you need? if you want to use ptime with cppdb: p = booster::ptime::universal_time(get<std::tm>(column)) p = booster::ptime::local_time(get<std::tm>(column)) statement.bind(booster::ptime::universal_time(p)); statement.bind(booster::ptime::local_time(p)); If you want to use boost::locale::date_time with cppdb, you need to convert it into std::tm or from std::tm Usually it is not stright-forward as date_time may use for example Hebrew or Japanese calendar and the conversion to std::tm would not be correct as Gregorian month and Hebrew month are different. So you need to convert it first to either time_t or booster::ptime and then to std::tm and the other way around. Of course you can set std::tm's fields manually if you know that boost::locale::date_time uses Gregorian calendar. If you want to store only date you have two options: Use string and format it as "YYYY-MM-DD" or use std::tm with relevant fields set if the SQL engine knows to convert one type to another. Artyom Beilis -------------- CppCMS - C++ Web Framework: http://cppcms.com/ CppDB - C++ SQL Connectivity: http://cppcms.com/sql/cppdb/ >________________________________ > From: "ele...@ex..." <ele...@ex...> >To: cpp...@li... >Sent: Monday, February 6, 2012 9:36 AM >Subject: Re: [Cppcms-users] How to store/manipulate dates? > >Ah yes, thank you. > >This is what im doing now - > >c.topics[i].created_at = >booster::ptime::local_time(r.get<std::tm>("created_at")); > >Next question is - what data type to use for storing only date? > >Thanks again >Petr > >> Sorry for the "late" reply. >> What you are looking for is this >> >> http://cppcms.com/cppcms_ref_v0_99/classbooster_1_1ptime.html >> >> greetings >> > > >------------------------------------------------------------------------------ >Try before you buy = See our experts in action! >The most comprehensive online learning library for Microsoft developers >is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3, >Metro Style Apps, more. Free future releases when you subscribe now! >http://p.sf.net/sfu/learndevnow-dev2 >_______________________________________________ >Cppcms-users mailing list >Cpp...@li... >https://lists.sourceforge.net/lists/listinfo/cppcms-users > > > |
From: <ele...@ex...> - 2012-02-06 09:10:35
|
> If you want to store only date you have two options: > > Use string and format it as "YYYY-MM-DD" or > use std::tm with relevant fields set if the SQL engine > knows to convert one type to another. Thanks a lot. It makes much more sense to me now. What if I want a date iterator for example? Should I just use boost::date_time?(since Im 99% certain i'll only be using gregorian calendar anyway) Or does booster offer something similar? Petr |
From: Artyom B. <art...@ya...> - 2012-02-06 09:41:14
|
> >> If you want to store only date you have two options: >> >> Use string and format it as "YYYY-MM-DD" or >> use std::tm with relevant fields set if the SQL engine >> knows to convert one type to another. > > > Thanks a lot. It makes much more sense to me now. > > What if I want a date iterator for example? See: Boost.Locale date-time tutorial: http://www.boost.org/doc/libs/1_48_0/libs/locale/doc/html/dates_times_timezones.html Calendar example: http://www.boost.org/doc/libs/1_48_0/libs/locale/doc/html/calendar_8cpp-example.html Note Booster.Locale == Boost.Locale in different namespace so everything applicable to Boost.Locale is same for Booster.Locale. > Should I just use > boost::date_time? >(since Im 99% certain i'll only be using gregorian > calendar anyway) Or does booster offer something similar? > You can use boost::date_time but... I'd recommend to use boost(er)::locale::date_time as it offers more power as it locale dependent. See: http://www.boost.org/doc/libs/1_48_0/libs/locale/doc/html/dates_times_timezones.html#dates_times_timezones_qn For example what if you want to display a calendar for a current month? Where should you start a week at? boost(er)::locale::date_time gives you that knowledge while Boost.DateTime does not. So it is really up-to-you Artyom Beilis ------------- Support CppCMS by donating money: https://sourceforge.net/donate/index.php?group_id=209965 |
From: <ele...@ex...> - 2012-02-07 02:55:36
|
Thank you very much for helping me understand cppcms. But I do have one more question unrelated to dates - What's the best way to check a cookie inside a template view? For example - a person logs in, and then on every page it will say "Hello ${user}". The example blog application seems use java script to do some client side checking, but what's the best way to do it on the server side? In simplistic terms what I'm trying to do in the template is something like this <%if is_logged_in()%> <% gt "Hello {1}" using first_name%> <%else%> Please login below: <%end%> Thanks Petr |
From: Artyom B. <art...@ya...> - 2012-02-07 11:37:56
|
----- Original Message ----- > From: "ele...@ex..." <ele...@ex...> > > [snip] > > The example blog application seems use java script to do some client side > checking, but what's the best way to do it on the server side? > I use javascript because I want to cache pages and so I don't need to have separate version for visitor and administrator. > In simplistic terms what I'm trying to do in the template is something > like this > > <%if is_logged_in()%> > <% gt "Hello {1}" using first_name%> > <%else%> > Please login below: > <%end%> > a) you can create a variable called first_name that you set in the controller that remains empty if the user is not logged in <%if not empty first_name %> <% gt "Hello {1}" using first_name%> <%else%> Please login below: <%end%> b) You can create a variable or callback in the content called "is_logged_in" c) You can use session object directly <% if app().session().is_set("username") %> <% gt "Hello {1}" using first_name%> <%else%> Please login below: <%end%> Enjoy :-) Artyom Beilis ------------- Support CppCMS by donating money: https://sourceforge.net/donate/index.php?group_id=209965 |
From: <ele...@ex...> - 2012-02-09 23:08:23
|
Hi, > a) you can create a variable called first_name that you set in the > controller > that remains empty if the user is not logged in > > <%if not empty first_name %> > <% gt "Hello {1}" using first_name%> > <%else%> > Please login below: > <%end%> In the controller? Using this method I get view/blog.tmpl:21: error: 'class data::Blog' has no member named 'first_name' > b) You can create a variable or callback in the content called > "is_logged_in" In the "content", do you mean in the template or in the data model? Cheers, Petr |
From: Artyom B. <art...@ya...> - 2012-02-10 12:25:03
|
> >Hi, > >> a) you can create a variable called first_name that you set in the >> controller >> that remains empty if the user is not logged in >> >> <%if not empty first_name %> >> <% gt "Hello {1}" using first_name%> >> <%else%> >> Please login below: >> <%end%> > >In the controller? > >Using this method I get > >view/blog.tmpl:21: error: 'class data::Blog' has no member named 'first_name' > > >> b) You can create a variable or callback in the content called >> "is_logged_in" > >In the "content", do you mean in the template or in the data model? > >Cheers, >Petr > > > Maybe RTFM? http://cppcms.com/wikipp/en/page/cppcms_1x_tut_hello_templates http://cppcms.com/wikipp/en/page/cppcms_1x_templates Artyom |