Convert odd timestamp with offset to UNIX time in Python [duplicate] - python

This question already has an answer here:
Converting string with UTC offset to a datetime object [duplicate]
(1 answer)
Closed 9 years ago.
I have a timestamp string from a web log that looks like this:
10/Jun/2005:05:59:05 -0500
It like to convert it to a UNIX timestamp.
A datetime can be converted with time.mktime(datetime.timetuple())
According to the datetime docs, datetime.strptime() should convert it to a datetime:
from datetime import datetime
datetime.strptime("10/Jun/2005:05:59:05 -0500","%d/%b/%Y:%H:%M:%S %z")
At least on with Python 2.7.2 on my Mac, this results in
ValueError: 'z' is a bad directive in format '%d/%b/%Y:%H:%M:%S %z'
After reading many questions on SO about that error, I decided to try python-dateutil:
from dateutil import parser
parser.parse("10/Jun/2005:05:59:05 -0500")
That didn't work either:
ValueError: unknown string format
Now what?

You can use dateutils to make the converstion, you will need two steps:
>>> import calendar
>>> from dateutil.parser import parse
>>> d = parse('10/Jun/2005:05:59:05 -0500', fuzzy=True)
This will create a datetime object
>>> d
datetime.datetime(2005, 6, 10, 5, 59, 5, tzinfo=tzoffset(None, -18000))
And to convert it to UNIX timestamp:
>>> ts = calendar.timegm(d.utctimetuple())
>>> print ts
1118401145

Related

How do I parse timestamp strings which contain BST (British Summer Time) into a timezone aware datetime timestamp? [duplicate]

This question already has answers here:
Python datetime strptime() does not match format
(4 answers)
Closed 1 year ago.
I have timestamps of the format "25-Oct-20 1:00:00 AM GMT" and "25-Oct-20 12:00:00 AM BST" and I wish to parse them into a timezone aware datetime. I have tried using the strptime format "%d-%b-%y %I:%M:%S %p %Z"
However the %Z argument of datetime.strptime does not recognize BST as a valid input, and I can not figure out the "correct" way to handle parsing daylight savings times.
If you look at the error from dateutil, it says something like:
UnknownTimezoneWarning: tzname BST identified but not understood.
Pass `tzinfos` argument in order to correctly return a timezone-aware
datetime. In a future version, this will raise an exception.
In other words, you can pass in a dictionary that maps timezone aliases (like "BST") to appropriate timezone information. Maybe something like:
>>> import dateutil.parser
>>> import dateutil.tz
>>> BST = dateutil.tz.gettz('Europe/London')
>>> dateutil.parser.parse('25-Oct-20 12:00:00 AM BST', tzinfos={'BST': BST})
datetime.datetime(2020, 10, 25, 0, 0, tzinfo=tzfile('/usr/share/zoneinfo/Europe/London'))

How to convert time to T Z format in python [duplicate]

This question already has answers here:
Generate RFC 3339 timestamp in Python [duplicate]
(7 answers)
Closed 2 years ago.
I would like to convert today's date to below format in python
What I tried:
>>> import datetime
>>> d_date = datetime.datetime.now()
>>> reg_format_date = d_date.strftime("%Y-%m-%d %I:%M:%S %p")
>>> print(reg_format_date)
2020-08-04 06:40:52 PM
Expected format:
2017-10-18T04:46:53.553472514Z
can some one suggest please
Use utcnow() instead of now() to get the UTC time.
Use the isoformat() method. You'll need to add the trailing "Z" yourself.
In summary:
from datetime import datetime
reg_format_date = datetime.utcnow().isoformat() + "Z"
Here's how to get the current UTC time and convert to the ISO-8601 format (which is what your example shows). The timezone is hardcoded to Z.
import datetime
datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None).isoformat() + 'Z'

Convert string datetime to timestamp and vice verse in python [duplicate]

