Python Forum
Check time within specific time ranges
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Check time within specific time ranges
#1
Hello,

I got the following case:

time slots:

From. To. Multiplier
9:00 19:59 1
20:00 21:59 2
22:00 23:59 3
0:00 6:59 4
7:00 8:59 2

I would like to compute the sum of the minutes of the time between starttime and endtime in each time slot multiplied by their multiplier.

So for example:

starttime = 08:11
endtime = 14:16

will lead to 48 minutes x 1.5 + 316 minutes x 1 = 388.

Can anyone help me writing a code for this?
Reply
#2
Your example is wrong:
Quote:So for example:

starttime = 08:11
endtime = 14:16

will lead to 48 minutes x 1.5 + 316 minutes x 1 = 388.
The time overlaps are correct, but the multiplier for 7:00 8:59 is 2, not 1.5.

It will be easier if you convert all times to minutes. datetime makes this easy.

The intersection of time windows a and b is:
start = max(a_start, b_start)
end = max(start, min(a_end, b_end))

8:11 = 491 minutes
14:16 = 856 minutes
9:00 = 540 minutes
19:59 = 1199 minutes
start = max(491, 540) = 540
end = max(540, min(856, 1199)) = 856
overlap = 856 - 540 = 316 minutes
Reply
#3
Okay can you try this one?

from datetime import datetime, timedelta def compute_weighted_minutes(starttime, endtime, time_slots): # Convert input times to datetime objects starttime = datetime.strptime(starttime, "%H:%M") endtime = datetime.strptime(endtime, "%H:%M") # Handle case where endtime is past midnight if endtime < starttime: endtime += timedelta(days=1) total_weighted_minutes = 0 for slot in time_slots: slot_start = datetime.strptime(slot["From"], "%H:%M") slot_end = datetime.strptime(slot["To"], "%H:%M") # Adjust slots for crossing midnight if slot_end < slot_start: slot_end += timedelta(days=1) # Determine overlap between time interval and current slot overlap_start = max(starttime, slot_start) overlap_end = min(endtime, slot_end) if overlap_start < overlap_end: overlap_minutes = (overlap_end - overlap_start).seconds // 60 total_weighted_minutes += overlap_minutes * slot["Multiplier"] return total_weighted_minutes # Define time slots with multipliers time_slots = [ {"From": "09:00", "To": "19:59", "Multiplier": 1}, {"From": "20:00", "To": "21:59", "Multiplier": 2}, {"From": "22:00", "To": "23:59", "Multiplier": 3}, {"From": "00:00", "To": "06:59", "Multiplier": 4}, {"From": "07:00", "To": "08:59", "Multiplier": 2}, ] # Example usage starttime = "08:11" endtime = "14:16" result = compute_weighted_minutes(starttime, endtime, time_slots) print("Total weighted minutes:", result)
our gd project- geometry dash
Reply
#4
(Jan-20-2025, 03:59 PM)Keville_35 Wrote: Okay here it's
@Keville_35 This is a Python forum but your code is not Python code. What is the point of posting this?
« We can solve any problem by introducing an extra level of indirection »
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [SOLVED] [datetime.strptime] ValueError: time data 'foo' does not match format 'bar' Winfried 1 3,091 Jan-02-2025, 02:09 AM
Last Post: lyly19
  Help about a specific time selection QJZ 0 862 Dec-01-2024, 11:25 AM
Last Post: QJZ
  How to move an object over time rather than instantly? temlotresid6 3 3,301 Oct-23-2024, 11:20 AM
Last Post: temlotresid6
  Is there a difference between Python’s time.sleep and win32api.Sleep? phpjunkie 4 1,968 Sep-21-2024, 05:17 PM
Last Post: aakritiintelligence
  How to insert text time - into video frames? oxidian 0 2,670 Aug-25-2024, 04:51 PM
Last Post: oxidian
  Using RTC time in Rasberry Pi Programs sab201 1 1,545 Aug-18-2024, 05:50 PM
Last Post: snippsat
  Schedule exit a program at a specific time 4 am every day. chubbychub 3 2,307 May-17-2024, 03:45 PM
Last Post: chubbychub
  Filer and sort files by modification time in a directory tester_V 5 3,987 May-02-2024, 05:39 PM
Last Post: tester_V
Question Convert UTC now() to local time to compare to a strptime() Calab 2 2,919 Apr-29-2024, 07:24 PM
Last Post: deanhystad
  Date Time Series Help...Please spra8560 2 2,106 Feb-01-2024, 01:38 PM
Last Post: spra8560

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.