Printing during different times of week - python

Welp, I'm a noob when it comes to Python. No doubt about it. Have done some VBS and VB so I have a bit of understanding.
What I am tasked to do using Python seems easy: run an action only during these times:
Mon: between 1:30 am and 7:30 am
Tues – Fri: between 3:00 am 7:30 am
Sat: between 1:00 am and 9:00 am and 5:00 pm to Midnight
Sun: Midnight to 8:30 am
Trouble is, all I've been able to come up with is this (and I'm not even sure this is working properly):
import time
def IsOffHour(time):
if (time.tm_wday > 4):
return True
elif (time.tm_hour >= 17):
return True
elif (time.tm_hour < 8):
return True
else:
return False
now = time.localtime()
if IsOffHour(now):
print 'hello cruel world !'
I'm not sure how to handle the times that start at :30. It's been a bit hard to test, maybe I can change the system date and dime to test it out.
It seems like I'm close, open to ideas.
Thanks!

Instead of using the time module you should try the datetime module. It's much easier for tasks like these.
If you use a fictional date (or replace the date in your checks) than you can do it like this:
>>> x = datetime.datetime(1, 1, 1, 13, 37, 40)
>>> a = datetime.datetime(1, 1, 1, 1, 30, 0)
>>> b = datetime.datetime(1, 1, 1, 7, 30, 0)
>>> a < x < b
False
>>> x = datetime.datetime(1, 1, 1, 5, 0, 0)
>>> a < x < b
True

My ideas:
do check for each day separately (if time_wday == ... or if time_wday in [...])
for checking hours convert them to 24h based string (there is strftime()) and then compare as strings, so instead of time.tm_hour >= .. this will look as hrstr > '13:30' and hrstr < '19:30'
this gives code like:
def IsOffHour(dt):
hrstr = '%02d:%02d' % (dt.tm_hour, dt.tm_min)
if dt.tm_wday == 0:
return '01:30' <= hrstr <= '07:30'
if dt.tm_wday in [1, 2, 3, 4]:
return '03:00' <= hrstr <= '07:30'
if dt.tm_wday == 5:
return '01:00' <= hrstr <= '09:00' or hrstr >= '17:00'
if dt.tm_wday == 6:
return hrstr <= '08:30'
return False

What you should be doing is comparing time objects to time objects rather than extracting the hours and minutes and doing this by hand.
So define the acceptable time windows in your script using time objects and then just see if the current time falls in any of the those windows.
from datetime import datetime,time
# Set our allowed time windows in a dictionay indexed by day, with 0 =
# Monday, 1 = Tuesday etc. Each value is list of tuples, the tuple
# containing the start and end time of each window in that day
off_windows = {
0: [(time(1,30),time(7,30))],
1: [(time(3,0),time(7,30))],
2: [(time(3,0),time(7,30))],
3: [(time(3,0),time(7,30))],
4: [(time(3,0),time(7,30))],
5: [(time(1,0),time(9,0)),(time(16,0),time.max)], #time.max is just before midnight
6: [(time(0,0),time(8,30))]
}
def is_off_hours():
# Get current datetime
current = datetime.now()
# get day of week and time
current_time = current.time()
current_day = current.weekday()
# see if the time falls in any of the windows for today
return any(start <= current_time <= end for (start,end) in off_windows[current_day])
if is_off_hours():
print 'Hello cruel world!'
Above we use the any function which returns True if any value of an iterable is True. So he code loops through the off windows which we have defined for a day, returning true if the current time falls within any of them.
A nice this about python is we can say:
start <= current_time <= end
instead of
start <= current_time and current_time <= end

Related

Code to check if current time is before due time

