I'm attempting to convert a given string to Unix time. The string always has a time in it and optionally has a date with it as well. For example, 12/31/15 11:59PM
, 12/31/15 11:59
, and 11:59
are strings that I can expect to get.
Using the following, any of those strings gets converted correctly:
from dateutil import parser
import time
timezone = "12/31/15 11:59"
target = time.mktime(parser.parse(timestamp).timetuple())
However, if given a timezone as well, e.g. 12/31/15 11:59PM PST
, the timezone gets dropped by timetuple()
and while the conversion works, it will still give the same result as if the timezone weren't in the string (and thus is correct only for the local time of the system).
I haven't found an elegant way to 1) properly convert the string to the appropriate timezone when a timezone is given and at the same time 2) allow for the timezone string to be present, or if missing just assume local timezone.