Mailing List Archive

Changing format of date field
Hi,

I have a date field in a DBIx::Class Result class using
InflateColumn::DateTime. When I pass this through a Catalyst/TT
application, the date is presented in YYYY-MM-DD format.

I can modify this to DD/MM/YYYY in my TT template with object.dmy('/'),
but I would prefer to change the default DateTime stringification,
however I am not quite sure where or how to do this?

Any ideas greatly appreciated.

Thanks

Adam

_______________________________________________
List: Catalyst@lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/
Re: Changing format of date field [ In reply to ]
DateTime has an option to change the default stringification with a
'formatter' option.

Also DBIx::Class::InflateColumn::DateTime is a convenience method, you
could choose to use the DBIx::Class::InflateColumn yourself and combine
this with the DateTime formatter option to give you the result you are
looking for.

icydee


On 6 January 2014 15:18, Adam Witney <awitney@sgul.ac.uk> wrote:

> Hi,
>
> I have a date field in a DBIx::Class Result class using
> InflateColumn::DateTime. When I pass this through a Catalyst/TT
> application, the date is presented in YYYY-MM-DD format.
>
> I can modify this to DD/MM/YYYY in my TT template with object.dmy('/'),
> but I would prefer to change the default DateTime stringification, however
> I am not quite sure where or how to do this?
>
> Any ideas greatly appreciated.
>
> Thanks
>
> Adam
>
> _______________________________________________
> List: Catalyst@lists.scsys.co.uk
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive: http://www.mail-archive.com/
> catalyst@lists.scsys.co.uk/
> Dev site: http://dev.catalyst.perl.org/
>
Re: Changing format of date field [ In reply to ]
On Mon, Jan 6, 2014 at 7:18 AM, Adam Witney <awitney@sgul.ac.uk> wrote:

> Hi,
>
> I have a date field in a DBIx::Class Result class using
> InflateColumn::DateTime. When I pass this through a Catalyst/TT
> application, the date is presented in YYYY-MM-DD format.
>
> I can modify this to DD/MM/YYYY in my TT template with object.dmy('/'),
> but I would prefer to change the default DateTime stringification, however
> I am not quite sure where or how to do this?
>
> Any ideas greatly appreciated.
>

Seem like formatting in the template is the right place -- may want to have
different formats in different places in your app. Then you might also
think about how best to localize.

One idea is to localize a set of names for a set of formats:

[.% dt_fmt = c.localize( '_DT_DATE_ONLY' ); foo.some_dt_object.strftime(
dt_fmt ) | html %]


I have also used a function that does the above, but also clones and sets
the time zone and locale based on the user's preferences:

[% user_time( foo.event_start, '_DT_TIMESTAMP_WITH_ZONE' ) | html %]




--
Bill Moseley
moseley@hank.org
Re: Changing format of date field [ In reply to ]
Thanks for your reply Ian,

So you mean copy DBIx::Class::InflateColumn::DateTime under my lib/
directory and build into the DateTime formatter option?

On 6. 1. 2014 15:42, Ian Docherty wrote:
> DateTime has an option to change the default stringification with a
> 'formatter' option.
>
> Also DBIx::Class::InflateColumn::DateTime is a convenience method, you
> could choose to use the DBIx::Class::InflateColumn yourself and combine
> this with the DateTime formatter option to give you the result you are
> looking for.
>
> icydee
>
>
> On 6 January 2014 15:18, Adam Witney <awitney@sgul.ac.uk
> <mailto:awitney@sgul.ac.uk>> wrote:
>
> Hi,
>
> I have a date field in a DBIx::Class Result class using
> InflateColumn::DateTime. When I pass this through a Catalyst/TT
> application, the date is presented in YYYY-MM-DD format.
>
> I can modify this to DD/MM/YYYY in my TT template with
> object.dmy('/'), but I would prefer to change the default DateTime
> stringification, however I am not quite sure where or how to do this?
>
> Any ideas greatly appreciated.
>
> Thanks
>
> Adam
>
> _________________________________________________
> List: Catalyst@lists.scsys.co.uk <mailto:Catalyst@lists.scsys.co.uk>
> Listinfo:
> http://lists.scsys.co.uk/cgi-__bin/mailman/listinfo/catalyst
> <http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst>
> Searchable archive:
> http://www.mail-archive.com/__catalyst@lists.scsys.co.uk/
> <http://www.mail-archive.com/catalyst@lists.scsys.co.uk/>
> Dev site: http://dev.catalyst.perl.org/
>
>
>
>
> _______________________________________________
> List: Catalyst@lists.scsys.co.uk
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
> Dev site: http://dev.catalyst.perl.org/
>