I am trying to see if the current hour, time and section is before the due hour, due minute and due section then it should print true otherwise false. My code is not working and ive been working on this for 2 hours
current_hour = 12
current_minute = 37
current_section = "PM"
due_hour = 9
due_minute = 0
due_section = "AM"
if (((current_hour < 9) and (current_hour != 12)) and (current_minute != 0) and current_section):
print("True")
else:
print("False")
Your current code is failing (presumably) because you're using 'and current_section' which will pass True for any value of current_selection.
Using the datetime library makes this quite simple:
from datetime import datetime
due_time = datetime.strptime('9:00AM','%I:%M%p')
curr_time = datetime.strptime('12:37PM','%I:%M%p')
diff_seconds = (curr_time - due_time).total_seconds()
if diff_seconds > 0:
print('False')
else:
print('True')
You can also add dates to make it more robust (see https://stackoverflow.com/a/466376/10475762 for more information on how to use strptime).

How many "premium" hours between datetimes?

This is part of a bigger problem we're facing but the problem at the moment is splitting time between two datetimes into two rates based on when those hours are in the day. It's quite arbitrary but we treat 7am-7pm as normal hours and the opposite 12 hours as premium.
So for any given pair of datetimes, we need to grade these down so that we know how many normal hours, or how many premium hours there were in that period. A couple of examples:
If we took the next 24 hours, I'd expect an exact split of 12 hours.
> start = datetime.datetime.now()
> end = start + datetime.timedelta(1)
> split_hours(start, end)
(datetime.timedelta(0, 43200), datetime.timedelta(0, 43200))
If we took the next 12 hours, at 20:26, I'd expect 1h26 normal and 10h34m premium rate:
> start = datetime.datetime(2017, 11, 6, 20, 26, 0)
> end = start + datetime.timedelta(hours=12)
> split_hours(start, end)
(datetime.timedelta(0, 5160), datetime.timedelta(0, 38040))
"How do I do that?" is my question. Sorry. I've been thinking through this most of the day but only ever got as far as the following napkin algorithm:
Split range into distinct-date datetime ranges (how?!) and for each:
Count hours before 7am and after 7pm as premium
Count hours between 7am and 7pm
Total them up.
But even there I don't know how to split things up.
There is also a natural extension —that I'll almost certainly have to implement at some point— that also grades weekend hours as premium too. If I could split time (as in my napkin algorithm) it would be easy to tack on but I still don't like how clumsy that "code" is). If your answer covers that too, you can have my firstborn. Well, no, you can have a bounty or something.
I'm doing this in Python without any real library limits (if eg Pandas Just Does™ this) but if you want to submit a raw C or Pseudo code answer, I'm sure I'll be able to read it.
We could:
generate a range of datetime between start and end
loop that range and calculate normal seconds (the length - normal = premium)
Here is the code:
import datetime
def split_hours(start, end):
# Total seconds
length = int((end-start).total_seconds())
# Generator with datetime objects
s = (start + datetime.timedelta(seconds=i) for i in range(length))
# Calculate normal and premium
# normal when hour > 7 AM, smaller than 7 PM and weekday not sat,sun
normal = sum(7 <= i.hour < 19 and i.weekday() not in [5,6] for i in s)
premium = length - normal
d = dict(normal=normal,
premium=premium,
total=dict(h=length/3600,m=length/60,s=length))
return d
And now we can do some tests:
start = datetime.datetime.now()
end1 = start + datetime.timedelta(hours=12)
end2 = start + datetime.timedelta(days=1)
end3 = start + datetime.timedelta(days=24)
print(split_hours(start,end1))
print(split_hours(start,end2))
print(split_hours(start,end3))
Returns:
# 12 hours
{'total': {'h': 12.0, 's': 43200, 'm': 720.0}, 'premium': 26131, 'normal': 17069}
# 1 days / 24 hours
{'total': {'h': 24.0, 's': 86400, 'm': 1440.0}, 'premium': 43200, 'normal': 43200}
# 7 days
{'total': {'h': 168.0, 's': 604800, 'm': 10080.0}, 'premium': 388800, 'normal': 216000}
That would be my approach:
from datetime import datetime, timedelta
def is_premium_time_period(start_time, end_time):
start_time = datetime.strptime(start_time, "%d-%m-%Y %H:%M")
end_time = datetime.strptime(end_time, "%d-%m-%Y %H:%M")
seconds = (end_time - start_time).total_seconds()
minutes = int(seconds / 60)
premium_minutes = 0
regular_minutes = 0
for minute in range(minutes):
premium_start = datetime.strptime("19:00 {}".format(start_time.date()), "%H:%M %Y-%m-%d")
premium_end = premium_start + timedelta(hours=12)
previous_start = premium_start - timedelta(hours=24)
previous_end = previous_start + timedelta(hours=12)
if premium_start <= start_time < premium_end or previous_start <= start_time < previous_end:
premium_minutes += 1
else:
regular_minutes += 1
start_time += timedelta(minutes=1)
_premium_hours = premium_minutes / 60
_regular_hours = regular_minutes / 60
return _premium_hours, _regular_hours
datetime_01 = "06-11-2017 14:17"
datetime_02 = "06-11-2017 19:20"
datetime_03 = "05-11-2017 02:39"
datetime_04 = "11-11-2017 08:39"
print(is_premium_time_period(datetime_01, datetime_02))
print(is_premium_time_period(datetime_03, datetime_04))
EDIT: I'm sorry, I forgot to post what it returns:
It returns:
(0.3333333333333333, 4.716666666666667)
(76.35, 73.65)
Meaning (premium_hours, regular_hours)

