Input any time, output timezones that are currently that time I made a program that will take in a time and a weekday, and return which parts of the
world are at times closest to the one specified. Part of the program focuses on extracting a date and country out of a user input.
Here are some example inputs and outputs:
&quot;In what part of the world is it 4:00 PM Friday right now?&quot; (datetime.datetime(2021, 1, 1, 16, 0), 'World')
&quot;Where in America is it 6:40 AM Thu?&quot;                       (datetime.datetime(2020, 12, 31, 6, 40), 'America')
&quot;It is 10:30 pm fri in which part of Asia?&quot;                 (datetime.datetime(2021, 1, 1, 22, 30), 'Asia')
&quot;Where would the time be 10:10 AM Sat?&quot;                     (datetime.datetime(2021, 1, 2, 10, 10), 'World')
&quot;Which state of brazil has the time 8:30 AM Saturday now?&quot;  (datetime.datetime(2021, 1, 2, 8, 30), 'Brazil')

The reason that the only part of the date that needs to be given is the weekday,
is because the maximum possible days in difference between a timezone and the timezone
the user is currently at is one day, either ahead or behind.
So the program checks if the weekday given is at the current day, one day before the current day, or one day after the current day, and can return the rest of the date.
Here is my code:
import pytz
from datetime import datetime, timedelta
import re

def return_words(string):
    return re.findall(r'\b\S+\b', string.lower())

def string_to_datetime(string):
    weekdays = [&quot;sunday&quot;, &quot;monday&quot;, &quot;tuesday&quot;, &quot;wednesday&quot;, &quot;thursday&quot;, &quot;friday&quot;, &quot;saturday&quot;]
    string = string.lower()
    words = sorted(return_words(string), key=len)[::-1]
    countries = [t.split(&quot;/&quot;)[0] for t in pytz.all_timezones]

    for word in words:
        in_day = [weekday for weekday in weekdays if word in weekday]
        if len(in_day) == 1: # Make sure that the word found is a weekday name, not just something like &quot;day&quot;
            weekday = in_day[0]
            break
    else:
        return
    
    for word in words:
        if word.title() in countries:
            country = word.title()
            break
    else:
        country = &quot;World&quot; # If no country name found, use &quot;World&quot;
            
    date = datetime.now()
    one_day = timedelta(days=1)
    if (datetime.now() + one_day).strftime(&quot;%A&quot;).lower() == weekday:
        date += one_day
    elif (datetime.now() - one_day).strftime(&quot;%A&quot;).lower() == weekday:
        date -= one_day
    elif datetime.now().strftime(&quot;%A&quot;).lower() != weekday:
        return
    
    year, month, day = date.year, date.month, date.day
    hours_minutes = re.findall(&quot;\d+\:\d+&quot;, string)[0]
    
    half = re.findall(f&quot;(?&lt;={hours_minutes}).{{0,3}}[ap]m&quot;, string) # Play it safe, and only extract the am or pm that is at most 3 steps away from the hours:minutes
    if half: # If found an am or pm
        half = half[0].strip()
    else: # If not found when playing it safe, resort to finding any am or pm in the string
        half = re.findall(&quot;[ap]m&quot;, string)[0].strip()
    string = f&quot;{year} {month} {day} {hours_minutes} {half}&quot;
    try:
        return datetime.strptime(string, &quot;%Y %m %d %I:%M %p&quot;), country
    except ValueError:
        return

while True:
    print(string_to_datetime(input(&quot;Input your string &gt;&gt;&gt; &quot;)))

Can you show me how to improve the efficiency of my code? Also, any tips on tidying up would be of big help.
in_day = [weekday for weekday in weekdays if word in weekday]
    if len(in_day) == 1:

seems odd. You make the entire list, put it in memory, take its length and then throw it away. Consider instead
n_matching = sum(1 for weekday in weekdays if word in weekday)
if n_matching == 1:

This loop:
for word in words:
    in_day = [weekday for weekday in weekdays if word in weekday]
    if len(in_day) == 1: # Make sure that the word found is a weekday name, not just something like &quot;day&quot;
        weekday = in_day[0]
        break

can be simplified if you use set operations; lower everything in words before this:
countries = {t.split(&quot;/&quot;, 1)[0].lower() for t in pytz.all_timezones}
try:
    country = next(iter(countries &amp; set(words))).title()
except StopIteration:
    country = &quot;World&quot; # If no country name found, use &quot;World&quot;