Last active
June 14, 2024 20:16
-
-
Save ydm/dba727d2f1bb9fafac9437b63024efe0 to your computer and use it in GitHub Desktop.
Converts the default golang format of time.Time to a Python datetime.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://gist.github.com/ydm/dba727d2f1bb9fafac9437b63024efe0 | |
from datetime import datetime, timedelta | |
import pytz | |
def convert_golang_time(s): | |
''' | |
>>> convert_golang_time('2024-06-14 22:40:35.106900879 +0300 EEST m=+8.946057668') | |
'2024-06-14T22:40:35.106901+03:00' | |
''' | |
a, b = s.split(' +') | |
f = round(int(a[20:].ljust(9, '0')) / 1000) | |
dt = datetime.strptime('{}.{:06}'.format(a[:19], f), '%Y-%m-%d %H:%M:%S.%f') | |
tz = timedelta(hours=int(b[0:2]), minutes=int(b[2:4])) | |
aw = dt.replace(tzinfo=pytz.FixedOffset(tz.total_seconds() / 60)) | |
return aw |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment