born2Learn
born2Learn

Reputation: 1403

Converting 12 hour format time string (with am/pm) into UTC in 24 hour format

I have time string 11:15am or 11:15pm. I am trying to convert this string into UTC timezone with 24 hour format.

FROM EST to UTC

For example: When I pass 11:15am It should convert into 15:15 and when I pass 11:15pm then it should convert to 3:15.

I have this code which I am trying:

def appointment_time_string(time_str):
    import datetime
    a = time_str.split()[0]

    # b =  re.findall(r"[^\W\d_]+|\d+",a)

    # c = str(int(b[0]) + 4) + ":" + b[1]
    # print("c", c)



    in_time = datetime.datetime.strptime(a,'%I:%M%p')

    print("In Time", in_time)

    start_time = str(datetime.datetime.strftime(in_time, "%H:%M:%S"))

    print("Start TIme", start_time)

    if time_str.split()[3] == 'Today,':
        start_date = datetime.datetime.utcnow().strftime("%Y-%m-%dT")
    elif time_str.split()[3] == 'Tomorrow,':
        today = datetime.date.today( )
        start_date = (today + datetime.timedelta(days=1)).strftime("%Y-%m-%dT")

    appointment_time = str(start_date) + str(start_time)

    return appointment_time

x = appointment_time_string(time_str)
print("x", x)

But this is just converting to 24 hour not to UTC.

Upvotes: 1

Views: 1706

Answers (2)

born2Learn
born2Learn

Reputation: 1403

Developed the following script using provided options/solutions to satisfy my requirement.

def appointment_time_string(time_str):

    import datetime
    import pytz

    a = time_str.split()[0]

    in_time = datetime.datetime.strptime(a,'%I:%M%p')

    start_time = str(datetime.datetime.strftime(in_time, "%H:%M:%S"))

    if time_str.split()[3] == 'Today,':
        start_date = datetime.datetime.utcnow().strftime("%Y-%m-%d")
    elif time_str.split()[3] == 'Tomorrow,':
        today = datetime.date.today( )
        start_date = (today + datetime.timedelta(days=1)).strftime("%Y-%m-%d")

    appointment_time = str(start_date) + " " + str(start_time)

    # print("Provided Time", appointment_time)

    utc=pytz.utc
    eastern=pytz.timezone('US/Eastern')
    fmt='%Y-%m-%dT%H:%M:%SZ'
    # testeddate = '2016-09-14 22:30:00'

    test_date = appointment_time

    dt_obj = datetime.datetime.strptime(test_date,'%Y-%m-%d %H:%M:%S')
    dt_str = datetime.datetime.strftime(dt_obj, '%m/%d/%Y %H:%M:%S')
    date=datetime.datetime.strptime(dt_str,"%m/%d/%Y %H:%M:%S")
    date_eastern=eastern.localize(date,is_dst=None)
    date_utc=date_eastern.astimezone(utc)

    # print("Required Time", date_utc.strftime(fmt))

    return date_utc.strftime(fmt)

Upvotes: 0

Moinuddin Quadri
Moinuddin Quadri

Reputation: 48067

To convert the time from 12 hours to 24 hours format, you may use below code:

from datetime import datetime
new_time = datetime.strptime('11:15pm', '%I:%M%p').strftime("%H:%M")
# new_time: '23:15'

In order to convert time from EST to UTC, the most reliable way is to use third party library pytz. Refer How to convert EST/EDT to GMT? for more details

Upvotes: 2

Related Questions