1 package FS::rate_time_interval;
2 use base qw(FS::Record);
5 use List::Util 'first';
9 FS::rate_time_interval - Object methods for rate_time_interval records
13 use FS::rate_time_interval;
15 $record = new FS::rate_time_interval \%hash;
16 $record = new FS::rate_time_interval { 'column' => 'value' };
18 $error = $record->insert;
20 $error = $new_record->replace($old_record);
22 $error = $record->delete;
24 $error = $record->check;
28 An FS::rate_time_interval object represents an interval of clock time during
29 the week, such as "Monday, 7 AM to 8 PM". FS::rate_time_interval inherits
30 from FS::Record. The following fields are currently supported:
40 Start of the interval, in seconds from midnight on Sunday.
48 A foreign key to an L<FS::rate_time> object representing the set of intervals
49 to which this belongs.
60 Creates a new example. To add the example to the database, see L<"insert">.
62 Note that this stores the hash reference, not a distinct copy of the hash it
63 points to. You can ask the object for a copy with the I<hash> method.
67 # the new method can be inherited from FS::Record, if a table method is defined
69 sub table { 'rate_time_interval'; }
73 Adds this record to the database. If there is an error, returns the error,
74 otherwise returns false.
78 # the insert method can be inherited from FS::Record
82 Delete this record from the database.
86 # the delete method can be inherited from FS::Record
88 =item replace OLD_RECORD
90 Replaces the OLD_RECORD with this one in the database. If there is an error,
91 returns the error, otherwise returns false.
95 # the replace method can be inherited from FS::Record
99 Checks all fields to make sure this is a valid interval. If there is
100 an error, returns the error, otherwise returns false. Called by the insert
109 $self->ut_numbern('intervalnum')
110 || $self->ut_number('stime')
111 || $self->ut_number('etime')
112 || $self->ut_number('ratetimenum')
114 return $error if $error;
115 # Disallow backward intervals. As a special case, an etime of 0
116 # should roll to the last second of the week.
117 $self->etime(7*24*60*60) if $self->etime == 0;
118 return "end of interval is before start" if ($self->etime < $self->stime);
120 # Detect overlap between intervals within the same rate_time.
121 # Since intervals are added one at a time, we only need to look
122 # for an existing interval that contains one of the endpoints of
123 # this one or that is completely inside this one.
124 my $overlap = $self->rate_time->contains($self->stime + 1) ||
125 $self->rate_time->contains($self->etime - 1) ||
126 first { $self->stime <= $_->stime && $self->etime >= $_->etime }
127 ( $self->rate_time->intervals );
128 return "interval overlap: (".join('-',$self->description).') with ('.
129 join('-',$overlap->description).')' if $overlap;
136 Returns the L<FS::rate_time> comprising this interval.
140 Returns two strings containing stime and etime, formatted
141 "Day HH:MM AM/PM". Example: "Mon 5:00 AM". Seconds are
142 not displayed, so be careful.
146 my @days = qw(Sun Mon Tue Wed Thu Fri Sat);
151 sprintf('%s %02d:%02d %s',
152 $days[int($_/86400) % 7],
153 (int($_/3600) % 12 || 12),
155 (($_/3600) % 24 < 12) ? 'AM' : 'PM' )
156 } ( $self->stime, $self->etime );
165 L<FS::rate_time>, L<FS::Record>, schema.html from the base documentation.