2

Let's say, there's a String --->

"I eat potato at 5:30 PM"

So, what I want to do is to capture the time 5:30 PM from the String, and after it finishes capturing the time (5:30 PM and suppose the timezone is in 'America/New_York' format), it'll convert it to a different timezone (Suppose in 'UTC' format).

How can I do that in Python?

Red
  • 26,798
  • 7
  • 36
  • 58

6 Answers6

3

You can use the pytz module:

import pytz, datetime
import re

s = "I eat potato at 5:30 PM"
d = re.findall('[\d ]\d:\d\d \w\w', s)[0].strip()

utc_dt = pytz.timezone("America/New_York").localize(datetime.datetime.strptime(d, "%H:%M %p"), is_dst=None).astimezone(pytz.utc)
print(f'{utc_dt.hour}:{utc_dt.minute}')

Output:

10:26
Red
  • 26,798
  • 7
  • 36
  • 58
1

For capturing time in text, you can use regular expressions with the re module. (https://docs.python.org/3/library/re.html)

For operations of date and time, use datetime module. (https://docs.python.org/3/library/datetime.html#timezone-objects)

Alanp
  • 37
  • 5
  • 1
    you could add [zoneinfo](https://docs.python.org/3/library/zoneinfo.html) for handling time zones (pytz is outdated since this was added to the standard lib with Python 3.9). – FObersteiner Nov 06 '20 at 06:41
1
from datetime import datetime
import pytz

utc = pytz.utc
eastern = pytz.timezone('US/Eastern')

# Using datetime1 from the question
datetime1 = datetime.strptime(somestring, "%Y-%m-%dT%H:%M:%S")

# First, tell Python what timezone that string was in (you said Eastern)
eastern_time = eastern.localize(datetime1)

# Then convert it from Eastern to UTC
utc_time = eastern_time.astimezone(utc)
Serial Lazer
  • 1,667
  • 1
  • 7
  • 15
1

Use pytz.datetime:

from datetime import datetime
import pytz
utc = pytz.utc
eastern = pytz.timezone('US/Eastern')
datetime1 = datetime.strptime(somestring, "%Y-%m-%dT%H:%M:%S")
easternTime = eastern.localize(datetime1)
utcTime = easternTime.astimezone(utc)
1

I guess you can always assume that the time you are trying to capture is in this format.

HH:MM PM/AM

So you can do something like

example_str = "I eat potato at 5:30 PM"

def extract_time(str):
    str_lst = str.split()
    for i, elem in enumerate(str_lst):
        if elem == "PM":
            hour = int(str_lst[i-1].split(":")[0]) + 12
            min = int(str_lst[i-1].split(":")[1])
            return hour, min
        if elem == "AM":
            hour = int(str_lst[i-1].split(":")[0])
            min = int(str_lst[i-1].split(":")[1])
            return hour, min
    return None
            

If you do

extract_time(example_str)

it would return (17, 30).

Now you can use the examples here Python Timezone conversion to convert the timezone.

DKDK
  • 302
  • 2
  • 10
1

Since the question is tagged pandas, I assume the strings with the time in it are part of a pd.Series or pd.DataFrame. Also note that you cannot associate a time to a time zone unambiguously if you don't have a date - just think about DST.

Similar to the other answers, in pandas you would extract time with a regex, parse to datetime and associate / change time zone / UTC:

import pandas as pd

df = pd.DataFrame({'strings':["I eat potato at 5:30 PM"]})

# add a date,
# extract the time with a suitable regex,
# parse to datetime
date = '2020-11-06 '
df['USEastern_dt'] = pd.to_datetime(date + df['strings'].str.extract('(\d{1,2}\:\d{2}\ [A-P]{2})')[0])

# localize to US/Eastern
df['USEastern_dt'] = df['USEastern_dt'].dt.tz_localize('US/Eastern')

# convert to UTC
df['UTC_dt'] = df['USEastern_dt'].dt.tz_convert('UTC')

...which would give you

df['UTC_dt']
0   2020-11-06 22:30:00+00:00
Name: UTC_dt, dtype: datetime64[ns, UTC]
FObersteiner
  • 22,500
  • 8
  • 42
  • 72