Thread: [Cppcms-users] How to format readable date from unix timestamp within template?
Brought to you by:
artyom-beilis
|
From: augustin <aug...@ov...> - 2013-12-08 05:59:59
|
Hello,
How to format a readable date from a unix timestamp within a template?
I thought it would be something straightforward to do but I seem to be stuck.
I've spent a fair amount of time reading the API documentation, the cppcms
wiki, and code examples found in cpp blog, but the only thing I ever get
displayed within my application is the unix timestamp itself.
I have tried all of the following within my .tmpl file, but none of them
actually filter the timestamp:
r.date_returned is a time_t unix timestamp.
<% gt "{1,datetime=s}" using r.date_returned %>
<%= r.date_returned | date %>
<%= r.date_returned | strftime("%d/%m/%Y") %>
What am I missing?
thanks,
augustin.
|
|
From: Artyom B. <art...@ya...> - 2013-12-08 09:52:38
|
> r.date_returned is a time_t unix timestamp.
>
> <% gt "{1,datetime=s}" using r.date_returned %>
> <%= r.date_returned | date %>
> <%= r.date_returned | strftime("%d/%m/%Y") %>
> What am I missing?
>
> thanks,
>
> augustin.
It actually should work....
How do you render the template?
What are the locale settings?
Do you render to a stream that isn't the respone().out()?
The only point I can see that this isn't working is that the stream you
are rendering into is missing the locale configuration.
Artyom Beilis
--------------
CppCMS - C++ Web Framework: http://cppcms.com/
CppDB - C++ SQL Connectivity: http://cppcms.com/sql/cppdb/
|
|
From: augustin <aug...@ov...> - 2013-12-09 00:25:45
|
On Sunday, December 08, 2013 05:52:31 PM Artyom Beilis wrote:
> > r.date_returned is a time_t unix timestamp.
> >
> > <% gt "{1,datetime=s}" using r.date_returned %>
> > <%= r.date_returned | date %>
> > <%= r.date_returned | strftime("%d/%m/%Y") %>
> >
> > What am I missing?
> >
> > thanks,
> >
> > augustin.
>
> It actually should work....
>
> How do you render the template?
> What are the locale settings?
> Do you render to a stream that isn't the respone().out()?
>
> The only point I can see that this isn't working is that the stream you
> are rendering into is missing the locale configuration.
Thanks for your reply, Artyom, as always.
For my locale, I have in config.js:
"localization" : {
"locales" : ["en_US.UTF-8"],
},
I have not yet specifically studied locale and translations. For the time
being English is hard coded in my application.
That particular piece of template is rendered thus:
namespace content {
struct book_page : public cppcms::base_content;
}
void view_book::render_main_content(content::page* cp) {
content::book_page c;
// ... snipped parts where I add content to c, including r.date_returned.
std::ostringstream buf;
// app() returns a pointer to my class myApp : public cppcms::application;
app()->render(std::string("my_skin"), std::string("book_page"), buf, c);
cp->rContent = buf.str();
}
I have simplified the code above. Hopefully, I have not removed anything
relevant.
The wiki page about template filters is pretty long:
http://cppcms.com/wikipp/en/page/cppcms_1x_templates_comm
Do you think I should complement it by setting up a separate wiki page about
best practices for rendering times and dates?
Thanks,
augustin.
--
Friends: http://www.reuniting.info/
My projects:
http://astralcity.org/ http://lesenjeux.fr/ http://linux.overshoot.tv/
http://overshoot.tv/ http://charityware.info/ http://masquilier.org/
http://openteacher.info/ http://minguo.info/ http://jacqueslemaire.fr/
http://www.wechange.org/ http://searching911.info/
.
|
|
From: Artyom B. <art...@ya...> - 2013-12-09 06:24:50
|
The problem with this clode
std::ostringstream buf;
app()->render(std::string("my_skin"), std::string("book_page"), buf, c);
cp->rContent = buf.str();
is that buf does not have a locale object associated with it.
You need to add following line
buf.imbue(app()->context().locale());
i.e.
std::ostringstream buf;
buf.imbue(app()->context().locale());
app()->render(std::string("my_skin"), std::string("book_page"), buf, c);
cp->rContent = buf.str();
The response().out() - the default output already has a locale embedded in it.
when you create a generic std::ostream you need to imbue application
specific locale (even if it is default system locale) to make various localization
related tools to work.
Artyom Beilis
--------------
CppCMS - C++ Web Framework: http://cppcms.com/
CppDB - C++ SQL Connectivity: http://cppcms.com/sql/cppdb/
--------------------------------------------
On Mon, 12/9/13, augustin <aug...@ov...> wrote:
Subject: Re: [Cppcms-users] How to format readable date from unix timestamp within template?
To: cpp...@li...
Date: Monday, December 9, 2013, 2:25 AM
On Sunday, December 08, 2013 05:52:31
PM Artyom Beilis wrote:
> > r.date_returned is a time_t unix timestamp.
> >
> > <% gt "{1,datetime=s}" using
r.date_returned %>
> > <%= r.date_returned | date %>
> > <%= r.date_returned |
strftime("%d/%m/%Y") %>
> >
> > What am I missing?
> >
> > thanks,
> >
> > augustin.
>
> It actually should work....
>
> How do you render the template?
> What are the locale settings?
> Do you render to a stream that isn't the
respone().out()?
>
> The only point I can see that this isn't working is
that the stream you
> are rendering into is missing the locale
configuration.
Thanks for your reply, Artyom, as always.
For my locale, I have in config.js:
"localization" : {
"locales" : ["en_US.UTF-8"],
},
I have not yet specifically studied locale and translations.
For the time
being English is hard coded in my application.
That particular piece of template is rendered thus:
namespace content {
struct book_page : public cppcms::base_content;
}
void view_book::render_main_content(content::page* cp) {
content::book_page c;
// ... snipped parts where I add content to c,
including r.date_returned.
std::ostringstream buf;
// app() returns a pointer to my class myApp : public
cppcms::application;
app()->render(std::string("my_skin"),
std::string("book_page"), buf, c);
cp->rContent = buf.str();
}
I have simplified the code above. Hopefully, I have not
removed anything
relevant.
The wiki page about template filters is pretty long:
http://cppcms.com/wikipp/en/page/cppcms_1x_templates_comm
Do you think I should complement it by setting up a separate
wiki page about
best practices for rendering times and dates?
Thanks,
augustin.
--
Friends: http://www.reuniting.info/
My projects:
http://astralcity.org/ http://lesenjeux.fr/ http://linux.overshoot.tv/
http://overshoot.tv/ http://charityware.info/ http://masquilier.org/
http://openteacher.info/ http://minguo.info/ http://jacqueslemaire.fr/
http://www.wechange.org/ http://searching911.info/
.
------------------------------------------------------------------------------
Sponsored by Intel(R) XDK
Develop, test and display web and hybrid apps with a single
code base.
Download it for free now!
http://pubads.g.doubleclick.net/gampad/clk?id=111408631&iu=/4140/ostg.clktrk
_______________________________________________
Cppcms-users mailing list
Cpp...@li...
https://lists.sourceforge.net/lists/listinfo/cppcms-users
|
|
From: augustin <aug...@ov...> - 2013-12-10 01:54:34
|
On Monday, December 09, 2013 02:24:42 PM Artyom Beilis wrote:
> The problem with this clode
>
> std::ostringstream buf;
> app()->render(std::string("my_skin"), std::string("book_page"), buf, c);
> cp->rContent = buf.str();
>
> is that buf does not have a locale object associated with it.
>
> You need to add following line
>
> buf.imbue(app()->context().locale());
>
> i.e.
>
> std::ostringstream buf;
> buf.imbue(app()->context().locale());
> app()->render(std::string("my_skin"), std::string("book_page"), buf, c);
> cp->rContent = buf.str();
>
> The response().out() - the default output already has a locale embedded in
> it. when you create a generic std::ostream you need to imbue application
> specific locale (even if it is default system locale) to make various
> localization related tools to work.
That's it!
Thanks a lot.
Do you think it would be useful for me to add something about this in the
wiki?
If you wish, I can create a page in an appropriate place to document such
newbie gotchas.
augustin.
.
|
|
From: sergey l. <ccp...@gm...> - 2013-12-10 03:24:29
|
Hello friends! There are some references about locale in stream in documentation. http://cppcms.com/cppcms_ref/latest/classcppcms_1_1application.html#a3a1342b4834c8cdb79c2619dff010319 Render a template template_name of a skin skin using content content to an output stream out. Note: You are responsible to imbue suitable locale to the stream. On Tue, Dec 10, 2013 at 5:54 AM, augustin <aug...@ov...>wrote: > On Monday, December 09, 2013 02:24:42 PM Artyom Beilis wrote: > > The problem with this clode > > > > std::ostringstream buf; > > app()->render(std::string("my_skin"), std::string("book_page"), buf, c); > > cp->rContent = buf.str(); > > > > is that buf does not have a locale object associated with it. > > > > You need to add following line > > > > buf.imbue(app()->context().locale()); > > > > i.e. > > > > std::ostringstream buf; > > buf.imbue(app()->context().locale()); > > app()->render(std::string("my_skin"), std::string("book_page"), buf, c); > > cp->rContent = buf.str(); > > > > The response().out() - the default output already has a locale embedded > in > > it. when you create a generic std::ostream you need to imbue application > > specific locale (even if it is default system locale) to make various > > localization related tools to work. > > That's it! > Thanks a lot. > > Do you think it would be useful for me to add something about this in the > wiki? > If you wish, I can create a page in an appropriate place to document such > newbie gotchas. > > > augustin. > > > > > . > > > ------------------------------------------------------------------------------ > Sponsored by Intel(R) XDK > Develop, test and display web and hybrid apps with a single code base. > Download it for free now! > > http://pubads.g.doubleclick.net/gampad/clk?id=111408631&iu=/4140/ostg.clktrk > _______________________________________________ > Cppcms-users mailing list > Cpp...@li... > https://lists.sourceforge.net/lists/listinfo/cppcms-users > |