How to find number of Mondays or any other weekday between two dates in Python?

I have two dates between which I need to find out how many Mon- Fri are coming(except for Sta, Sun), everyday should be counted
Currently I am thinking this:
import calendar
import datetime
start_date = datetime.datetime.strptime("01/01/2017",'%d/%m/%Y')
end_date = datetime.datetime.strptime("31/01/2017",'%d/%m/%Y')
week_arr = [0] * 7
calendar.day_name[start_date.weekday()] ## will give me name of day
"""
As I receive Monday I will increment week_arr[0] by 1, Tuesday
week_arr[1]+= 1,
"""
I am not getting how to do it effectively so that I dont use much line of code(less if -else and for loops), may be some tricks in pandas.
You can define a function and use it like this :
def num_days_between( start, end, week_day):
num_weeks, remainder = divmod( (end-start).days, 7)
if ( week_day - start.weekday() ) % 7 < remainder:
return num_weeks + 1
else:
return num_weeks
where week_day is day number you wan to calculate count.
This code still uses a for loop and an if/else.
import datetime
import calendar
def weekday_count(start, end):
start_date = datetime.datetime.strptime(start, '%d/%m/%Y')
end_date = datetime.datetime.strptime(end, '%d/%m/%Y')
week = {}
for i in range((end_date - start_date).days):
day = calendar.day_name[(start_date + datetime.timedelta(days=i+1)).weekday()]
week[day] = week[day] + 1 if day in week else 1
return week
print(weekday_count("01/01/2017", "31/01/2017"))
# prints result
# {'Monday': 5, 'Tuesday': 5, 'Friday': 4, 'Wednesday': 4, 'Thursday': 4, 'Sunday': 5, 'Saturday': 4}
Number of Mondays in 2020 can be got using numpy library
import numpy as np
np.busday_count('2020', '2021', weekmask='Mon')
This is efficient - even in the face of ten thousands of days between start and end - and still very flexible (it iterates at most 7 times inside the sum function):
def intervening_weekdays(start, end, inclusive=True, weekdays=[0, 1, 2, 3, 4]):
if isinstance(start, datetime.datetime):
start = start.date() # make a date from a datetime
if isinstance(end, datetime.datetime):
end = end.date() # make a date from a datetime
if end < start:
# you can opt to return 0 or swap the dates around instead
raise ValueError("start date must be before end date")
if inclusive:
end += datetime.timedelta(days=1) # correct for inclusivity
try:
# collapse duplicate weekdays
weekdays = {weekday % 7 for weekday in weekdays}
except TypeError:
weekdays = [weekdays % 7]
ref = datetime.date.today() # choose a reference date
ref -= datetime.timedelta(days=ref.weekday()) # and normalize its weekday
# sum up all selected weekdays (max 7 iterations)
return sum((ref_plus - start).days // 7 - (ref_plus - end).days // 7
for ref_plus in
(ref + datetime.timedelta(days=weekday) for weekday in weekdays))
This takes both datetime.date as well as datetime.datetime objects for start and end, respectively.
Also, you can choose between a closed (inclusive=True) and a half-open (inclusive=False) interval.
By default, it calculates the number of workdays between the dates, but you can choose any set of weekdays (weekend days: weekdays=[5, 6]) or single weekdays (Wednesdays: weekdays=2) as well.
If anyone need an even simpler answer,
from datetime import date
d1 = date(2017, 1, 4)
d2 = date(2017, 1, 31)
count = 0
for d_ord in range(d1.toordinal(), d2.toordinal()):
d = date.fromordinal(d_ord)
if (d.weekday() == 4):
count += 1
print(count)
I found a simple and easy to understand code using for loop.
Take first date identify its weekday with "%a" compare it with your intrested weekday if found increment a count value. and repeat the steps till your last day.
Code is as below for your refrence i took monday as my intrested weekdays.
import datetime
A1=datetime.datetime.strptime("1/23/2016", "%m/%d/%Y")
A2=datetime.datetime.strptime("11/10/2016", "%m/%d/%Y")
count=0
week="Mon"
for i in range ((A2-A1).days): #gives the no of days from A1 to A2
if A1.strftime("%a")==week:
count+=1
A1+=datetime.timedelta(days=1)
print(count)
https://www.w3schools.com/python/python_datetime.asp