_______________________________________________
List: Catalyst@lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/
Re: Changing format of date field [ In reply to ]
I suggested that you extend DBIx::Class::InflateColumn to create
DBIx::Class::InflateColumn::MyDateTime to give you the result you need.

However, I would state that although I have shown you how to do this,
personally I would keep decisions about how to display data where it
belongs, in the view, and stay with the object.dmy() which makes it quite
clear your intent, rather than doing it 'at a distance' in the model.

Kind Regards
icydee


On 7 January 2014 09:32, Adam Witney <awitney@sgul.ac.uk> wrote:

>
> Thanks for your reply Ian,
>
> So you mean copy DBIx::Class::InflateColumn::DateTime under my lib/
> directory and build into the DateTime formatter option?
>
>
> On 6. 1. 2014 15:42, Ian Docherty wrote:
>
>> DateTime has an option to change the default stringification with a
>> 'formatter' option.
>>
>> Also DBIx::Class::InflateColumn::DateTime is a convenience method, you
>> could choose to use the DBIx::Class::InflateColumn yourself and combine
>> this with the DateTime formatter option to give you the result you are
>> looking for.
>>
>> icydee
>>
>>
>> On 6 January 2014 15:18, Adam Witney <awitney@sgul.ac.uk
>> <mailto:awitney@sgul.ac.uk>> wrote:
>>
>> Hi,
>>
>> I have a date field in a DBIx::Class Result class using
>> InflateColumn::DateTime. When I pass this through a Catalyst/TT
>> application, the date is presented in YYYY-MM-DD format.
>>
>> I can modify this to DD/MM/YYYY in my TT template with
>> object.dmy('/'), but I would prefer to change the default DateTime
>> stringification, however I am not quite sure where or how to do this?
>>
>> Any ideas greatly appreciated.
>>
>> Thanks
>>
>> Adam
>>
>> _________________________________________________
>> List: Catalyst@lists.scsys.co.uk <mailto:Catalyst@lists.scsys.co.uk>
>> Listinfo:
>> http://lists.scsys.co.uk/cgi-__bin/mailman/listinfo/catalyst
>> <http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst>
>> Searchable archive:
>> http://www.mail-archive.com/__catalyst@lists.scsys.co.uk/
>>
>> <http://www.mail-archive.com/catalyst@lists.scsys.co.uk/>
>> Dev site: http://dev.catalyst.perl.org/
>>
>>
>>
>>
>> _______________________________________________
>> List: Catalyst@lists.scsys.co.uk
>> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
>> Searchable archive: http://www.mail-archive.com/
>> catalyst@lists.scsys.co.uk/
>> Dev site: http://dev.catalyst.perl.org/
>>
>>
> _______________________________________________
> List: Catalyst@lists.scsys.co.uk
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive: http://www.mail-archive.com/
> catalyst@lists.scsys.co.uk/
> Dev site: http://dev.catalyst.perl.org/
>
Re: Changing format of date field [ In reply to ]
On 6. 1. 2014 21:41, Bill Moseley wrote:
>
> On Mon, Jan 6, 2014 at 7:18 AM, Adam Witney <awitney@sgul.ac.uk
> <mailto:awitney@sgul.ac.uk>> wrote:
>
> Hi,
>
> I have a date field in a DBIx::Class Result class using
> InflateColumn::DateTime. When I pass this through a Catalyst/TT
> application, the date is presented in YYYY-MM-DD format.
>
> I can modify this to DD/MM/YYYY in my TT template with
> object.dmy('/'), but I would prefer to change the default DateTime
> stringification, however I am not quite sure where or how to do this?
>
> Any ideas greatly appreciated.
>
>
> Seem like formatting in the template is the right place -- may want to
> have different formats in different places in your app. Then you might
> also think about how best to localize.
>
> One idea is to localize a set of names for a set of formats:
>
> [.% dt_fmt = c.localize( '_DT_DATE_ONLY' );
> foo.some_dt_object.strftime( dt_fmt ) | html %]
>
>
> I have also used a function that does the above, but also clones and
> sets the time zone and locale based on the user's preferences:
>
> [% user_time( foo.event_start, '_DT_TIMESTAMP_WITH_ZONE' ) | html %]

Hi Bill,

Thanks for your email. I agree that the template seems like a good place
for this, but to give a little extra detail.

I have a form that is used for both object create and editing. When I
edit an object I have this in my controller:

$c->stash(formdata => $object);

and in the template:

<input name="datefield" type="text" value="[%
formdata.datefield.dmy('/') %]" />

But when creating a new object, if the form validation fails I pass the
form data back to the form like so:

$c->stash( formdata => $c->request->params );

But now the datefield is a text string and formdata.datefield.dmy('/')
prints blank.

I was thinking that if I could define my DateTime stringification to my
required format centrally then I could just use this in the template

<input name="datefield" type="text" value="[% formdata.datefield %]" />

