Skip to main content.
home | support | download

Back to List Archive

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

From: Ahmad, Zeeshan (FMC) <Zeeshan.Ahmad(at)not-real.fmc.sa.gov.au>
Date: Thu Jan 22 2004 - 01:59:15 GMT
Thanks. I'll try that.

Another issue.

I am searching via API. For some documents, title is returned null and thus
swishdocpath is displayed - but when running via binary title is never null
(swish returns filename as title). Is this by design or I am doing something
wrong?

__________________
 
Zeeshan Ahmad
FMC Computing Services
Bedford Park SA
 
Ph: 8204 6178
 

-----Original Message-----
From: swish-e@sunsite.berkeley.edu [mailto:swish-e@sunsite.berkeley.edu] On
Behalf Of Bill Moseley
Sent: Thursday, 22 January 2004 4:31 AM
To: Multiple recipients of list
Subject: [SWISH-E] Re: [Ignor Abuse] RE: Re: Date Format

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 Thu Jan 22 01:59:51 2004