Tosh Cooey wrote:
> Maybe somebody found something less fugly?

Hi Tosh,

You might like to have a look at Badger::Timestamp. It is (or eventually will
be) the successor to the Date plugin.  The main difference is that it's
written as a stand-alone Perl module rather than as a TT-specific plugin.
The TT plugin will be a very thin wrapper around it.

http://badgerpower.com/docs/Badger/Timestamp.html

There isn't a TT plugin interface to it yet, but it would be trivially easy
to write one.  Or you can define a constructor subroutine and pass it into
your templates as a variable, as shown in the example attached below.

It's intentionally simple in that it only recognises IS0-8601 timestamps
(or something suitably similar, like MySQL timestamps) or a time in epoch
seconds.  But that covers 99% of the *simple* things that I've ever wanted to
do in a template: mainly pretty-printing database timestamps and file
creation / modified dates.

However, it's also got adjust() and before()/after() methods that are
sufficient to work out when 5 minutes ago was and see if another timestamp
is before or after that point.  Or you can just have it convert the db
timestamp and current time to epoch seconds (epoch_time()) and > 600 it
yourself.

For anything more substantial, DateTime is probably your best bet.

Cheers
A



#!/usr/bin/perl
#
# Perl script to demonstrate Badger::Timestamp
#
# Written by Andy Wardley http://wardley.org/
#
# 10 June 2009
#

use Template;
use Badger::Timestamp;

my $tt   = Template->new;
my $vars = {
     stamp => sub { Badger::Timestamp->new(@_) }
};

$tt->process(\*DATA,  $vars)
     || die $tt->error;


__END__
[% # you can pass a timestamp argument
    t = stamp('2009-06-10 16:29:04')
%]
timestamp: [% t %]
date: [% t.date %]
time: [% t.time %]
hour: [% t.hour %]
epoch time: [% t.epoch_time %]

[% # or you get the current time %]
The current time is [% stamp %]

[% # you can adjust a timestamp to change the time/date %]
[% ago4 = stamp.adjust( minutes = -4 ) -%]
4 minutes ago the time was [% ago4.time %]
[% ago5 = stamp.adjust( minutes = -5 ) -%]
5 minutes ago the time was [% ago5.time %]
[% ago6 = stamp.adjust( minutes = -6 ) -%]
6 minutes ago the time was [% ago6.time %]

[% # before() and after() allow you to compare timestamps %]
Is [% ago4 %] before 5 minutes ago? [% ago4.before(ago5) ? 'YES' : 'NO' %]
Is [% ago6 %] before 5 minutes ago? [% ago6.before(ago5) ? 'YES' : 'NO' %]


_______________________________________________
templates mailing list
[email protected]
http://mail.template-toolkit.org/mailman/listinfo/templates

Reply via email to