How to Divide a Given Time Interval into Equal Intervals

How do you divide a time period into equal intervals and find the current one?

If you only want to reduce the math here, you can use remainder instead of a division and multiplication.

long millisSinceIntervalStart = sinceOrigin % millisPerInterval;
Instant startNext = now.minusMillis(millisSinceIntervalStart);

Here you don't have to calculate the number of intervals passed since origin. Just get the time passed since intervalStart and subtract it from current time.

Also, your startNext seems to indicate the start of current interval, not the next interval. Correct?

how to divide an interval into equal parts

Here is a python implementation which is quite easy to understand.

def divide(number, parts):
'''number is the last number of the range and parts is no. of intervals you
want to make'''
chunksize = number//parts # size of each interval
chunkstart = 1 # start of interval
chunkend = chunkstart + chunksize -1 # end of that interval
while chunkstart < number: # don't go beyond the range
if chunkend > number: # interval end is beyond the range
print chunkstart, number
break # we are beyond the range now
print chunkstart, chunkend
chunkstart += chunksize # take me to beginning of next interval
chunkend += chunksize # also tell me where to end that

Sample Input and Ouputs

divide(100, 5)
1 20
21 40
41 60
61 80
81 100

divide(102, 5)
1 20
21 40
41 60
61 80
81 100
101 102

Dividing Time into equal intervals

After hour of RND I found solution.

Model class for ExceptDate

public class ExceptDate{

ExceptDate(String from,String to){
this.from=from;
this.to=to;
}

private String from;

private String to;

public void setFrom(String from){
this.from = from;
}

public String getFrom(){
return from;
}

public void setTo(String to){
this.to = to;
}

public String getTo(){
return to;
}

}

Input Variables :

String strFromTime = "10:00";
String strToTime = "14:00";
ArrayList<ExceptDate> exceptTime = ArrayList < String > ()
exceptTime.add(new ExceptDate("11:10", "11:45"));
exceptTime.add(new ExceptDate("13:10", "13:30"));

And the final method where magic happens.

public ArrayList<String> getTimeSlot(int slotMinute, strFromTime, strToTime, exceptTime) {
int fromHour, fromMinute, toHour, toMinute;

fromHour = Integer.parseInt(strFromTime.split(":")[0]);
fromMinute = Integer.parseInt(strFromTime.split(":")[1]);

toHour = Integer.parseInt(strToTime.split(":")[0]);
toMinute = Integer.parseInt(strToTime.split(":")[1]);

long slot = slotMinute * 60 * 1000;
Calendar calendar2 = Calendar.getInstance();
calendar2.set(Calendar.HOUR_OF_DAY, fromHour);
calendar2.set(Calendar.MINUTE, fromMinute);

long currentTime = calendar2.getTimeInMillis();
Calendar calendar1 = Calendar.getInstance();
calendar1.set(Calendar.HOUR_OF_DAY, toHour);
calendar1.set(Calendar.MINUTE, toMinute);

long endTime = calendar1.getTimeInMillis();

ArrayList<String> timeSlot = new ArrayList<String>();

while (currentTime < endTime) {
if (availableDatesItem.getExcept() == null) {
timeSlot.add(sdfTime.format(new Date(currentTime)));
currentTime = currentTime + slot;
} else {
for (int i = 0; i < exceptTime.size(); i++) {

int exceptFromHour, exceptFromMinute, exceptToHour, exceptToMinute;

exceptFromHour = Integer.parseInt(exceptTime.get(i).getFrom().split(":")[0]);
exceptFromMinute = Integer.parseInt(exceptTime.get(i).getFrom().split(":")[1]);

exceptToHour = Integer.parseInt(exceptTime.get(i).getTo().split(":")[0]);
exceptToMinute = Integer.parseInt(exceptTime.get(i).getTo().split(":")[1]);

Calendar cal2 = Calendar.getInstance();
cal2.set(Calendar.HOUR_OF_DAY, exceptFromHour);
cal2.set(Calendar.MINUTE, exceptFromMinute);

long exceptFrom = cal2.getTimeInMillis();
Calendar cal1 = Calendar.getInstance();
cal1.set(Calendar.HOUR_OF_DAY, exceptToHour);
cal1.set(Calendar.MINUTE, exceptToMinute);

long exceptTo = cal1.getTimeInMillis();
if (currentTime < exceptFrom) {
timeSlot.add(sdfTime.format(new Date(currentTime)));
currentTime = currentTime + slot;
} else if (currentTime > exceptFrom && currentTime > exceptTo) {
timeSlot.add(sdfTime.format(new Date(currentTime)));
currentTime = currentTime + slot;
} else {
currentTime = currentTime + slot;
}
}
}
}
return timeSlot;
}

How can I divide an interval [start,end] into n points of equal distance?

According to the logic you have shown so far the the interval sizes are:

  • closed: (end - start) / (n - 1)
  • open one side: (end - start) / n
  • open both sides: (end - start) / (n + 1)

