source: trunk/gsdl/perllib/sorttools.pm@ 537

Last change on this file since 537 was 537, checked in by sjboddie, 25 years ago

added GPL headers

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 3.7 KB
Line 
1###########################################################################
2#
3# sorttools.pm --
4# A component of the Greenstone digital library software
5# from the New Zealand Digital Library Project at the
6# University of Waikato, New Zealand.
7#
8# Copyright (C) 1999 New Zealand Digital Library Project
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23#
24###########################################################################
25
26# various subroutines to format strings
27# for sorting
28
29package sorttools;
30
31
32# format an english string for sorting
33# i.e. convert to lowercase, remove the, a or an
34# from beginning of string etc.
35sub format_string_english {
36 my ($stringref) = @_;
37
38 $$stringref =~ tr/A-Z/a-z/;
39 $$stringref =~ s/[^a-z0-9 ]//g;
40 $$stringref =~ s/^\s*(the|a|an)\b//;
41 $$stringref =~ s/^\s+//;
42}
43
44# format an english name for sorting
45# i.e. convert to lowercase, put surname before
46# first names etc.
47sub format_string_name_english {
48 my ($stringref) = @_;
49
50 $$stringref =~ tr/A-Z/a-z/;
51 $$stringref =~ s/[^a-z0-9 ]//g;
52 $$stringref =~ s/\s+/ /g;
53 $$stringref =~ s/^\s+//;
54 my @names = split / /, $$stringref;
55 my $surname = pop @names;
56 while (scalar @names && $surname =~ /^(jnr|snr)$/i) {
57 $surname = pop @names;
58 }
59 $$stringref = $surname . " " . $$stringref;
60}
61
62# takes arguments of day, month, year and converts to
63# date of form yyyymmdd. month may be full (e.g. "January"),
64# abbreviated (e.g. "Jan"), or a number (1-12). Years like "86"
65# will be assumed to be "1986".
66sub format_date {
67 my ($day, $month, $year) = @_;
68
69 my %months = ('january' => '01', 'jan' => '01', 'february' => '02', 'feb' => '02',
70 'march' => '03', 'mar' => '03', 'april' => '04', 'apr' => '04',
71 'may' => '05', 'june' => '06', 'jun' => '06', 'july' => '07',
72 'jul' => '07', 'august' => '08', 'aug' => '08', 'september' => '09',
73 'sep' => '09', 'october' => '10', 'oct' => '10', 'november' => '11',
74 'nov' => '11', 'december' => '12', 'dec' => '12');
75
76 $month =~ tr/A-Z/a-z/;
77
78 if ($day < 1) {
79 print STDERR "sorttools::format_date WARNING day $day out of range\n";
80 $day = "01";
81 } elsif ($day > 31) {
82 print STDERR "sorttools::format_date WARNING day $day out of range\n";
83 $day = "31";
84 }
85
86 $day = "0$day" if (length($day) == 1);
87
88 if ($month =~ /^\d\d?$/) {
89 if ($month < 1) {
90 print STDERR "sorttools::format_date WARNING month $month out of range\n";
91 $month = "01";
92 } elsif ($month > 12) {
93 print STDERR "sorttools::format_date WARNING month $month out of range\n";
94 $month = "12";
95 }
96 if ($month =~ /^\d$/) {
97 $month = "0" . $month;
98 }
99 } elsif (!defined $months{$month}) {
100 print STDERR "sorttools::format_date WARNING month $month out of range\n";
101 $month = "01";
102 } else {
103 $month = $months{$month};
104 }
105
106 if ($year !~ /^\d\d\d\d$/) {
107 if ($year !~ /^\d\d$/) {
108 print STDERR "sorttools::format_date WARNING year $year assumed to be 19$year\n";
109 $year = "19" . $year;
110 } else {
111 print STDERR "sorttools::format_date WARNING year $year out of range - reset to 1900\n";
112 $year = "1900";
113 }
114 }
115
116 return "$year$month$day";
117}
118
119
1201;
Note: See TracBrowser for help on using the repository browser.