Thanks Tom, this is what I was also thinking, but it just doesn't work, 
'-1' keeps being returned.

After digging into the source of the Date plugin:
http://cpansearch.perl.org/src/ABW/Template-Toolkit-2.20_4/lib/Template/Plugin/Date.pm

The docs say:
# specify time as seconds since epoch
# or as a 'h:m:s d-m-y' or 'y-m-d h:m:s' string
[% date.format('4:20:36 21/12/2000') %]
[% date.format('2000/12/21 4:20:36') %]

But neither of the above will work.  This however does:
[% date.format('4:20:36 21-12-2000') %]

So the solution to my problem is this monstrosity:

[% formatted_date = record.0.timestamped.split(' ').1 _ ' ' _ 
record.0.timestamped.split(' ').0.split('-').reverse.join('-') %]

This will convert a MySQL date looking like this:
2009-06-09 09:33:39

Into this:
09:33:39 09-06-2009

Which can then be used as you indicated below:

[% IF date.now - date.format(formatted_date, '%s') > 600 %]
...

Maybe somebody found something less fugly?

Thanks everyone!

Tosh



Tom Molesworth wrote:
> 
> The Date plugin will happily convert strings to timestamps, so something 
> like this should work:
> 
> [% USE Date %]
> Timestamp is
> [%
>    ts = Date.format('2009-06-09 09:33:39', '%s');
>    IF ts > 300 %]
> more than five minutes ago
> [% ELSE %]
> [% (ts / 60) | printf('%d') %] minutes ago
> [% END %]
> 
> cheers,
> 
> Tom
> 
> Tosh Cooey wrote:
>> I have a list of records with MySQL timestamps, the display of which 
>> works fine for my application.
>>
>> Record 2
>> 2009-06-09 09:33:39
>>
>> Record 1
>> 2009-06-02 11:33:33
>>
>> etc...
>>
>> What I would like to do is make a little notification that says:
>>
>> "Record 2" was created in the previous 5 minutes!
>>
>> That's it, just a check of the first record.
>>
>> My first instinct is to check date.now versus the second 
>> representation of the timestamp but I can't see how to make that 
>> conversion that using the Date plugin, nor does there seem to be a 
>> trivial way to accomplish this.
>>
>> I know I can use the unix_timestamp() function in the MySQL calls but 
>> if there's 1000 records it seems a waste to use it for all records 
>> when I just need the first.
>>
>> Is there a better way to compare the timestamp to check if it's 
>> withing the last five minutes, or will I need to run unix_timestamp() 
>> on all queried records?
>>
>> Thanks!
>>
>> Tosh
>>
>>   
> 
> 

-- 
McIntosh Cooey - Twelve Hundred Group LLC - http://www.1200group.com/

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

Reply via email to