and both scenarios would be happy.

Is the only way to do this to put more code in the template?

Thanks again for your help

Adam

_______________________________________________
List: Catalyst@lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/
Re: Changing format of date field [ In reply to ]
On 7/01/2014 8:41 PM, Adam Witney wrote:
>
>
> On 6. 1. 2014 21:41, Bill Moseley wrote:
>>
>> On Mon, Jan 6, 2014 at 7:18 AM, Adam Witney <awitney@sgul.ac.uk
>> <mailto:awitney@sgul.ac.uk>> wrote:
>>
>> Hi,
>>
>> I have a date field in a DBIx::Class Result class using
>> InflateColumn::DateTime. When I pass this through a Catalyst/TT
>> application, the date is presented in YYYY-MM-DD format.
>>
>> I can modify this to DD/MM/YYYY in my TT template with
>> object.dmy('/'), but I would prefer to change the default DateTime
>> stringification, however I am not quite sure where or how to do
>> this?
>>
>> Any ideas greatly appreciated.
>>
>>
>> Seem like formatting in the template is the right place -- may want to
>> have different formats in different places in your app. Then you might
>> also think about how best to localize.
>>
>> One idea is to localize a set of names for a set of formats:
>>
>> [.% dt_fmt = c.localize( '_DT_DATE_ONLY' );
>> foo.some_dt_object.strftime( dt_fmt ) | html %]
>>
>>
>> I have also used a function that does the above, but also clones and
>> sets the time zone and locale based on the user's preferences:
>>
>> [% user_time( foo.event_start, '_DT_TIMESTAMP_WITH_ZONE' ) | html %]
>
> Hi Bill,
>
> Thanks for your email. I agree that the template seems like a good
> place for this, but to give a little extra detail.
>
> I have a form that is used for both object create and editing. When I
> edit an object I have this in my controller:
>
> $c->stash(formdata => $object);
>
> and in the template:
>
> <input name="datefield" type="text" value="[%
> formdata.datefield.dmy('/') %]" />
>
> But when creating a new object, if the form validation fails I pass
> the form data back to the form like so:
>
> $c->stash( formdata => $c->request->params );
>
> But now the datefield is a text string and formdata.datefield.dmy('/')
> prints blank.
Yep. Your form response param is going to be text and not a datetime
object anymore. At any rate here is your case if I am reading correctly.

1. Model returns an inflated Datetime object. This is parsed from a
string based on the Driver backend you have. Which is what inflate
column does.
2. You don't want to have the default "stringify" displayed. So either
change the stashed value or represent differently in view.
3. You *Are* going to get back a string in the raw params. So what you
need to do is parse that format into a DateTime object again when
submitting back to model or wherever else you want to use it as an object.

You can either do this in your controller directly or look at the
various Form handling modules on CPAN where you can set up the
Inflation/Deflation to your needs. All of these give you another object
to deal with but raw input params are just going to be strings without
further handling.
>
> I was thinking that if I could define my DateTime stringification to
> my required format centrally then I could just use this in the template
>
> <input name="datefield" type="text" value="[% formdata.datefield %]" />
>
> and both scenarios would be happy.
>
> Is the only way to do this to put more code in the template?
>
> Thanks again for your help
>
> Adam
>
> _______________________________________________
> List: Catalyst@lists.scsys.co.uk
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive:
> http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
> Dev site: http://dev.catalyst.perl.org/


---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com


_______________________________________________
List: Catalyst@lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/
Re: Changing format of date field [ In reply to ]
On 7. 1. 2014 9:55, neil.lunn wrote:

> 3. You *Are* going to get back a string in the raw params. So what you
> need to do is parse that format into a DateTime object again when
> submitting back to model or wherever else you want to use it as an object.

Thanks guys for the various suggestions, I think I will follow this
approach above and leave the format to the view as suggested!

Thanks again

Adam

_______________________________________________
List: Catalyst@lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/
Re: Changing format of date field [ In reply to ]
On 2014-01-08 18:19, Adam Witney wrote:
>
>
> On 7. 1. 2014 9:55, neil.lunn wrote:
>
>> 3. You *Are* going to get back a string in the raw params. So what you
>> need to do is parse that format into a DateTime object again when
>> submitting back to model or wherever else you want to use it as an
>> object.
>
> Thanks guys for the various suggestions, I think I will follow this
> approach above and leave the format to the view as suggested!
>
> Thanks again
>
> Adam
I have various format_ methods in my view class that are exposed to TT
using View::TT's expose_methods config option including format_date,
format_datetime and format_datetime_ajax.


*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*
T-Systems Austria GesmbH Rennweg 97-99, 1030 Wien
Handelsgericht Wien, FN 79340b
*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*
Notice: This e-mail contains information that is confidential and may be privileged.
If you are not the intended recipient, please notify the sender and then
delete this e-mail immediately.
*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*