The initial left point is:

  • closed on the left: start
  • open on the left: start + interval.

All other points just add a interval on top

Php - divide time period into equal intervals

Finaly I solve my problem! Thanks, tharkun for interest!
At this moment it works as I want it to! I suppose that in some cases this function might work with some bugs, so I would like to know if they show up.
For now here are two functions. Sory, if it could be written shorter and more optimized, but at least it works!

//Author: Elvijs Kukša
function splitTimeIntoIntervals($work_starts,$work_ends,$break_starts=null,$break_ends=null,$minutes_per_interval=60){
$intervals=array();
$time = date("H:i",strtotime($work_starts));
$first_after_break=false;
while( strtotime($time) < strtotime($work_ends)){
// if at least one of the arguments associated with breaks is mising - act like there is no break
if($break_starts==null || $break_starts==null )
{
$time_starts=date("H:i", strtotime($time));
$time_ends=date("H:i", strtotime($time_starts."+$minutes_per_interval minutes"));
}
// if the break start/end time is specified
else{
if($first_after_break==true){//first start time after break
$time=(date("H:i",strtotime($break_ends)));
$first_after_break=false;
}
$time_starts=(date("H:i",strtotime($time)));
$time_ends=date("H:i", strtotime($time_starts."+$minutes_per_interval minutes"));
//if end_time intersects break_start and break_end times
if(timesIntersects($time_starts, $time_ends, $break_starts, $break_ends))
{
$time_ends=date("H:i",strtotime($break_starts));
$first_after_break=true;
}
}
//if end_time is after work_ends
if(date("H:i", strtotime($time_ends))>date("H:i",strtotime($work_ends)))
{
$time_ends=date("H:i",strtotime($work_ends));
}
$intervals[] = array( 'starts' =>$time_starts, 'ends' => $time_ends);
$time=$time_ends;
}
return $intervals;
}
//time intersects if order of parametrs is one of the following 1342 or 1324


function timesIntersects($time1_from,$time1_till, $time2_from, $time2_till){
$out;
$time1_st=strtotime($time1_from);
$time1_end=strtotime($time1_till);
$time2_st=strtotime($time2_from);
$time2_end=strtotime($time2_till);
$duration1 =$time1_end - $time1_st;
$duration2 = $time2_end - $time2_st;
$time1_length=date("i",strtotime($time1_till."-$time1_from"));
if(
(($time1_st<=$time2_st&&$time2_st<=$time1_end&&$time1_end<=$time2_end)
||
($time1_st<=$time2_st&&$time2_st<=$time2_end&&$time2_end<=$time1_end)
&&
($duration1>=$duration2)
)
||
( $time1_st<=$time2_st&&$time2_st<=$time1_end&&$time1_end<=$time2_end)
&&
($duration1<$duration2)
)
{
return true;
}


return false;
}

How to divide time interval into parts of varying length?

You could use a generator:

def interval(start, stop):
cur = start
yield cur # return the start value
while cur < stop:
for increment in (2.25, 2.25, 1.5):
cur += increment
if cur >= stop: # stop as soon as the value is above the stop (or equal)
break
yield cur
yield stop # also return the stop value

It works for the start and stop you proposed:

>>> list(interval(0, 19))
[0, 2.25, 4.5, 6.0, 8.25, 10.5, 12.0, 14.25, 16.5, 18.0, 19]

You could also use itertools.cycle to avoid the outer loop:

import itertools

def interval(start, stop):
cur = start
yield start
for increment in itertools.cycle((2.25, 2.25, 1.5)):
cur += increment
if cur >= stop:
break
yield cur
yield stop

Split time into equal intervals in Java

First, convert hours to minutes.

10AM = 10 * 60 minutes
2PM or 14h = 14 * 60 minutes

Then, in order to convert minutes back to hours :

minutes / 60 gives the number of hours
minutes % 60 gives the number of the minutes in the last hour.

If you want to display hours/minutes always on 2 digits, and eventually pad with a leading zero, use :

String result = String.format("%02d", minutes/60);

(2 indicates that you want 2 digits and 0 indicates that you pad with zeros)

That said, there are 2 ways of going from 10*60 to 14*60 with steps of 30 minutes :

The most intuitive : the while loop

public static void interval(int begin, int end, int interval) {
int time = begin;
while (time <= end) {
System.out.println(String.format("%02d:%02d", time / 60, time % 60));
time += interval;
}
}

But I don't like while loops. If you do it wrong (or if the input data are wrong), it ends in infinite loops.

The other way : as you know the begin, the end and the step, use a for loop :

public static void interval2(int begin, int end, int interval) {
for (int time = begin; time <= end; time += interval) {
System.out.println(String.format("%02d:%02d", time / 60, time % 60));
}
}

Test :

interval2(10 * 60, 14 * 60, 30);

Result :

10:00
10:30
11:00
11:30
12:00
12:30
13:00
13:30
14:00


Related Topics



Leave a reply



Submit