Skip to main content.
home | support | download

Back to List Archive

Re: [Ignor Abuse] RE: Re: Date Format

From: Bill Moseley <moseley(at)not-real.hank.org>
Date: Wed Jan 21 2004 - 18:00:48 GMT
On Wed, Jan 21, 2004 at 06:41:49PM +1030, Ahmad, Zeeshan (FMC) wrote:
> When using swish binary via swish script I used this method to format dates.
> 
>     my @props = map {
>     $_ eq 'swishlastmodified' ? "$_ fmt='%d %B %Y'" : $_
>     } @properties;
> 
> How can I change the date format when using Swish API instead (use_library
> => 1)?

You can't do it that way.

Swish::API provides a method to get at properties as a string:

   my $value = $result->ResultPropertyStr( $prop );

so it's good as a general purpose drop-in in swish.cgi.  But it doesn't 
provide the fmt options that -x does.

The better way to get properties in SWISH::API is with:

   my $prop = $result->Property( $prop );

but that's now in the native format.  So if you print that directly then 
dates will look like: 1074706191

The formatting of that date then is a *display* issue.  Look at 
search.cgi in the distribution for a much simpler search script that 
uses Template-Toolkit for creating the output.  It does this:

  Date: [% date.format( item.Property('swishlastmodified')) %]

That uses the default date format.  So if you want to change the look of 
the date the correct place to change that is in the *template* that 
generates the output:

  Date: [% date.format( item.Property('swishlastmodified'),'%d %B %Y') %]

(for those that have Template-Toolkit installed see perldoc Template::Plugin::Date)

Back to your specific question:  Did you look at the swish.cgi source code?

I'm not sure what I'd do.  I would probably do this in swish.cgi (but 
this is moving the formatting into the template module -- see below for 
a second way)

   my %isdate = (
	swishlastmodified => 1,
        otherdate         => 1,
   );
   [...]

        for my $prop ( @props ) {
            # Note, we use ResultPropertyStr instead since this is a general purpose
            # script (it converts dates to a string, for example).
            # $result->Property is a faster method and does not convert dates and n$
            #my $value = $result->Property( $prop );

            ## <<< new stuff here >>>
            my $value = $isdate{$prop}
                  ? $result->Property( $prop ) 
                  : $result->ResultPropertyStr( $prop );
            next unless $value;

            $props{$prop} = $value;
        }

Then in the template module (which ever you are using)

   Use Date::Format;
   my $mod_date_string = $prop{swishlastmodified}
       ? time2str('%d %B %Y', $prop{swishlastmodified}) 
       : 'unknown time';


But, that's more work.  The easy and wrong way is to do it in swish.cgi

            use  Date::Format;

            ## <<< new stuff here >>>
            my $value = $isdate{$prop} && $result->Property( $prop )
                  ? time2str('%d %B %Y', $result->Property( $prop ) )
                  : $result->ResultPropertyStr( $prop );
            next unless $value;

One minor point:  that last "next unless $value" will also see zero as 
false, which may or not be what you want.  It probably should be:

            next unless defined $value;

-- 
Bill Moseley
moseley@hank.org
Received on Wed Jan 21 18:01:35 2004