3

I have a timestamp string that looks like this:

2019-02-16T10:41:20.6080000+01:00

I have to parse it to datetime. Because there are 7 instead of 6 digits for microseconds the following format does not match:

timestamp = "2019-03-14T14:37:37.000000+01:00"
parsed_timestamp = datetime.datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%S.%f%z") #ValueError: time data '2019-03-14T14:37:37.0000000+01:00' does not match format '%Y-%m-%dT%H:%M:%S.%f%z'

How can I parse this format?

Kewitschka
  • 1,445
  • 1
  • 21
  • 35

3 Answers3

2

Using dparser:

import dateutil.parser as dparser
dt_1 = '2019-02-16T10:41:20.6080000+01:00'
print("Datetime: {}".format(dparser.parse(dt_1,fuzzy=True)))

OUTPUT:

Datetime: 2019-02-16 10:41:20.608000+01:00

If you want the date component:

print("Date: {}".format(dparser.parse(dt_1,fuzzy=True).date()))

OUTPUT:

Date: 2019-02-16
DirtyBit
  • 16,613
  • 4
  • 34
  • 55
2

Looks like you can use simple string slicing.

Ex:

import datetime
timestamp = "2019-02-16T10:41:20.6080000+01:00"
parsed_timestamp = datetime.datetime.strptime(timestamp[:26], "%Y-%m-%dT%H:%M:%S.%f").date() 

print(parsed_timestamp)

Output:

2019-02-16
Rakesh
  • 81,458
  • 17
  • 76
  • 113
1

There are actually two things wrong with your data: you have seven digits for microseconds, and your timezone has a colon.

I would use a regex to solve this problem:

timestamp = "2019-02-16T10:41:20.6080000+01:00"
cleaned_timestamp = re.sub('(\d{6})\d(\+\d{2})(:)(\d{2})', r'\1\2\4', timestamp)
parsed_timestamp = datetime.datetime.strptime(cleaned_timestamp, "%Y-%m-%dT%H:%M:%S.%f%z")
parsed_timestamp

Output:

datetime.datetime(2019, 2, 16, 10, 41, 20, 608000, tzinfo=datetime.timezone(datetime.timedelta(0, 3600)))
gmds
  • 19,325
  • 4
  • 32
  • 58