easy +10 pts

Unix timestamp to datetime

Convert a Unix timestamp (seconds since 1970-01-01 UTC) into a formatted 'YYYY-MM-DD HH:MM:SS' string in UTC.

Write a function `unix_to_datetime(ts: int) -> str` that takes a Unix timestamp (the number of seconds elapsed since 1970-01-01 00:00:00 UTC) and returns the corresponding date and time as a string in the format `'YYYY-MM-DD HH:MM:SS'` in UTC. Use Python's `datetime` module. Do not adjust for timezones; the returned string must always be in UTC. Example: `unix_to_datetime(0)` should return `'1970-01-01 00:00:00'`.

Constraints

- `0 <= ts <= 9,999,999,999` (up to the year 2286) - The input will always be a non-negative integer. - Your solution must not rely on external libraries.

Example

>>> unix_to_datetime(0)
'1970-01-01 00:00:00'
>>> unix_to_datetime(1609459200)
'2021-01-01 00:00:00'
>>> unix_to_datetime(1234567890)
'2009-02-13 23:31:30'
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Use `datetime.datetime.fromtimestamp(ts, tz=datetime.timezone.utc)` to get a timezone-aware datetime.
Use the `.strftime('%Y-%m-%d %H:%M:%S')` method to format the datetime object.
Remember that the timestamp is in seconds, not milliseconds.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.