Bahnzo
Bahnzo

Reputation: 88

Python's datetime conversion

Here's my code:

from datetime import datetime

def get_local_time(time_str):
    """
    takes a string in the format of '27 March at 3:00' which is UTC
    and converts it to local time and AM/PM
    :param time_str:
    """
    offset = datetime.now() - datetime.utcnow()
    time_dt = datetime.strptime(time_str, '%d %b at %H:%M')
    return (time_dt + offset).strftime('%I:%M %p')

What I'm having problems with is using a time_str that's only the time and doesn't include the day/month. ie: "02:00"

If I change it to: time_dt = datetime.strptime(time_str, '%H:%M') then I get an error about strftime and years before 1900.

So I'm stumped here. What needs done to allow just a time in the input string?

Upvotes: 3

Views: 288

Answers (2)

Mohammad Yusuf
Mohammad Yusuf

Reputation: 17054

You can try with dateutil package. The parser.parse() method is good when your input string changes. It will construct a datetime object with todays date if only time is specified in the string. It will handle various other formats.

from datetime import datetime
from dateutil import parser

def get_local_time(time_str):
    """
    takes a string in the format of '27 March at 3:00' which is UTC
    and converts it to local time and AM/PM
    :param time_str:
    """
    offset = datetime.now() - datetime.utcnow()
    time_dt = parser.parse(time_str)
    return (time_dt + offset).strftime('%I:%M %p')

If you are restricted to only datetime package you can do something like this:

from datetime import datetime

def get_local_time(time_str):
    """
    takes a string in the format of '27 March at 3:00' which is UTC
    and converts it to local time and AM/PM
    :param time_str:
    """
    if len(time_str) <= 5:
        time_str = datetime.now().strftime('%d %B at ') + time_str
    offset = datetime.now() - datetime.utcnow()
    time_dt = datetime.strptime(time_str, '%d %B at %H:%M')
    return (time_dt + offset).strftime('%I:%M %p')

print get_local_time('27 March at 3:00')
print get_local_time('3:00')

Or you can do it like so:

from datetime import datetime

def get_local_time(time_str):
    """
    takes a string in the format of '27 March at 3:00' which is UTC
    and converts it to local time and AM/PM
    :param time_str:
    """
    offset = datetime.now() - datetime.utcnow()
    if len(time_str) <= 5:
        time_dt = datetime.combine(datetime.now().date(), datetime.strptime(time_str, '%H:%M').time())
    else:
        time_dt = datetime.strptime(time_str, '%d %B at %H:%M')
    return (time_dt + offset).strftime('%I:%M %p')

print get_local_time('27 March at 3:00')
print get_local_time('3:00')

Upvotes: 2

chenjesu
chenjesu

Reputation: 754

I just tried it in the repl. It worked for me:

>>> from datetime import datetime
>>> time = "02:00"
>>> time_dt = datetime.strptime(time, '%H:%M')
>>> time_dt
datetime.datetime(1900, 1, 1, 2, 0)
>>>

If I remember right, a datetime can never store just a time, there will always be a dummy date of January 1, 1900. If you want to store the time without the dummy date, try using the time class. It also has a strftime function, see the docs here: https://docs.python.org/2/library/time.html

You could also try adding a different dummy date to your datetime if it isn't working.

Upvotes: 2

Related Questions