This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Closed 8 years ago.
I have a date stored as a string:-
16/07/2014 13:00:00
I want to convert this into timestamp.
Also from timestamp to this format again.
Please suggest the best possible way to do this in python.
You can use datetime to handle combined dates and times. You could parse this string using datetime.strptime but then you'd have to manually select the formatting.
Alternatively you can use the dateutil package which has a parser which can intelligently guess the string format and return a datetime object, as shown below:
from dateutil import parser
s = '16/07/2014 13:00:00'
d = parser.parse(s)
print(d)
# 2014-07-16 13:00:00
print(type(d))
# datetime.datetime
The documentation to look into this deeper is here
The functions you are looking for are time.strptime(string[, format]) to go from string to timestamp, and then from timestamp to string is time.strftime(format[, t])
Here is an example for your format:
>>> from datetime import datetime
>>>
>>> date_object = datetime.strptime('16/07/2014 13:00:00', '%d/%m/%Y %H:%M:%S')
>>> print date_object
2014-07-16 13:00:00
The to go back to your format (I have used gmtime() to get the current time to show you can convert any datetime to your desired format)
>>> from time import gmtime, strftime
>>> date_string = strftime("%d/%m/%Y %H:%M:%S", gmtime())
>>> print date_string
17/09/2014 09:31:00
Your best bet is the datetime library: https://docs.python.org/2/library/datetime.html
import datetime
mytime='16/07/2014 13:00:00'
pythontime=datetime.datetime.strptime(mytime, '%d/%m/%Y %H:%M:%S')
stringtime=pythontime.strftime('%d/%m/%Y %H:%M:%S')
Enjoy!

How to convert a timezone aware string to datetime in Python without dateutil?

