logo
down
shadow

How do I get certain Time Range in Python


How do I get certain Time Range in Python

By : Aslam
Date : January 12 2021, 08:33 AM
like below fixes the issue This is the closest interpretation of your JS code I could do in this short time:
code :
from datetime import datetime

#assuming time format comes in a string like this: '8:30:00'
#assumming provided time has sense, i.e. end1 > start1 and end2 > start2

TIME_FORMAT = '%H:%M:%S'

def overlap(start1, end1, start2, end2):
    #transform time        
    start1_time = datetime.strptime(start1, TIME_FORMAT )
    end1_time = datetime.strptime(end1, TIME_FORMAT )
    start2_time = datetime.strptime(start2, TIME_FORMAT )
    end2_time = datetime.strptime(end2, TIME_FORMAT )

    #checking conditions
    if (min(start1_time, end1_time) <= max(start2_time, end2_time)) \
    and \
    (max(start1_time, end1_time) >= min(start2_time, end2_time)):
        return True
    else:
        return False

>>>overlap('8:30:00','9:30:00','8:54:00','9:00:00')
True

>>>overlap('8:30:00','9:30:00','9:54:00','10:00:00')
False


Share : facebook icon twitter icon
How to check if the current time is in range in python using time strings?

How to check if the current time is in range in python using time strings?


By : Icaro Oliveira
Date : March 29 2020, 07:55 AM
Hope that helps Not really sure which version of Python you're using so I worked it up in python3.
code :
import time

old_time = time.strptime('01:00', '%H:%M')
new_time = time.strptime('06:00', '%H:%M')

print(old_time)
print(new_time)

if old_time > new_time:
    print('old time is old')
else:
    print('new is cool')
How can a looping Python script detect if the current time is in a certain hour:minute time range?

How can a looping Python script detect if the current time is in a certain hour:minute time range?


By : Ranger
Date : March 29 2020, 07:55 AM
Any of those help Here is the in_angle_interval function from my linked answer in the comments, modified to work with minutes, since your code uses that resolution. The in_time_interval function itself is quite simple, but I've written a few other functions to make it more convenient to use and to test. The code below also has a simple now_in_minutes function so you can easily test if the current time falls within a specified interval; that function isn't used in my test code.
code :
from datetime import datetime

def hhmm_to_minutes(s):
    h, m = s[:2], s[2:]
    return 60 * int(h) + int(m)

def minutes_to_hhmm(minutes):
    return hour_min_to_hhmm(*divmod(minutes, 60))

def hour_min_to_hhmm(h, m):
    return '{:02d}{:02d}'.format(h, m)

def now_in_minutes():
    now = datetime.now()
    return 60 * now.hour + now.minute

# Test if x falls within the the time interval from a to b, inclusive
# All times are given in minutes since midnight.
minutes_per_day = 1440
def in_time_interval(x, a, b):
    return (x - a) % minutes_per_day <= (b - a) % minutes_per_day

# Find which times fall in the interval t0 - t1, inclusive.
# Test times start at 0000 and increment by delta minutes
def in_test(t0, t1, delta=10):
    print('{} - {}: '.format(t0, t1), end='')
    t0 = hhmm_to_minutes(t0)
    t1 = hhmm_to_minutes(t1)

    passed = [minutes_to_hhmm(nowmin)
        for nowmin in range(0, minutes_per_day, delta)
            if in_time_interval(nowmin, t0, t1)
    ]
    print(', '.join(passed))

# Find which times DON'T fall in the interval t0 - t1, inclusive.
# Test times start at 0000 and increment by delta minutes
def out_test(t0, t1, delta=10):
    print('{} - {}: '.format(t0, t1), end='')
    t0 = hhmm_to_minutes(t0)
    t1 = hhmm_to_minutes(t1)

    failed = [minutes_to_hhmm(nowmin)
        for nowmin in range(0, minutes_per_day, delta)
            if not in_time_interval(nowmin, t0, t1)
    ]
    print(', '.join(failed))

m = 30
print('In tests. The printed times are in the interval at the start of the line')
for h in range(24):
    t0 = hour_min_to_hhmm(h, m)
    t1 = hour_min_to_hhmm((h + 2) % 24, m)
    in_test(t0, t1)

print('\n' + '- ' * 20 + '\n')
print('Out tests. The printed times are outside the interval at the start of the line')
for h in range(24):
    t0 = hour_min_to_hhmm(h, m)
    t1 = hour_min_to_hhmm((h + 2) % 24, m)
    out_test(t1, t0)
