I need the following script to compute the working hours from 9 am to 6 pm, so that if I add 5 hours it will be added from 9 am the next day.
Example: if it is now 5 pm and I add 5 hours and the working day ends at 6 pm, the output would be: 13 hours.
17 + 1 = 18 and
9 + 4 = 13 hs
So far the script computes hours regardless of the labor restriction.
from datetime import datetime, timedelta
updated = ( datetime.now() +
timedelta( hours = 5 )).strftime('%H:%M:%S')
print( updated )
--22:12:00
Here you are:
workday_begin = time(9)
workday_end = time(18)
# compute workday length
workday_hours = datetime.combine(date.today(), workday_end) - datetime.combine(date.today(), workday_begin)
# this is timedelta from your example
duration_hours = timedelta(hours=17)
# ignore times longer than a workday
day_cnt = 0 # this could be used to know how many day we've skipped, not part of question tho
while duration_hours > workday_hours:
day_cnt += 1
duration_hours -= workday_hours
now = datetime.now()
# now = datetime(2021,12,10,11,25,16)
if workday_begin < now.time() < workday_end:
# now is in work-hours
if datetime.combine(date.today(), workday_end) - now < duration_hours:
# duration would exceed work-hours, jumping to next day
day_cnt += 1
duration_hours -= (datetime.combine(date.today(), workday_end) - now)
updated = datetime.combine(date.today(), workday_begin) + duration_hours
else:
# everything is fine, just add hours
updated = now + duration_hours
else:
# now is not in work-hours. Add remaining duration to workday begin
updated = datetime.combine(date.today(), workday_begin) + duration_hours
# keep just a time part
updated = updated.time().strftime('%H:%M:%S')
print( updated )
I hope I understood your question.
Related
I am attempting to write a program that will calculate the difference in a given time and the actual time then display that delta in a while loop. I have been able to get most of this working, the only issue I have found so far is the time variables in the print statement do not update as the loop runs.
import datetime
import time
from os import system
from sys import platform
clear_screen = lambda: system("cls" if platform == "win32" else "clear")
# print("What is the time and date of your event?")
# year = int(input("Year: "))
# month = int(input("Month: "))
# day = int(input("Day: "))
# hour = int(input("Hour: "))
# minute = int(input("Minute: "))
i = 0
year = 2023
month = 1
day = 27
hour = 12
minute = 0
second = 00
today = datetime.datetime.now()
date_entry = datetime.datetime(year, month, day, hour, minute, second)
print(f"The current date & time: {today}")
print(f"The big day is: {date_entry}")
print()
print()
print(f"Tiff's Big day is going to be here soon:")
print()
event_count = date_entry - today
event_hour = event_count.total_seconds() / 3600
event_min = ((event_hour % 1) * (60 / 100)) * 100
event_sec = ((event_min % 1) * (60 / 100)) * 100
def countdown():
print(f"{event_hour:.0f} Hours, {event_min:.0f} Minutes, {event_sec:.0f} seconds until big mode!!!!!")
while i < 50:
i += 1
countdown()
time.sleep(2)
# clear_screen()`
I have a feeling that the time variables in the print statement are not recalculating... I have tried restructuring the program by moving the variables into the countdown() function. That had the same result.
I am expecting the script to output hours, minutes and seconds until a defined time. This part works great. Then pause for 2 seconds (this works) then print the statement again after it recalculates the time delta. This is were it fails, prints the exact same time as in the first print statement.
You might also notice the clear_screen(). This kinda works, it will clear all of the output. I am looking to make it clear the last line printed in the loop (ie: 40 Hours, 12 Minutes, 56 seconds until big mode!!!!!) This is something I haven't looked at much yet. If you have any suggestions...
Thanks in advance for any suggestions.
Output:
The current date & time: 2023-01-25 19:48:04.383425
The big day is: 2023-01-27 12:00:00
Tiff's Big day is going to be here soon:
40 Hours, 12 Minutes, 56 seconds until big mode!!!!!
40 Hours, 12 Minutes, 56 seconds until big mode!!!!!
40 Hours, 12 Minutes, 56 seconds until big mode!!!!!
40 Hours, 12 Minutes, 56 seconds until big mode!!!!!
40 Hours, 12 Minutes, 56 seconds until big mode!!!!!
Be careful with calling time functions, the time is assigned to a variable only the first time, here is an example in the REPL:
>>> import time
>>> time.time()
1674700748.035392
>>> time.time()
1674700749.2911549
>>> time.time()
1674700750.440412
>>> time.time()
1674700751.571879
>>> x = time.time()
>>> x
1674700755.0605464
>>> x
1674700755.0605464
>>> x
1674700755.0605464
>>> x
1674700755.0605464
>>> for i in range(5): print(time.time())
...
1674700912.1213877
1674700912.1214447
1674700912.1214585
1674700912.1214688
1674700912.1214786
>>> for i in range(5): print(x)
...
1674700755.0605464
1674700755.0605464
1674700755.0605464
1674700755.0605464
1674700755.0605464
As you can see if I call time.time multiple times the time changes, but if I assign it to x, then x always has the same value.
Below is the code I wrote to solve my problem:
import datetime
import time
from os import system
from sys import platform
import cursor
# print("What is the time and date of your event?")
# year = int(input("Year: "))
# month = int(input("Month: "))
# day = int(input("Day: "))
# hour = int(input("Hour: "))
# minute = int(input("Minute: "))
year = 2023
month = 1
day = 27
hour = 12
minute = 0
second = 00
date_entry = datetime.datetime(year, month, day, hour, minute, second)
print(f"The current date & time: {datetime.datetime.now()}")
print(f"The big day is: {date_entry}")
print()
print()
print(f"Tiff's Big day is going to be here soon:")
print()
while True:
event_count = date_entry - datetime.datetime.now()
event_hour = event_count.total_seconds() / 3600
event_min = ((event_hour % 1) * (60 / 100)) * 100
event_sec = ((event_min % 1) * (60 / 100)) * 100
print(f"{event_hour:.0f} Hours, {event_min:.0f} Minutes, {event_sec:.0f} seconds until big time!!!", end = "\r")
cursor.hide()
time.sleep(.5)
How can I convert any date to just number of days? This is what I tried:
import datetime
import calendar
def leap_day_counter(yr):
leap_days = 0
# since 1582 11 days are missing
if yr >= 1582:
leap_days += 11
for specific_year in range(1, yr):
if calendar.isleap(specific_year):
leap_days += 1
return leap_days
def month_to_day(yr, mth):
all_days = 0
for specific_month in range(1, mth+1):
days_in_month = calendar.monthrange(yr, specific_month)
all_days += days_in_month[1]
return all_days
date = datetime.datetime.now()
days_passed = ((date.year * 365) + leap_day_counter(date.year)) + month_to_day(date.year, date.month) + date.day
print(days_passed)
I got 737 158 days but according to https://www.timeanddate.com/date/durationresult.html I should have 736 755 days. Do I miss something? Is there easier way to do this?
This helps
from datetime import date
d0 = date(2000, 1, 01)
d1 = date.today()
delta = d1 - d0
print delta.days
Are the amount of days in a year correct for you?
01/01/0001 - 01/01/2018 has 736,696, you say there is 737,060. This is roughly 1 year too many.
(date.year - 1) * 365
After fixing the above, we should check if 01/01/0001 - 01/02/2018 works.
The website says 736,727, where you say 736,754. Which is about the entire month of February too many.
for specific_month in range(1, mth)
You have one too many leap years.
for specific_year in range(1, yr)
You can also simplify this code to:
def leap_day_counter(y):
y -= 1
return y//4 - y//100 + y//400
This is now the same as datetime.datetime.now().toordinal().
The number of days between two dates can be calculated as below: For more see here. Hope this may help
>>>enddate = "2018/03/12" +" 23:59"
>>>enddate = datetime.strptime(enddate, "%Y/%m/%d %H:%M")
>>>(enddate-datetime.now()).days
12
Update:edit
>>>import datetime
>>>checkdate = datetime.datetime.strptime("0001-01-01", "%Y-%m-%d")
>>>days = (datetime.datetime.now()-checkdate).days
>>>days
736757
2 days difference because start days and end date are excluded.
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)
Given a start time Timestamp('2015-05-06 09:40:45') and an end time Timestamp('2015-05-06 011:12:13'), I want to determine the number of minutes covered per hour of this duration.
That is, I want the following output:
hour 9 -- 19.25 minutes
hour 10 -- 60.00 minutes
hour 11 -- 12.22 minutes
I have an algorithm in mind, but I'm stumped comparing int and timestamp.
We are assuming that the start date and end date are the same.
from datetime import datetime
starttime = datetime(year = 2015, month = 5, day =6, hour=9, minute=40, second=45)
endtime = datetime(year = 2015, month = 5, day =6, hour=11, minute=12, second=13)
for i in range(starttime.hour, endtime.hour+1):
if i == starttime.hour:
rightbound = datetime(starttime.year, starttime.month, starttime.day, hour=(i+1), minute=0, second=0)
print starttime.hour, (rightbound - starttime).seconds/60.
elif i == endtime.hour:
leftbound = datetime(starttime.year, starttime.month, starttime.day, hour=(i), minute=0, second=0)
print leftbound.hour, (endtime - leftbound).seconds/60.
else:
print i, "60"
The (nth hour, # of minutes) output is:
9 19.25
10 60
11 12.2166666667
I need to compile a program that can convert a Gregorian date to a Mayan one. I also need to use 01/01/1970 as a reference date.
The following auxiliary functions work accordingly and have no mistakes.
# turns dates into tuples
def dmj(date):
"""
>>> dmj('01/01/1970')
(1, 1, 1970)
>>> dmj('00012+00012+02012')
(12, 12, 2012)
"""
tup = ()
for i in date:
if i.isdigit() == False and i != ' ':
date = date.replace(i, ' ')
number_str = ''
for i in date:
number_str += i
if i == ' ':
number = int(number_str)
tup += (number,)
number_str = ''
tup += (int(number_str),)
return tup
# counts days that have passed since 01/01/1970
def daysPassed(date):
"""
>>> daysPassed('01/01/1970')
0
>>> daysPassed('20-7-1988')
6775
>>> daysPassed('00012+00012+02012')
15686
"""
from datetime import date
tup = dmj(date)
begin = date(1970, 1, 1)
end = date(tup[2], tup[1], tup[0])
passed = abs(end - begin)
return passed.days
My idea was to first calculate how many days have passed since the beginning of this pictun (20 baktuns long) in 01/01/1970 and then added the days passed since then according to the given date.
In the Mayan calendar a day is refered to as a kin. Their periods (within one pictun) are as follows:
20 kin = 1 uinal; 18 uinal = 1 tun; 20 tun = 1 katun; 20 katun = 1 baktun
In the long count notation the Mayan date for 01/01/1970 is '12.17.16.7.5'. Baktun are written first, then katuns, etc... Mayan dates start from 0. Basically the first kin of a uinal is number zero and the last one number 19, twenty in total.
I've first compiled the following:
def mayanDate(date, separation='')
"""
>>> mayanDate('01/01/1970')
'12/17/16/7/5'
>>> mayaDate('20-7-1988', separator='/')
'12/18/15/4/0'
>>> mayaDate('00012+00012+02012', separator='-')
'12-19-19-17-11'
>>> mayaDate('21 12 2012', separator='+')
'13+0+0+0+0'
>>> mayaDate('26.03.2407')
'14.0.0.0.0'
"""
days = daysPassed(date) + 13 * 144000 + 18 * 7200\
+ 17 * 400 + 8 * 20 + 6
baktun = str((days //((20 **3) * 18)) - 1)
days = days % ((20 **3) * 18)
katun = str((days //((20 **2) * 18)) - 1)
days = days % ((20 **2) * 18)
tun = str((days // (20 **2)) - 1)
days = days % (20 **2)
uinal = str((days // 20) - 1)
days = days % 20 - 1
kin = str(days)
mayanlist = [baktun, katun, tun, uinal, kin]
for i in date:
if i.isdigit() == False and separator == '':
separator = i
break
mayandate = separator.join(mayanlist)
return mayandate
For some strange reason only 01/01/1970 returns the correct Mayan long notation, despite counting from the beginning of an entire pictun (7,900 years in length!). For all other dates it seems to advance too fast through the calendar, despite my second auxiliary function returning the correct values (even for millennia into the future).
I wonder where's the mistake. For instance mayanDate('20-7-1988') returns '12-18-15-6-0' instead of '12-18-15-4-0' and mayanDate('21 12 2012') returns '13 0 1 12 0' instead of '13 0 0 0 0'.
The issue you're seeing with the negative 1 kin for date '15/01/1970' is due to the removal of one from every date ordinal during the calculation. Taking x%20 will always return a value between 0 and 19 inclusive. Taking one from the result necessarily shifts this range to -1 to 18 inclusive.
The number added to the result of daysPassed(date) seems to be a conversion of the long form of 1/1/1970, with one added to each digit. I'm assuming this has been done to counter the fact that the Mayan calendar starts counting at zero, but is unnecessary. The Mayan date 0.0.0.0.1.5 counts 25, not 151646. This doesn't appear to be the source of the error though, since removing this issue from my own code, I still get the same results as described for the 20-7-1988 and 21-12-2012.
I finally rooted out the error when I went back and switched out all the magic numbers in my code for named constants (it makes the code far easier to debug, read and maintain). You state there are 18 uinal in a tun, and 20 tun in a katun, but these numbers are reversed in the code.
Here's my code:
def mayanDate(date_str, seperation=','):
days_in_kin = 1
kin_in_uinal = 20
uinal_in_tun = 18
tun_in_katun = 20
katun_in_baktun = 20
days_in_uinal = days_in_kin * kin_in_uinal
days_in_tun = days_in_uinal * uinal_in_tun
days_in_katun = days_in_tun * tun_in_katun
days_in_baktun = days_in_katun * katun_in_baktun
days_1970 = 12 * days_in_baktun \
+ 17 * days_in_katun\
+ 16 * days_in_tun\
+ 7 * days_in_uinal\
+ 5 * days_in_kin
total_days = daysPassed(date_str) + days_1970
baktun = total_days // days_in_baktun
total_days -= baktun * days_in_baktun
katun = total_days // days_in_katun
total_days -= katun * days_in_katun
tun = total_days // days_in_tun
total_days -= tun * days_in_tun
uinal = total_days // days_in_uinal
total_days -= uinal * days_in_uinal
kin = total_days // days_in_kin
print seperation.join(map(str, (baktun, katun, tun, uinal, kin)))
(I subtracted the previous calculations from total days, rather than using a modulo operator, since I feel it's cleaner. I guess it's a matter of personal preference.)
I may have found something.
>>>mayanDate('15/01/1970')
'12/17/16/8/-1'
Obviously not possible. -1 has to be 19 here and 8 has to be 7. It seems to turn month too early. Still not out why 01/01/1970 remains correct here. No idea what's so special about that date.