Refactor exact duration intervals into a separate method.
This commit is contained in:
parent
56773eefc1
commit
3c4ae9aa82
3 changed files with 57 additions and 6 deletions
|
|
@ -126,7 +126,7 @@ Returns the interval formatted as quantity and units.
|
|||
|
||||
sub getValueAsHtml {
|
||||
my $self = shift;
|
||||
return join ' ', $self->session->datetime->secondsToInterval($self->getOriginalValue);
|
||||
return join ' ', $self->session->datetime->secondsToExactInterval($self->getOriginalValue);
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
|
@ -177,7 +177,7 @@ sub toHtml {
|
|||
years => $i18n->get(703),
|
||||
);
|
||||
my %reverseUnits = reverse %units;
|
||||
my ($interval, $units) = $self->session->datetime->secondsToInterval($self->getOriginalValue);
|
||||
my ($interval, $units) = $self->session->datetime->secondsToExactInterval($self->getOriginalValue);
|
||||
# not sure why, but these things need to be defined like this or
|
||||
# they fail under some circumstnaces
|
||||
my $cmd = "WebGUI::Form::Integer";
|
||||
|
|
@ -209,7 +209,7 @@ Returns the field as hidden controls rather than displayable controls.
|
|||
|
||||
sub toHtmlAsHidden {
|
||||
my $self = shift;
|
||||
my ($interval, $units) = $self->session->datetime->secondsToInterval($self->getOriginalValue);
|
||||
my ($interval, $units) = $self->session->datetime->secondsToExactInterval($self->getOriginalValue);
|
||||
return WebGUI::Form::Hidden->new($self->session,
|
||||
name=>$self->get("name").'_interval',
|
||||
value=>$interval
|
||||
|
|
|
|||
|
|
@ -812,7 +812,8 @@ sub new {
|
|||
|
||||
=head2 secondsToInterval ( seconds )
|
||||
|
||||
Returns an interval and internationalized units derived the number of seconds.
|
||||
Returns an interval and internationalized units derived the number
|
||||
of seconds, rounding to the closest unit smaller than the interval.
|
||||
|
||||
=head3 seconds
|
||||
|
||||
|
|
@ -842,6 +843,38 @@ sub secondsToInterval {
|
|||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 secondsToExactInterval ( seconds )
|
||||
|
||||
Returns an interval and internationalized units derived the number of seconds.
|
||||
|
||||
=head3 seconds
|
||||
|
||||
The number of seconds in the interval.
|
||||
|
||||
=cut
|
||||
|
||||
sub secondsToExactInterval {
|
||||
my $self = shift;
|
||||
my $seconds = shift;
|
||||
my $i18n = WebGUI::International->new($self->session, 'WebGUI');
|
||||
my %units = (
|
||||
31536000 => "703", # years
|
||||
2592000 => "702", # months
|
||||
604800 => "701", # weeks
|
||||
86400 => "700", # days
|
||||
3600 => "706", # hours
|
||||
60 => "705", # minutes
|
||||
);
|
||||
for my $unit (sort { $b <=> $a } keys %units) {
|
||||
if ($seconds % $unit == 0) {
|
||||
return ($seconds / $unit, $i18n->get($units{$unit}));
|
||||
}
|
||||
}
|
||||
return ($seconds, $i18n->get("704")); # seconds
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 secondsToTime ( seconds )
|
||||
|
||||
Returns a time string of the format HH::MM::SS on a 24 hour clock. See also timeToSeconds().
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue