felipearbelaez
Member
I'm trying to use the following Python code to print the closest to the two target times, 7 am or 1 pm. If the current time is past 1 pm, the target time will be 7 am on the following business day. Can I use the 'pytz' module in Pabbly?
import datetime
import pytz
def closest_time(now, time1, time2):
if now < time1:
return time1
elif now < time2:
return time2
else:
return None
def main():
# Set timezone
tz = pytz.timezone("US/Eastern")
# Get current time
now = datetime.datetime.now(tz)
# Check if it's a business day (Monday = 0, Sunday = 6)
if now.weekday() < 5:
# Define target times: 7 AM and 1 PM
time1 = now.replace(hour=7, minute=0, second=0, microsecond=0)
time2 = now.replace(hour=13, minute=0, second=0, microsecond=0)
# Get the closest target time
target_time = closest_time(now, time1, time2)
if target_time:
print(f"Closest target time: {target_time.strftime('%Y-%m-%d %H:%M:%S')}")
else:
# If it's past 1 PM on Friday, set the target time to 7 AM on the following Monday
if now.weekday() == 4:
next_monday = now + datetime.timedelta(days=(7 - now.weekday()))
next_monday_time1 = next_monday.replace(hour=7, minute=0, second=0, microsecond=0)
print(f"Closest target time: {next_monday_time1.strftime('%Y-%m-%d %H:%M:%S')}")
# If it's past 1 PM on any other business day, set the target time to 7 AM the following day
else:
next_day = now + datetime.timedelta(days=1)
next_day_time1 = next_day.replace(hour=7, minute=0, second=0, microsecond=0)
print(f"Closest target time: {next_day_time1.strftime('%Y-%m-%d %H:%M:%S')}")
# If it's Saturday or Sunday, set the target time to 7 AM on the following Monday
else:
next_monday = now + datetime.timedelta(days=(7 - now.weekday()))
next_monday_time1 = next_monday.replace(hour=7, minute=0, second=0, microsecond=0)
print(f"Closest target time: {next_monday_time1.strftime('%Y-%m-%d %H:%M:%S')}")
if __name__ == "__main__":
main()
import datetime
import pytz
def closest_time(now, time1, time2):
if now < time1:
return time1
elif now < time2:
return time2
else:
return None
def main():
# Set timezone
tz = pytz.timezone("US/Eastern")
# Get current time
now = datetime.datetime.now(tz)
# Check if it's a business day (Monday = 0, Sunday = 6)
if now.weekday() < 5:
# Define target times: 7 AM and 1 PM
time1 = now.replace(hour=7, minute=0, second=0, microsecond=0)
time2 = now.replace(hour=13, minute=0, second=0, microsecond=0)
# Get the closest target time
target_time = closest_time(now, time1, time2)
if target_time:
print(f"Closest target time: {target_time.strftime('%Y-%m-%d %H:%M:%S')}")
else:
# If it's past 1 PM on Friday, set the target time to 7 AM on the following Monday
if now.weekday() == 4:
next_monday = now + datetime.timedelta(days=(7 - now.weekday()))
next_monday_time1 = next_monday.replace(hour=7, minute=0, second=0, microsecond=0)
print(f"Closest target time: {next_monday_time1.strftime('%Y-%m-%d %H:%M:%S')}")
# If it's past 1 PM on any other business day, set the target time to 7 AM the following day
else:
next_day = now + datetime.timedelta(days=1)
next_day_time1 = next_day.replace(hour=7, minute=0, second=0, microsecond=0)
print(f"Closest target time: {next_day_time1.strftime('%Y-%m-%d %H:%M:%S')}")
# If it's Saturday or Sunday, set the target time to 7 AM on the following Monday
else:
next_monday = now + datetime.timedelta(days=(7 - now.weekday()))
next_monday_time1 = next_monday.replace(hour=7, minute=0, second=0, microsecond=0)
print(f"Closest target time: {next_monday_time1.strftime('%Y-%m-%d %H:%M:%S')}")
if __name__ == "__main__":
main()