easy +8 pts

Datetime to Unix timestamp

Convert naive datetime strings with a UTC assumption into Unix timestamps.

Write a function `datetime_to_timestamp(dt_str: str) -> int` that takes a string representing a date and time in the format `YYYY-MM-DD HH:MM:SS` (24-hour clock, no timezone information) and returns the Unix timestamp (seconds since 1970-01-01 00:00:00 UTC) for that datetime, **assuming the given time is in UTC**. The input string will always be valid and in the exact format described. Return the timestamp as an integer.

Constraints

The input string is always in the format `YYYY-MM-DD HH:MM:SS`. The year is between 1970 and 2038 inclusive. The output is an integer. Complexity: O(1) time and space.

Example

>>> datetime_to_timestamp('1970-01-01 00:00:00')
0
>>> datetime_to_timestamp('1970-01-01 00:00:01')
1
>>> datetime_to_timestamp('1970-01-02 00:00:00')
86400
>>> datetime_to_timestamp('2021-01-01 00:00:00')
1609459200
8 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Use `datetime.strptime` to parse the string with the format `'%Y-%m-%d %H:%M:%S'`.
The parsed datetime is naive; attach a UTC timezone using `timezone.utc` or set the `tzinfo`.
Convert to timestamp with `.timestamp()` and return as `int`.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.