On Tue, Jul 13, 2004 at 11:32:02AM -0300, Bruno Negrão wrote:
> Hi all,
> 
> I've made a new module, I'm thinking to call it Time::Seconds::GroupedBy. It
> is designed to convert an amount of seconds in other time units. I'm calling
> time units SECONDS, MINUTES, HOURS, DAYS, MONTHS, and YEARS . The user
> defines which will be the time unit to group the amount of seconds.

> Any suggestions? Opinions? questions? etc....

Seems related to, and an extension of, Time::Duration.
Perhaps you could extend that module rather than create a new one.

Tim.

> Using my module, the following test code:
> 
> use strict;
> my $seconds = 31556931;
> foreach ('MINS', 'HOURS', 'DAYS', 'MONTHS', 'YEARS') {
>             my $s = new GroupedBy ( $_ );
>             my ($secs, $mins, $hours, $days, $months, $years) =
>                         $s->calculate($seconds);
>             print "Grouping by $_, $seconds seconds are:\n ",
>                         "  $years years, $months months, $days days, $hours hours,",
>                         " $mins mins, $secs seconds.\n\n";
> }
> 
> Will bring this output:
> 
> Grouping by MINS, 31556931 seconds are:
>    0 years, 0 months, 0 days, 0 hours, 525948 mins, 51 seconds.
> 
> Grouping by HOURS, 31556931 seconds are:
>    0 years, 0 months, 0 days, 8765 hours, 48 mins, 51 seconds.
> 
> Grouping by DAYS, 31556931 seconds are:
>    0 years, 0 months, 365 days, 5 hours, 48 mins, 51 seconds.
> 
> Grouping by MONTHS, 31556931 seconds are:
>    0 years, 12 months, 5 days, 5 hours, 48 mins, 51 seconds.
> 
> Grouping by YEARS, 31556931 seconds are:
>    1 years, 0 months, 0 days, 0 hours, 0 mins, 1 seconds.
> 
> The draft code of the module is bellow,
> 
> package GroupedBy;
> sub new {
>  my $class = shift;
>  my $self = {};
>  bless ( $self, $class );
>  my $GroupedBy = shift;
>  # Amount of Seconds input by the user (set by calculate() method)
>  $self->{SECONDS};
>  # Initializing data about years, months, days, hours and minutes;
>  $self->{SECS}  = { result => 0 };
>  # A minute is 60 seconds
>  $self->{MINS}  = { inSecs => 60, opId => 1, took => 0, result => 0 };
>  # An hour is 60 * 60 seconds
>  $self->{HOURS} = { inSecs => 3600, opId => 2, took => 0, result => 0 };
>  # A day is 60 * 60 * 24 seconds
>  $self->{DAYS}  = { inSecs => 86400, opId => 3, took => 0, result => 0 };
>  # A month is 60 * 60 * 24 * 30 seconds
>  $self->{MONTHS} = { inSecs => 2592000, opId => 4, took => 0, result => 0 };
>  # Year is 60 * 60 * 24 * 365.24225 = 31556930.4 seconds (we'll ignore 0.4 secs)
>  $self->{YEARS}  = { inSecs => 31556930, opId => 5, took => 0, result => 0 };
> 
>  # OPID - operation ID, is a number indentifying the kind of grouping that
>  # was chosen by the user. Each grouping requires a diferent operation to
>  # calculate the resulting values.
>  $self->{OPID} = $self->{$GroupedBy}->{opId};
>  die "Unvalid value. Valid values are 'MINS', 'HOURS', 'DAYS', 'MONTHS', 'YEARS'"
>   unless defined $self->{OPID};
> 
>  return $self;
> }
> 
> sub calculate { # returns: Array: the results of the calculus
>  my $self = shift;
>  $self->{SECONDS} = shift;
>  $self->_clean();
>  my @result = (  $self->_years(), $self->_months(), $self->_days(),
>      $self->_hours(), $self->_mins(), $self->_secs() );
>  return reverse @result;
> }
> 
> sub groupedBy { # returns: String: the current GROUPED_BY type
>  my $self = shift;
>  my $GroupedBy = shift;
>  $self->{OPID} = $self->($GroupedBy)->{opId};
>  die "Unvalid value. Valid values are 'MINS', 'HOURS', 'DAYS', 'MONTHS', 'YEARS'"
>   unless defined $self->{OPID};
>  return $GroupedBy;
> }
> 
> # Cleans(set to 0)the 'took' and 'result' counters for all groups
> sub _clean { # returns: nothing
>  my $self = shift;
>  foreach ( qw ( SECS MINS HOURS DAYS MONTHS YEARS ) ) {
>   $self->{$_}->{took} = 0;
>   $self->{$_}->{result} = 0;
>  }
> }
> 
> sub _secs { # returns: Integer: the result
>  my $self = shift;
>  my $name = "SECS";
>  $self->{$name}->{result} = $self->{SECONDS} - $self->{YEARS}->{took}
>    - $self->{MONTHS}->{took} - $self->{DAYS}->{took} - $self->{HOURS}->{took}
>    - $self->{MINS}->{took};
>  return $self->{$name}->{result};
> }
> 
> sub _mins { # returns: Integer: the result
>  my $self = shift;
>  my $name = "MINS";
>  if ( $self->{OPID} >= 1 ) {
>   my $secsLeft = $self->{SECONDS} - $self->{YEARS}->{took}
>    - $self->{MONTHS}->{took} - $self->{DAYS}->{took} - $self->{HOURS}->{took};
>   $self->{$name}->{result} = int($secsLeft / $self->{$name}->{inSecs});
>   if ( $self->{$name}->{result} != 0 ) {
>    $self->{$name}->{took} = $self->{$name}->{result} * $self->{$name}->{inSecs};
>   }
>  }
>  return $self->{$name}->{result};
> }
> 
> sub _hours { # returns: Integer: the result
>  my $self = shift;
>  my $name = "HOURS";
>  if ( $self->{OPID} >= 2 ) {
>   my $secsLeft = $self->{SECONDS} - $self->{YEARS}->{took}
>    - $self->{MONTHS}->{took} - $self->{DAYS}->{took};
>   $self->{$name}->{result} = int($secsLeft / $self->{$name}->{inSecs});
>   if ( $self->{$name}->{result} != 0 ) {
>    $self->{$name}->{took} = $self->{$name}->{result} * $self->{$name}->{inSecs};
>   }
> 
>  }
>  return $self->{$name}->{result};
> }
> 
> sub _days { # returns: Integer: the result
>  my $self = shift;
>  my $name = "DAYS";
>  if ( $self->{OPID} >= 3 ) {
>   my $secsLeft = $self->{SECONDS} - $self->{YEARS}->{took} - $self->{MONTHS}->{took};
>   $self->{$name}->{result} = int($secsLeft / $self->{$name}->{inSecs});
>   if ( $self->{$name}->{result} != 0 ) {
>    $self->{$name}->{took} = $self->{$name}->{result} * $self->{$name}->{inSecs};
>   }
>  }
>  return $self->{$name}->{result};
> }
> 
> sub _months { # returns: Integer: the result
>  my $self = shift;
>  my $name = "MONTHS";
>  if ( $self->{OPID} >= 4 ) {
>   my $secsLeft = $self->{SECONDS} - $self->{YEARS}->{took};
>   $self->{$name}->{result} = int($secsLeft / $self->{$name}->{inSecs});
>   if ( $self->{$name}->{result} != 0 ) {
>    $self->{$name}->{took} = $self->{$name}->{result} * $self->{$name}->{inSecs};
>   }
>  }
>  return $self->{$name}->{result};
> }
> 
> sub _years { # returns: Integer: the result
>  my $self = shift;
>  my $name = "YEARS";
>  if ( $self->{OPID} == 5 ) {
>   $self->{$name}->{result} = int($self->{SECONDS} / $self->{$name}->{inSecs});
>   if ( $self->{$name}->{result} != 0 ) {
>    $self->{$name}->{took} = $self->{$name}->{result} * $self->{$name}->{inSecs};
>   }
>  }
>  return $self->{$name}->{result};
> }
> 
> Any suggestions? Opinions? questions? etc....
> 
> A hug for everybody,
> Bruno Negrão
> 
> 
> 
> 
> 

Reply via email to