Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I have a datetime string 2020-10-23T11:50:19+00:00. I can parse it without a timezone as:

>>> datetime.strptime('2020-10-23T11:50:19', '%Y-%m-%dT%H:%M:%S')

datetime.datetime(2020, 10, 23, 11, 50, 19)

But I'm having trouble parsing it with a 00:00 version of a timezone. What would be a correct way to do that?

>>> datetime.strptime('2020-10-23T11:50:19+00:00', '%Y-%m-%dT%H:%M:%S+???')

 

2 Answers

0 votes
by (36.8k points)

You're looking for %z:

>>> datetime.strptime('2020-10-23T11:50:19+00:00', '%Y-%m-%dT%H:%M:%S%z')

datetime.datetime(2020, 10, 23, 11, 50, 19, tzinfo=datetime.timezone.utc)

Beware of some Python version compatibility notes:

Changed in version 3.7: When the %z directive is provided to strptime() method, the UTC offsets can have a colon as a separator between hours, minutes, and seconds. For example, '+01:00:00' will be parsed as an offset of one hour. In addition, providing 'Z' is identical to '+00:00'.

More robust approach, it's not strptime, but it's still in stdlib since Python 3.7:

>>> datetime.fromisoformat('2020-10-23T11:50:19+00:00')

datetime.datetime(2020, 10, 23, 11, 50, 19, tzinfo=datetime.timezone.utc)

#As documented this function supports strings in the format:

YYYY-MM-DD[*HH[:MM[:SS[.fff[fff]]]][+HH:MM[:SS[.ffffff]]]]

where * can match any single character (not just a T).

If you want to know more about the Data Science then do check out the following Data Science which will help you in understanding Data Science from scratch.

0 votes
by (3.1k points)

To give you a better idea of how to use strptime to parse a datetime string with an offset for the timezone in Python, use the datetime module. A simple example follows:


from datetime import datetime


# Datetime string with timezone offset
datetime_str = '2023-10-29 15:30:00 -0400'


# Format, with the timezone offset
format_str = '%Y-%m-%d %H:%M:%S %z'


# Parse a datetime string into a datetime object using strptime function
parsed_datetime = datetime.strptime(datetime_str, format_str)


Print out the parsed datetime and its details of tz_info
```
print("Parsed datetime:",parsed_datetime)
print("Timezone offset:",parsed_datetime.tzinfo)
Change to UTC
This only to make current date-time into UTC.

utc_datetime=parsed_datetime.astimezone(datetime.timezone.utc)
print("UTC datetime:", utc_datetime)
```
Import the module which one would need to run on that application: The provided data type which is supported here is like this; the data type object which has the datetime module.

Datetime Str = date_str holds sample string contains both date time and a tz information that it has which used the minus sign here indicates its is minus ( negative 4 hours or four hour's behind ).

Specify the Format String: the format string '%Y-%m-%d %H:%M:%S %z' specifies the structure for the datetime string, indicating the timezone offset.

Use strptime to Parse a String: strptime converts this string into a datetime.

Print Results: The datetimes and their timezone knowledge are printed. Lastly, it shows how to convert from the datetime to UTC with astimezone.

1.2k questions

2.7k answers

501 comments

693 users

Browse Categories

...