I have to convert a timezone-aware string like "2012-11-01T04:16:13-04:00" to a Python datetime object.
I saw the dateutil module which has a parse function, but I don't really want to use it as it adds a dependency.
So how can I do it? I have tried something like the following, but with no luck.
datetime.datetime.strptime("2012-11-01T04:16:13-04:00", "%Y-%m-%dT%H:%M:%S%Z")
As of Python 3.7, datetime.datetime.fromisoformat() can handle your format:
>>> import datetime
>>> datetime.datetime.fromisoformat('2012-11-01T04:16:13-04:00')
datetime.datetime(2012, 11, 1, 4, 16, 13, tzinfo=datetime.timezone(datetime.timedelta(days=-1, seconds=72000)))
In older Python versions you can't, not without a whole lot of painstaking manual timezone defining.
Python does not include a timezone database, because it would be outdated too quickly. Instead, Python relies on external libraries, which can have a far faster release cycle, to provide properly configured timezones for you.
As a side-effect, this means that timezone parsing also needs to be an external library. If dateutil is too heavy-weight for you, use iso8601 instead, it'll parse your specific format just fine:
>>> import iso8601
>>> iso8601.parse_date('2012-11-01T04:16:13-04:00')
datetime.datetime(2012, 11, 1, 4, 16, 13, tzinfo=<FixedOffset '-04:00'>)
iso8601 is a whopping 4KB small. Compare that tot python-dateutil's 148KB.
As of Python 3.2 Python can handle simple offset-based timezones, and %z will parse -hhmm and +hhmm timezone offsets in a timestamp. That means that for a ISO 8601 timestamp you'd have to remove the : in the timezone:
>>> from datetime import datetime
>>> iso_ts = '2012-11-01T04:16:13-04:00'
>>> datetime.strptime(''.join(iso_ts.rsplit(':', 1)), '%Y-%m-%dT%H:%M:%S%z')
datetime.datetime(2012, 11, 1, 4, 16, 13, tzinfo=datetime.timezone(datetime.timedelta(-1, 72000)))
The lack of proper ISO 8601 parsing is being tracked in Python issue 15873.
Here is the Python Doc for datetime object using dateutil package..
from dateutil.parser import parse
get_date_obj = parse("2012-11-01T04:16:13-04:00")
print get_date_obj
There are two issues with the code in the original question: there should not be a : in the timezone and the format string for "timezone as an offset" is lower case %z not upper %Z.
This works for me in Python v3.6
>>> from datetime import datetime
>>> t = datetime.strptime("2012-11-01T04:16:13-0400", "%Y-%m-%dT%H:%M:%S%z")
>>> print(t)
2012-11-01 04:16:13-04:00
You can convert like this.
date = datetime.datetime.strptime('2019-3-16T5-49-52-595Z','%Y-%m-%dT%H-%M-%S-%f%z')
date_time = date.strftime('%Y-%m-%dT%H:%M:%S.%fZ')
You can create a timezone unaware object and replace the tzinfo and make it a timezone aware DateTime object later.
from datetime import datetime
import pytz
unware_time = datetime.strptime("2012-11-01 04:16:13", "%Y-%m-%d %H:%M:%S")
aware_time = unaware_time.replace(tzinfo=pytz.UTC)
I'm new to Python, but found a way to convert
2017-05-27T07:20:18.000-04:00
to
2017-05-27T07:20:18 without downloading new utilities.
from datetime import datetime, timedelta
time_zone1 = int("2017-05-27T07:20:18.000-04:00"[-6:][:3])
>>returns -04
item_date = datetime.strptime("2017-05-27T07:20:18.000-04:00".replace(".000", "")[:-6], "%Y-%m-%dT%H:%M:%S") + timedelta(hours=-time_zone1)
I'm sure there are better ways to do this without slicing up the string so much, but this got the job done.
This suggestion for using dateutil by Mohideen bin Mohammed definitely is the best solution even if it does a require a small library. having used the other approaches there prone to various forms of failure. Here's a nice function for this.
from dateutil.parser import parse
def parse_date_convert(date, fmt=None):
if fmt is None:
fmt = '%Y-%m-%d %H:%M:%S' # Defaults to : 2022-08-31 07:47:30
get_date_obj = parse(str(date))
return str(get_date_obj.strftime(fmt))
dates = ['2022-08-31T07:47:30Z','2022-08-31T07:47:29.098Z','2017-05-27T07:20:18.000-04:00','2012-11-01T04:16:13-04:00']
for date in dates:
print(f'Before: {date} After: {parse_date_convert(date)}')
Results:
Before: 2022-08-31T07:47:30Z After: 2022-08-31 07:47:30
Before: 2022-08-31T07:47:29.098Z After: 2022-08-31 07:47:29
Before: 2017-05-27T07:20:18.000-04:00 After: 2017-05-27 07:20:18
Before: 2012-11-01T04:16:13-04:00 After: 2012-11-01 04:16:13
Having tried various forms such as slicing split replacing the T Z like this:
dates = ['2022-08-31T07:47:30Z','2022-08-31T07:47:29.098Z','2017-05-27T07:20:18.000-04:00','2012-11-01T04:16:13-04:00']
for date in dates:
print(f'Before: {date} After: {date.replace("T", " ").replace("Z", "")}')
You still are left with subpar results. like the below
Before: 2022-08-31T07:47:30Z After: 2022-08-31 07:47:30
Before: 2022-08-31T07:47:29.098Z After: 2022-08-31 07:47:29.098
Before: 2017-05-27T07:20:18.000-04:00 After: 2017-05-27 07:20:18.000-04:00
Before: 2012-11-01T04:16:13-04:00 After: 2012-11-01 04:16:13-04:00

Lets say I have a string...how do I convert that to a datetime? [duplicate]

This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Closed 8 years ago.
s = "June 19, 2010"
How do I conver that to a datetime object?
There's also the very good dateutil library, that can parse also stranger cases:
from dateutil.parsers import parse
d = parse(s)
Use datetime.strptime. It takes the string to convert and a format code as arguments. The format code depends on the format of the string you want to convert, of course; details are in the documentation.
For the example in the question, you could do this:
from datetime import datetime
d = datetime.strptime(s, '%B %d, %Y')
As of python 2.5 you have the method datetime.strptime():
http://docs.python.org/library/datetime.html
dt = datetime.strptime("June 19, 2010", "%B %d, %Y")
if your locale is EN.
Use datetime.datetime.strptime:
>>> import datetime
>>> s = "June 19, 2010"
>>> datetime.datetime.strptime(s,"%B %d, %Y")
datetime.datetime(2010, 6, 19, 0, 0)

Categories

Resources