Patch by Eira to support times and weight conversions. Reviewed and changed a bit by me.

This commit is contained in:
ian%hixie.ch 2004-02-01 18:55:47 +00:00
Родитель 6bb7fc47be
Коммит dc9df6b4e2
1 изменённых файлов: 225 добавлений и 4 удалений

Просмотреть файл

@ -10,18 +10,23 @@ use vars qw(@ISA);
1;
# XXX support the suffixes "to <n> sf" or "to <n> dp"
# XXX support speed, volume, twips
# XXX support light year, parsec, furlong; fm, pm, µm, Mm, Gm, Tm, Pm
# XXX support 1x10^1 notation as well as the already-supported 1e1 notation
sub Help {
my $self = shift;
my ($event) = @_;
return {
'' => 'A generic converter. Currently supports converting between positive integers in binary, octal, decimal and hexidecimal forms, converting temperatures, and converting lengths.',
'' => 'A generic converter. Currently supports converting between positive integers in binary, octal, decimal and hexidecimal forms, and converting temperatures, lengths, times and masses.',
'syntax' => 'To convert a number, simply give the number with units or appropriate prefixes, for example to convert from hexadecimal: \'0x2F\'',
'integers' => 'Decimal: Simply give the number. Hexadecimal: Prefix with 0x. Octal: Prefix with 0. Binary: Prefix with 0b.',
'temperature' => 'Kelvin: Suffix with K. Celsius: Suffix with C. Fahrenheit: Suffix with F.',
'length' => 'Imperial: in, ft, yd, mi. Metric: A, nm, mm, cm, m, km', # XXX should support light year, parsec, furlong; f, p, n, µ, m, -, k, M, G, T, P
# XXX should support weight, speed, volume, twips
};
'length' => 'Imperial: in, ft, yd, mi. Metric: A, nm, mm, cm, m, km.', # XXX should also support light year, parsec, furlong; fm, pm, µm, Mm, Gm, Tm, Pm
'time' => 'ISO time units: year, month, week, day, hour, minute, second. Exotic time units: millifortnight.',
'mass' => 'Imperial: lbs, oz, stone. Metric: kg, g.',
# XXX should support speed, volume, twips
};
}
@ -70,7 +75,37 @@ sub Told {
$self->convertMeters($event, $1);
} elsif ($message =~ m/^\s*(-?(?:[0-9]+\.?|[0-9]+\.[0-9]+|\.[0-9]+)(?:e-?[0-9]+)?)\s*(?:kms?|kilometers?|kilometres?|klic?ks?)\s*\??\s*$/osi) {
$self->convertKilometers($event, $1);
# times
} elsif ($message =~ m/^\s*(-?(?:[0-9]+\.?|[0-9]+\.[0-9]+|\.[0-9]+)(?:e-?[0-9]+)?)\s*(?:years|year|yr)\s*\??\s*$/osi) {
$self->convertYears($event, $1);
} elsif ($message =~ m/^\s*(-?(?:[0-9]+\.?|[0-9]+\.[0-9]+|\.[0-9]+)(?:e-?[0-9]+)?)\s*(?:months|month|mo)\s*\??\s*$/osi) {
$self->convertMonths($event, $1);
} elsif ($message =~ m/^\s*(-?(?:[0-9]+\.?|[0-9]+\.[0-9]+|\.[0-9]+)(?:e-?[0-9]+)?)\s*(?:weeks|week|wk)\s*\??\s*$/osi) {
$self->convertWeeks($event, $1);
} elsif ($message =~ m/^\s*(-?(?:[0-9]+\.?|[0-9]+\.[0-9]+|\.[0-9]+)(?:e-?[0-9]+)?)\s*(?:fortnights|fortnight|mf)\s*\??\s*$/osi) {
$self->convertMillifortnights($event, $1);
} elsif ($message =~ m/^\s*(-?(?:[0-9]+\.?|[0-9]+\.[0-9]+|\.[0-9]+)(?:e-?[0-9]+)?)\s*(?:days|day|d)\s*\??\s*$/osi) {
$self->convertDays($event, $1);
} elsif ($message =~ m/^\s*(-?(?:[0-9]+\.?|[0-9]+\.[0-9]+|\.[0-9]+)(?:e-?[0-9]+)?)\s*(?:hours|hour|hr|h)\s*\??\s*$/osi) {
$self->convertHours($event, $1);
} elsif ($message =~ m/^\s*(-?(?:[0-9]+\.?|[0-9]+\.[0-9]+|\.[0-9]+)(?:e-?[0-9]+)?)\s*(?:minutes|minute|min)\s*\??\s*$/osi) {
$self->convertMinutes($event, $1);
} elsif ($message =~ m/^\s*(-?(?:[0-9]+\.?|[0-9]+\.[0-9]+|\.[0-9]+)(?:e-?[0-9]+)?)\s*(?:seconds|second|sec|s)\s*\??\s*$/osi) {
$self->convertSeconds($event, $1);
# masses
} elsif ($message =~ m/^\s*(-?(?:[0-9]+\.?|[0-9]+\.[0-9]+|\.[0-9]+)(?:e-?[0-9]+)?)\s*(?:grams|gram|g)\s*\??\s*$/osi) {
$self->convertGrams($event, $1);
} elsif ($message =~ m/^\s*(-?(?:[0-9]+\.?|[0-9]+\.[0-9]+|\.[0-9]+)(?:e-?[0-9]+)?)\s*(?:kilograms|kilogram|kilos|kilo|kg)\s*\??\s*$/osi) {
$self->convertKilograms($event, $1);
} elsif ($message =~ m/^\s*(-?(?:[0-9]+\.?|[0-9]+\.[0-9]+|\.[0-9]+)(?:e-?[0-9]+)?)\s*(?:pounds|pound|lbs)\s*\??\s*$/osi) {
$self->convertPounds($event, $1);
} elsif ($message =~ m/^\s*(-?(?:[0-9]+\.?|[0-9]+\.[0-9]+|\.[0-9]+)(?:e-?[0-9]+)?)\s*(?:ounces|ounce|oz)\s*\??\s*$/osi) {
$self->convertOunces($event, $1);
} elsif ($message =~ m/^\s*(-?(?:[0-9]+\.?|[0-9]+\.[0-9]+|\.[0-9]+)(?:e-?[0-9]+)?)\s*(?:stones|stone)\s*\??\s*$/osi) {
$self->convertStones($event, $1);
# oh well
} else {
return $self->SUPER::Told(@_);
@ -379,6 +414,192 @@ sub convertKilometers {
}
# Time
sub convertYears {
my $self = shift;
my($event, $input) = @_;
my $accurateSeconds = $input * 60.0 * 60.0 * 24.0 * 365.25;
my $years = sigfig(3, $input);
my $months = sigfig(3, $accurateSeconds / (60.0 * 60.0 * 24.0 * (365.25 / 12)));
my $weeks = sigfig(3, $accurateSeconds / (60.0 * 60.0 * 24.0 * (365.25 / (365.25 / 7.0))));
my $millifortnights = sigfig(3, $accurateSeconds / (60.0 * 60.0 * 24.0 * (365.25 / (365.25 / 7.0)) * 2.0 / 1000.0));
my $days = sigfig(3, $accurateSeconds / (60.0 * 60.0 * 24.0));
my $hours = sigfig(3, $accurateSeconds / (60.0 * 60.0));
my $minutes = sigfig(3, $accurateSeconds / 60.0);
my $seconds = sigfig(3, $accurateSeconds);
$self->say($event, "$event->{'from'}: ${years}yr = ${months}mo, ${weeks}wk, ${days}d, ${hours}hr, ${minutes}min, ${seconds}s, ${millifortnights}mf");
}
sub convertMonths {
my $self = shift;
my($event, $input) = @_;
my $accurateSeconds = $input * 60.0 * 60.0 * 24.0 * (365.25 / 12);
my $years = sigfig(3, $accurateSeconds / (60.0 * 60.0 * 24.0 * 365.25));
my $months = sigfig(3, $input);
my $weeks = sigfig(3, $accurateSeconds / (60.0 * 60.0 * 24.0 * (365.25 / (365.25 / 7.0))));
my $millifortnights = sigfig(3, $accurateSeconds / (60.0 * 60.0 * 24.0 * (365.25 / (365.25 / 7.0)) * 2.0 / 1000.0));
my $days = sigfig(3, $accurateSeconds / (60.0 * 60.0 * 24.0));
my $hours = sigfig(3, $accurateSeconds / (60.0 * 60.0));
my $minutes = sigfig(3, $accurateSeconds / 60.0);
my $seconds = sigfig(3, $accurateSeconds);
$self->say($event, "$event->{'from'}: ${months}mo = ${years}yr, ${weeks}wk, ${days}d, ${hours}hr, ${minutes}min, ${seconds}s, ${millifortnights}mf");
}
sub convertWeeks {
my $self = shift;
my($event, $input) = @_;
my $accurateSeconds = $input * 60.0 * 60.0 * 24.0 * 7.0;
my $years = sigfig(3, $accurateSeconds / (60.0 * 60.0 * 24.0 * 365.25));
my $months = sigfig(3, $accurateSeconds / (60.0 * 60.0 * 24.0 * (365.25 / 12)));
my $weeks = sigfig(3, $input);
my $millifortnights = sigfig(3, $accurateSeconds / (60.0 * 60.0 * 24.0 * (365.25 / (365.25 / 7.0)) * 2.0 / 1000.0));
my $days = sigfig(3, $accurateSeconds / (60.0 * 60.0 * 24.0));
my $hours = sigfig(3, $accurateSeconds / (60.0 * 60.0));
my $minutes = sigfig(3, $accurateSeconds / 60.0);
my $seconds = sigfig(3, $accurateSeconds);
$self->say($event, "$event->{'from'}: ${weeks}wk = ${years}yr, ${months}mo, ${days}d, ${hours}hr, ${minutes}min, ${seconds}s, ${millifortnights}mf");
}
sub convertMillifortnights {
my $self = shift;
my($event, $input) = @_;
my $accurateSeconds = $input * 60.0 * 60.0 * 24.0 * 7.0 * 2.0 / 1000.0;
my $years = sigfig(3, $accurateSeconds / (60.0 * 60.0 * 24.0 * 365.25));
my $months = sigfig(3, $accurateSeconds / (60.0 * 60.0 * 24.0 * (365.25 / 12)));
my $weeks = sigfig(3, $accurateSeconds / (60.0 * 60.0 * 24.0 * (365.25 / (365.25 / 7.0))));
my $millifortnights = sigfig(3, $accurateSeconds / (60.0 * 60.0 * 24.0 * (365.25 / (365.25 / 7.0)) * 2.0 / 1000.0));
my $days = sigfig(3, $accurateSeconds / (60.0 * 60.0 * 24.0));
my $hours = sigfig(3, $accurateSeconds / (60.0 * 60.0));
my $minutes = sigfig(3, $accurateSeconds / 60.0);
my $seconds = sigfig(3, $accurateSeconds);
$self->say($event, "$event->{'from'}: ${millifortnights}mf = ${years}yr, ${months}mo, ${weeks}wk, ${days}d, ${hours}hr, ${minutes}min, ${seconds}s");
}
sub convertDays {
my $self = shift;
my($event, $input) = @_;
my $accurateSeconds = $input * 60.0 * 60.0 * 24.0;
my $years = sigfig(3, $accurateSeconds / (60.0 * 60.0 * 24.0 * 365.25));
my $months = sigfig(3, $accurateSeconds / (60.0 * 60.0 * 24.0 * (365.25 / 12)));
my $weeks = sigfig(3, $accurateSeconds / (60.0 * 60.0 * 24.0 * (365.25 / (365.25 / 7.0))));
my $millifortnights = sigfig(3, $accurateSeconds / (60.0 * 60.0 * 24.0 * (365.25 / (365.25 / 7.0)) * 2.0 / 1000.0));
my $days = sigfig(3, $accurateSeconds / (60.0 * 60.0 * 24.0));
my $hours = sigfig(3, $accurateSeconds / (60.0 * 60.0));
my $minutes = sigfig(3, $accurateSeconds / 60.0);
my $seconds = sigfig(3, $accurateSeconds);
$self->say($event, "$event->{'from'}: ${days}d = ${years}yr, ${months}mo, ${weeks}wk, ${hours}hr, ${minutes}min, ${seconds}s, ${millifortnights}mf");
}
sub convertHours {
my $self = shift;
my($event, $input) = @_;
my $accurateSeconds = $input * 60.0 * 60.0;
my $years = sigfig(3, $accurateSeconds / (60.0 * 60.0 * 24.0 * 365.25));
my $months = sigfig(3, $accurateSeconds / (60.0 * 60.0 * 24.0 * (365.25 / 12)));
my $weeks = sigfig(3, $accurateSeconds / (60.0 * 60.0 * 24.0 * (365.25 / (365.25 / 7.0))));
my $millifortnights = sigfig(3, $accurateSeconds / (60.0 * 60.0 * 24.0 * (365.25 / (365.25 / 7.0)) * 2.0 / 1000.0));
my $days = sigfig(3, $accurateSeconds / (60.0 * 60.0 * 24.0));
my $hours = sigfig(3, $accurateSeconds / (60.0 * 60.0));
my $minutes = sigfig(3, $accurateSeconds / 60.0);
my $seconds = sigfig(3, $accurateSeconds);
$self->say($event, "$event->{'from'}: ${hours}hr = ${years}yr, ${months}mo, ${weeks}wk, ${days}d, ${minutes}min, ${seconds}s, ${millifortnights}mf");
}
sub convertMinutes {
my $self = shift;
my($event, $input) = @_;
my $accurateSeconds = $input * 60.0;
my $years = sigfig(3, $accurateSeconds / (60.0 * 60.0 * 24.0 * 365.25));
my $months = sigfig(3, $accurateSeconds / (60.0 * 60.0 * 24.0 * (365.25 / 12)));
my $weeks = sigfig(3, $accurateSeconds / (60.0 * 60.0 * 24.0 * (365.25 / (365.25 / 7.0))));
my $millifortnights = sigfig(3, $accurateSeconds / (60.0 * 60.0 * 24.0 * (365.25 / (365.25 / 7.0)) * 2.0 / 1000.0));
my $days = sigfig(3, $accurateSeconds / (60.0 * 60.0 * 24.0));
my $hours = sigfig(3, $accurateSeconds / (60.0 * 60.0));
my $minutes = sigfig(3, $accurateSeconds / 60.0);
my $seconds = sigfig(3, $accurateSeconds);
$self->say($event, "$event->{'from'}: ${minutes}min = ${years}yr, ${months}mo, ${weeks}wk, ${days}d, ${hours}hr, ${seconds}s, ${millifortnights}mf");
}
sub convertSeconds {
my $self = shift;
my($event, $input) = @_;
my $accurateSeconds = $input;
my $years = sigfig(3, $accurateSeconds / (60.0 * 60.0 * 24.0 * 365.25));
my $months = sigfig(3, $accurateSeconds / (60.0 * 60.0 * 24.0 * (365.25 / 12)));
my $weeks = sigfig(3, $accurateSeconds / (60.0 * 60.0 * 24.0 * (365.25 / (365.25 / 7.0))));
my $millifortnights = sigfig(3, $accurateSeconds / (60.0 * 60.0 * 24.0 * (365.25 / (365.25 / 7.0)) * 2.0 / 1000.0));
my $days = sigfig(3, $accurateSeconds / (60.0 * 60.0 * 24.0));
my $hours = sigfig(3, $accurateSeconds / (60.0 * 60.0));
my $minutes = sigfig(3, $accurateSeconds / 60.0);
my $seconds = sigfig(3, $accurateSeconds);
$self->say($event, "$event->{'from'}: ${seconds}s = ${years}yr, ${months}mo, ${weeks}wk, ${days}d, ${hours}hr, ${minutes}min, ${millifortnights}mf");
}
# Mass
sub convertGrams {
my $self = shift;
my($event, $input) = @_;
my $accurateGrams = $input;
my $grams = sigfig(3, $accurateGrams);
my $kgs = sigfig(3, $accurateGrams / 1000.0);
my $ounces = sigfig(3, $accurateGrams * 0.03527);
my $pounds = sigfig(3, $accurateGrams * 0.002205);
my $stones = sigfig(3, $accurateGrams * 0.00016);
$self->say($event, "$event->{'from'}: ${grams}g = ${kgs}kg, ${ounces}oz, ${pounds}lbs, ${stones}stone");
}
sub convertKilograms {
my $self = shift;
my($event, $input) = @_;
my $accurateGrams = $input * 1000.0;
my $grams = sigfig(3, $accurateGrams);
my $kgs = sigfig(3, $input);
my $ounces = sigfig(3, $accurateGrams * 0.03527);
my $pounds = sigfig(3, $accurateGrams * 0.002205);
my $stones = sigfig(3, $accurateGrams * 0.00016);
$self->say($event, "$event->{'from'}: ${kgs}kg = ${grams}g, ${ounces}oz, ${pounds}lbs, ${stones}stone");
}
sub convertPounds {
my $self = shift;
my($event, $input) = @_;
my $accurateGrams = $input * 453.6;
my $grams = sigfig(3, $accurateGrams);
my $kgs = sigfig(3, $accurateGrams / 1000.0);
my $ounces = sigfig(3, $accurateGrams * 0.03527);
my $pounds = sigfig(3, $input);
my $stones = sigfig(3, $accurateGrams * 0.00016);
$self->say($event, "$event->{'from'}: ${pounds}lbs = ${grams}g, ${kgs}kg, ${ounces}oz, ${stones}stone");
}
sub convertOunces {
my $self = shift;
my($event, $input) = @_;
my $accurateGrams = $input * 28.35;
my $grams = sigfig(3, $accurateGrams);
my $kgs = sigfig(3, $accurateGrams / 1000.0);
my $ounces = sigfig(3, $input);
my $pounds = sigfig(3, $accurateGrams * 0.002205);
my $stones = sigfig(3, $accurateGrams * 0.00016);
$self->say($event, "$event->{'from'}: ${ounces}oz = ${grams}g, ${kgs}kg, ${pounds}lbs, ${stones}stone");
}
sub convertStones {
my $self = shift;
my($event, $input) = @_;
my $accurateGrams = $input * 6350.3;
my $grams = sigfig(3, $accurateGrams);
my $kgs = sigfig(3, $accurateGrams / 1000.0);
my $ounces = sigfig(3, $accurateGrams * 0.03527);
my $pounds = sigfig(3, $accurateGrams * 0.002205);
my $stones = sigfig(3, $accurateGrams * 0.00016);
$self->say($event, "$event->{'from'}: ${stones}stone = ${grams}g, ${kgs}kg, ${ounces}oz, ${pounds}lbs");
}
# Utility Functions
sub round($$) {