In tests. The printed times are in the interval at the start of the line
0030 - 0230: 0030, 0040, 0050, 0100, 0110, 0120, 0130, 0140, 0150, 0200, 0210, 0220, 0230
0130 - 0330: 0130, 0140, 0150, 0200, 0210, 0220, 0230, 0240, 0250, 0300, 0310, 0320, 0330
0230 - 0430: 0230, 0240, 0250, 0300, 0310, 0320, 0330, 0340, 0350, 0400, 0410, 0420, 0430
0330 - 0530: 0330, 0340, 0350, 0400, 0410, 0420, 0430, 0440, 0450, 0500, 0510, 0520, 0530
0430 - 0630: 0430, 0440, 0450, 0500, 0510, 0520, 0530, 0540, 0550, 0600, 0610, 0620, 0630
0530 - 0730: 0530, 0540, 0550, 0600, 0610, 0620, 0630, 0640, 0650, 0700, 0710, 0720, 0730
0630 - 0830: 0630, 0640, 0650, 0700, 0710, 0720, 0730, 0740, 0750, 0800, 0810, 0820, 0830
0730 - 0930: 0730, 0740, 0750, 0800, 0810, 0820, 0830, 0840, 0850, 0900, 0910, 0920, 0930
0830 - 1030: 0830, 0840, 0850, 0900, 0910, 0920, 0930, 0940, 0950, 1000, 1010, 1020, 1030
0930 - 1130: 0930, 0940, 0950, 1000, 1010, 1020, 1030, 1040, 1050, 1100, 1110, 1120, 1130
1030 - 1230: 1030, 1040, 1050, 1100, 1110, 1120, 1130, 1140, 1150, 1200, 1210, 1220, 1230
1130 - 1330: 1130, 1140, 1150, 1200, 1210, 1220, 1230, 1240, 1250, 1300, 1310, 1320, 1330
1230 - 1430: 1230, 1240, 1250, 1300, 1310, 1320, 1330, 1340, 1350, 1400, 1410, 1420, 1430
1330 - 1530: 1330, 1340, 1350, 1400, 1410, 1420, 1430, 1440, 1450, 1500, 1510, 1520, 1530
1430 - 1630: 1430, 1440, 1450, 1500, 1510, 1520, 1530, 1540, 1550, 1600, 1610, 1620, 1630
1530 - 1730: 1530, 1540, 1550, 1600, 1610, 1620, 1630, 1640, 1650, 1700, 1710, 1720, 1730
1630 - 1830: 1630, 1640, 1650, 1700, 1710, 1720, 1730, 1740, 1750, 1800, 1810, 1820, 1830
1730 - 1930: 1730, 1740, 1750, 1800, 1810, 1820, 1830, 1840, 1850, 1900, 1910, 1920, 1930
1830 - 2030: 1830, 1840, 1850, 1900, 1910, 1920, 1930, 1940, 1950, 2000, 2010, 2020, 2030
1930 - 2130: 1930, 1940, 1950, 2000, 2010, 2020, 2030, 2040, 2050, 2100, 2110, 2120, 2130
2030 - 2230: 2030, 2040, 2050, 2100, 2110, 2120, 2130, 2140, 2150, 2200, 2210, 2220, 2230
2130 - 2330: 2130, 2140, 2150, 2200, 2210, 2220, 2230, 2240, 2250, 2300, 2310, 2320, 2330
2230 - 0030: 0000, 0010, 0020, 0030, 2230, 2240, 2250, 2300, 2310, 2320, 2330, 2340, 2350
2330 - 0130: 0000, 0010, 0020, 0030, 0040, 0050, 0100, 0110, 0120, 0130, 2330, 2340, 2350

- - - - - - - - - - - - - - - - - - - - 

Out tests. The printed times are outside the interval at the start of the line
0230 - 0030: 0040, 0050, 0100, 0110, 0120, 0130, 0140, 0150, 0200, 0210, 0220
0330 - 0130: 0140, 0150, 0200, 0210, 0220, 0230, 0240, 0250, 0300, 0310, 0320
0430 - 0230: 0240, 0250, 0300, 0310, 0320, 0330, 0340, 0350, 0400, 0410, 0420
0530 - 0330: 0340, 0350, 0400, 0410, 0420, 0430, 0440, 0450, 0500, 0510, 0520
0630 - 0430: 0440, 0450, 0500, 0510, 0520, 0530, 0540, 0550, 0600, 0610, 0620
0730 - 0530: 0540, 0550, 0600, 0610, 0620, 0630, 0640, 0650, 0700, 0710, 0720
0830 - 0630: 0640, 0650, 0700, 0710, 0720, 0730, 0740, 0750, 0800, 0810, 0820
0930 - 0730: 0740, 0750, 0800, 0810, 0820, 0830, 0840, 0850, 0900, 0910, 0920
1030 - 0830: 0840, 0850, 0900, 0910, 0920, 0930, 0940, 0950, 1000, 1010, 1020
1130 - 0930: 0940, 0950, 1000, 1010, 1020, 1030, 1040, 1050, 1100, 1110, 1120
1230 - 1030: 1040, 1050, 1100, 1110, 1120, 1130, 1140, 1150, 1200, 1210, 1220
1330 - 1130: 1140, 1150, 1200, 1210, 1220, 1230, 1240, 1250, 1300, 1310, 1320
1430 - 1230: 1240, 1250, 1300, 1310, 1320, 1330, 1340, 1350, 1400, 1410, 1420
1530 - 1330: 1340, 1350, 1400, 1410, 1420, 1430, 1440, 1450, 1500, 1510, 1520
1630 - 1430: 1440, 1450, 1500, 1510, 1520, 1530, 1540, 1550, 1600, 1610, 1620
1730 - 1530: 1540, 1550, 1600, 1610, 1620, 1630, 1640, 1650, 1700, 1710, 1720
1830 - 1630: 1640, 1650, 1700, 1710, 1720, 1730, 1740, 1750, 1800, 1810, 1820
1930 - 1730: 1740, 1750, 1800, 1810, 1820, 1830, 1840, 1850, 1900, 1910, 1920
2030 - 1830: 1840, 1850, 1900, 1910, 1920, 1930, 1940, 1950, 2000, 2010, 2020
2130 - 1930: 1940, 1950, 2000, 2010, 2020, 2030, 2040, 2050, 2100, 2110, 2120
2230 - 2030: 2040, 2050, 2100, 2110, 2120, 2130, 2140, 2150, 2200, 2210, 2220
2330 - 2130: 2140, 2150, 2200, 2210, 2220, 2230, 2240, 2250, 2300, 2310, 2320
0030 - 2230: 0000, 0010, 0020, 2240, 2250, 2300, 2310, 2320, 2330, 2340, 2350
0130 - 2330: 0000, 0010, 0020, 0030, 0040, 0050, 0100, 0110, 0120, 2340, 2350
Python : range between dates when range field has time

