格式化时间戳字段以在TemplateToolkit中输出 [英] Formatting timestamp field for output in TemplateToolkit

查看:104
本文介绍了格式化时间戳字段以在TemplateToolkit中输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Perl与Catalyst框架配合使用,DBIx类用作ORM,TT用于视图,Postgresql作为数据库。

I'm using Perl with Catalyst framework, DBIx class as ORM, TT for views, Postgresql as DB.

我有一列具有没有时区的时间戳记类型的列,如果我在Postgres中手动查询,则列值的格式为 2012- 08-30 21:30:14 ,但是当我在TT视图文件中打印值时,我会像这样 2012-08-30T15:03:13 ,因此很显然它已格式化,但按我无法告诉的内容。

I have a column with 'timestamp without timezone' type, and if I do manual query in Postgres the column value is in such format 2012-08-30 21:30:14, but when I print the value in TT view file I get it like this 2012-08-30T15:03:13, so obviously it gets formatted but by what exactly I can't tell.

我想使用 Template :: Plugin :: Date 格式化输出时间戳,但出现Catalyst错误:

I want to use Template::Plugin::Date to format output timestamps, but I get Catalyst error:


无法呈现模板 xxx / list.tt2:日期错误-错误的时间/日期字符串:期望'h:m:sd:m: y'got:'2012-08-30T21:28:22'

Couldn't render template "xxx/list.tt2: date error - bad time/date string: expects 'h:m:s d:m:y' got: '2012-08-30T21:28:22'"

在xxx / list.tt2中有代码 [%date.format(xxx.created)%]

with code in xxx/list.tt2 [% date.format(xxx.created) %]

那么我应该如何使其工作?

So how am I supposed to make it work? Thanks in advance.

推荐答案

您已配置DBIx :: Class将'created'列的值增加到 DateTime 对象。

You configured DBIx::Class to inflate the value of the 'created' column to a DateTime object.

请注意,您只需要加载基于InflateColumn :: DateTime的TimeStamp组件,不能同时基于两者!

Note that you only need to load the TimeStamp component which is based on InflateColumn::DateTime, not both!

此外,您还应该添加 on_connect_call => Datetime_setup 到您的DBIx :: Class connect_info ,以使DBIC设置数据库日期时间格式,使其与DateTime对象膨胀期望的格式匹配。这样做对每个受支持的数据库都是正确的,因此,如果您切换数据库或使用SQLite进行测试,它也将起作用。

Furthermore you should add on_connect_call => 'datetime_setup' to your DBIx::Class connect_info to make DBIC set the database datetime format to match what it expects for the DateTime object inflation. This does the right thing for every supported database so it will also work if you switch database or use SQLite for testing.

Template :: Plugin :: Date 不适用于处理DateTime对象, Template :: Plugin :: DateTime 是。

Template::Plugin::Date is not for handling DateTime objects, Template::Plugin::DateTime is.

关于模板渲染,我建议您使用 Catalyst :: View :: TT#expose_methods 功能,方法是在Catalyst :: View :: TT视图中添加方法,该方法将传递DateTime对象并返回格式化的字符串。您可以为不同的格式添加多种方法,例如日期+时间,仅日期等。
这样,您可以在中心位置定义DateTime格式,并可以根据需要轻松更改。

Regarding the template rendering I suggest you use Catalyst::View::TT#expose_methods feature by adding a method to your Catalyst::View::TT view which gets passed a DateTime object and returns a formatted string. You can add multiple methods for different formats, for example date + time, date only etc. This way you have a central location which defines the DateTime formatting which can be easily changed if needed.

这篇关于格式化时间戳字段以在TemplateToolkit中输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