Python and check if current datetime is in specific range

I am trying to do something similar to this, but i want to specify the start date and end date by actual weekday names and times. For example, I want to check if the current datetime (datetime.datetime.now()) is in between Tuesday at 4:30pm and Thursday at 11:45am. This would update weekly so it has to be by Tuesday/Thursday mentality.
I have thought about how to do the weekdays (but i don't know how to wrap the time part into it):
TimeNow = datetime.datetime.now()
if TimeNow.weekday() >= 1 and TimeNow.weekday() <= 3:
#runcodehere
Any thoughts on how i would do this?
Neatest way is to use the amount of minutes elapsed in a week:
def mins_in_week(day, hour, minute):
return day * 24 * 60 + hour * 60 + minute
if (mins_in_week(1, 16, 30) <
mins_in_week(TimeNow.weekday(), TimeNow.hour, TimeNow.minute) <
mins_in_week(3, 11, 45)):
....
It's not very neat but something like this should work:
TimeNow = datetime.datetime.now()
if (TimeNow.weekday() == 1 and ((TimeNow.hour() == 4 and TimeNow.minute >= 30) or TimeNow.hour > 4)) or (TimeNow.weekday() == 2) or (TimeNow.weekday() == 3 and (TimeNow.hour() < 11 or (TimeNow.hour() == 11 and TimeNow.minute <= 45)):
#runcodehere
You can use a combination of and and or, and have different conditions for each day:
import datetime
TimeNow = datetime.datetime.now()
day_now = TimeNow.weekday()
time_now = TimeNow.hour*60 + TimeNow.minute
if (day_now == 1 and time_now > 990) or (day_now == 2) or (day_now == 3 and time_now < 705):
# do something

Seemingly strange behavior with python counter program

A portion of a recent assignment was to design a program that counts days between dates. I have yet to finish, and I know this could be improved drastically. However, my question is: when I run this program with date2 (below), an error occurs, but this runs as planned with date1 (again, below). I find this behavior strange. These dates are only one day apart and yet one fails and one doesn't. I've tried this with multiple dates, but each fails after 994 days regardless of month, year, day, whatever. Why?
clarification: I define "fail" as
File "first.py", line 35, in counter
return counter(date_array, x+1)
.
def date_test(date_array):
first = (date_array[0], date_array[1], date_array[2])
second = (date_array[3], date_array[4], date_array[5])
if first > second:
return False
elif first != second:
return True
elif first == second:
return False
else:
return "Error: This shouldn't happen."
def counter(date_array, x = 0):
months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
while(date_test(date_array)):
if(date_array[2] == 31 and date_array[1] == 12):
date_array[0] += 1; date_array[1], date_array[2] = 1, 1
return counter(date_array, x+1)
else:
if date_array[2] < months[date_array[1]-1]:
date_array[2] += 1
return counter(date_array, x+1)
else:
date_array[1] += 1; date_array[2] = 1;
return counter(date_array, x+1);
return x
def tuple_test(date):
date_array = []
for x in date:
date_array.append(x)
if not date_test(date_array):
return "The first date is before the second. Swap them."
return counter(date_array)
date1 = (1900,1,1,1902,9,22)
date2 = (1900,1,1,1902,9,23)
print tuple_test(date1)
print tuple_test(date2)
The reason for the error is that you've exceeded your maximum recursion depth.
To "hack" (and verify) this problem you could simply add
import sys
sys.setrecursionlimit(10000)
to the top of your code
you should use the tools python provides
>>> import datetime
>>> date1 = (1900,1,1,1902,9,22) #your weird list with 2 dates
>>> dt1 = datetime.date(*date1[:3]) #create a date object == datetime.date(1900,1,1)
>>> dt2 = datetime.date(*date1[3:]) #create a date object == datetime.date(1902,9,22)
>>> if dt1 < dt2: dt1,dt2 = dt2,dt1 #if dt1 is smaller than dt2, swap them
...
>>> print (dt1 - dt2).days #subtract them and print their days
994
>>>

Categories