Python : range between dates when range field has time


By : shiva
Date : March 29 2020, 07:55 AM
I wish this help you I want to make a range between 2 dates and my range field has the time , You can add time to fdate and tdate using datetime.datetime.combine:
code :
>>> from datetime import datetime, date
>>> datetime.combine(date.today(), datetime.min.time())
datetime.datetime(2018, 7, 30, 0, 0)
>>> datetime.combine(date.today(), datetime.max.time())
datetime.datetime(2018, 7, 30, 23, 59, 59, 999999)
How to calculate efficiency of usable time from given Time Range in Python?

How to calculate efficiency of usable time from given Time Range in Python?


By : user2345969
Date : March 29 2020, 07:55 AM
I wish this help you I would do it by looping over all the time slots and adding the effective hours:
code :
def get_efficiency(inquiry_times, open_hours):
    usefull_hours = 0

    minimum_start_time = inquiry_times[0]
    max_end_time = inquiry_times[1]
    for times in open_hours:
        # the closing time cannot be later than the final end time
        # the start time cannot be earlier than the opening time/end of the previous time slot
        usefull_hours += min(max_end_time, times[1]) - max(minimum_start_time, times[0])
        minimum_start_time = times[1]

    total_hours = inquiry_times[1] - inquiry_times[0]

    return float(usefull_hours)/total_hours

print(get_efficiency((10, 18), [(10,12), (14, 16)]))
print(get_efficiency((10, 12), [(9, 14)]))
print(get_efficiency((8, 12), [(00, 10)]))
0.5
1.0
0.5
Finding time range of imputed time interval in Python

Finding time range of imputed time interval in Python


By : Lex
Date : March 29 2020, 07:55 AM
This might help you I want to make a function that will take start_time and end_time and that will find the time interval where those times fit. Time range must be at every 5 minutes, but imputed time interval can be any other time. , try this:
code :
import datetime
import time


# creating time intervals at every 5 minutes
def create_intervals():
    start_time = "00:00:00"
    end_time = "0:59:59"

    start = datetime.datetime.strptime(start_time, '%H:%M:%S')
    end = datetime.datetime.strptime(end_time, '%H:%M:%S')
    step = datetime.timedelta(minutes=5)

    time_intervals = []

    while start <= end:
        time_intervals.append(start.time())
        # print(start.time())
        start += step
    return time_intervals


def find_intervals(start_time, end_time, intervals):
    start = datetime.datetime.strptime(start_time, '%H:%M:%S').time()
    end = datetime.datetime.strptime(end_time, '%H:%M:%S').time()

    first = next(i for i, v in enumerate(intervals) if start <= v) - 1
    last = next(i for i, v in enumerate(intervals) if end <= v) + 1

    return intervals[first:last]


intervals = create_intervals()

result = [str(i) for i in find_intervals("00:13:24", "00:22:41", intervals)]
print(result)

Related Posts Related Posts :
  • How to calculate each single element of a numpy array based on conditions
  • How do I change the width of Jupyter notebook's cell's left part?
  • Measure distance between lat/lon coordinates and utm coordinates
  • Installing megam for NLTK on Windows
  • filter dataframe on each value of a samn column have a specific value of another column in Panda\Python
  • Threading with pubsub throwing AssertionError: 'callableObj is not callable' in wxPython
  • Get grouped data from 2 dataframes with condition
  • How can I import all of sklearns regressors
  • How to take all elements except the first k
  • Whats wrong with my iteration list of lists from csv
  • Tensorflow Estimator API save image summary in eval mode
  • How to Pack with PyQt - how to make QFrame/Layout adapt to content
  • python doubly linked list - insertAfter node
  • Open .h5 file in Python
  • Joining a directory name with a binary file name
  • python, sort list with two arguments in compare function
  • Is it possible to print from Python using non-ANSI colors?
  • Pandas concat historical data using date minus some number of days
  • CV2: Import Error in Python OpenCV
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org