_______________________________________________
List: Catalyst@lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/
Re: Changing format of date field [ In reply to ]
On 8. 1. 2014 17:31, Hartmaier Alexander wrote:
> On 2014-01-08 18:19, Adam Witney wrote:
>>
>>
>> On 7. 1. 2014 9:55, neil.lunn wrote:
>>
>>> 3. You *Are* going to get back a string in the raw params. So what you
>>> need to do is parse that format into a DateTime object again when
>>> submitting back to model or wherever else you want to use it as an
>>> object.
>>
>> Thanks guys for the various suggestions, I think I will follow this
>> approach above and leave the format to the view as suggested!
>>
>> Thanks again
>>
>> Adam
> I have various format_ methods in my view class that are exposed to TT
> using View::TT's expose_methods config option including format_date,
> format_datetime and format_datetime_ajax.

Thanks Alexander, could you possibly send me an example of the
format_datetime? I am trying to figure exactly where and how to put it in.

Thanks

Adam

_______________________________________________
List: Catalyst@lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/
Re: Changing format of date field [ In reply to ]
On 2014-01-08 18:56, Adam Witney wrote:
>
>
> On 8. 1. 2014 17:31, Hartmaier Alexander wrote:
>> On 2014-01-08 18:19, Adam Witney wrote:
>>>
>>>
>>> On 7. 1. 2014 9:55, neil.lunn wrote:
>>>
>>>> 3. You *Are* going to get back a string in the raw params. So what you
>>>> need to do is parse that format into a DateTime object again when
>>>> submitting back to model or wherever else you want to use it as an
>>>> object.
>>>
>>> Thanks guys for the various suggestions, I think I will follow this
>>> approach above and leave the format to the view as suggested!
>>>
>>> Thanks again
>>>
>>> Adam
>> I have various format_ methods in my view class that are exposed to TT
>> using View::TT's expose_methods config option including format_date,
>> format_datetime and format_datetime_ajax.
>
> Thanks Alexander, could you possibly send me an example of the
> format_datetime? I am trying to figure exactly where and how to put it
> in.
>
> Thanks
>
> Adam
>
package NAC::Web::NAC::View::HTML;

use strict;
use warnings;
use Safe::Isa;
use parent 'Catalyst::View::TT';

__PACKAGE__->config(
TEMPLATE_EXTENSION => '.tt',
render_die => 1,
CONSTANTS => { version => $NAC::Web::NAC::VERSION, },
ENCODING => 'utf-8',
expose_methods => [
qw( format_bps format_bytes format_datetime format_date
format_datetime_ajax format_int_speed format_int_speed_real )
],
);

=head1 NAME

NAC::Web::NAC::View::HTML - TT View for NAC::Web::NAC

=head1 DESCRIPTION

TT View for NAC::Web::NAC.

=over

=item format_datetime

Returns a scalar from a DateTime object stringified to %Y-%m-%d %H:%M %z.

=cut

sub format_datetime {
my ( $self, $c, $datetime ) = @_;

if ( $datetime->$_isa('DateTime') ) {

# FIXME: yes that's ugly, but else we'd need to get the users
timezone or format client-side
return $datetime->clone->set_time_zone('Europe/Vienna')
->strftime('%Y-%m-%d %H:%M %z');
}

return;
}

1;


*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*
T-Systems Austria GesmbH Rennweg 97-99, 1030 Wien
Handelsgericht Wien, FN 79340b
*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*
Notice: This e-mail contains information that is confidential and may be privileged.
If you are not the intended recipient, please notify the sender and then
delete this e-mail immediately.
*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*

_______________________________________________
List: Catalyst@lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/
Re: Changing format of date field [ In reply to ]
On Tue, Jan 7, 2014 at 1:41 AM, Adam Witney <awitney@sgul.ac.uk> wrote:

> [% user_time( foo.event_start, '_DT_TIMESTAMP_WITH_ZONE' ) | html %]
>
> Hi Bill,
>
> Thanks for your email. I agree that the template seems like a good place
> for this, but to give a little extra detail.
>
> I have a form that is used for both object create and editing. When I edit
> an object I have this in my controller:
>
> $c->stash(formdata => $object);
>
> and in the template:
>
> <input name="datefield" type="text" value="[% formdata.datefield.dmy('/')
> %]" />
>
> But when creating a new object, if the form validation fails I pass the
> form data back to the form like so:
>
> $c->stash( formdata => $c->request->params );
>

Have you looked at HTML::FormHandler? I would assume it addresses this.
I use something similar and the concept is that you have a field object
that knows how to render either from the existing object or from user
provided input.



--
Bill Moseley
moseley@hank.org