8.8. Datetime Timestamp
8.8.1. What is timestamp?
Seconds since midnight of January 1st, 1970 (1970-01-01 00:00:00 UTC)
Unix era, also known as "epoch"
In most systems represented as 32-bit integer
Max value is 2,147,483,647 (2038-01-19 03:14:07 UTC)
Min value is -2,147,483,647 (1901-12-13 20:45:52 UTC)
If you add 1 to max value, you will get overflow to min value
Linux kernel 5.6 (released 29 March 2020) has a fix for this problem so that 32-bit systems can run beyond the year 2038
https://lore.kernel.org/lkml/CAHk-=wi9ZT7Stg-uSpX0UWQzam6OP9Jzz6Xu1CkYu1cicpD5OA@mail.gmail.com/
Excel error: https://learn.microsoft.com/en-US/office/troubleshoot/excel/wrongly-assumes-1900-is-leap-year
8.8.2. Get current timestamp
Get current timestamp using datetime
module:
>>> from datetime import datetime
>>>
>>>
>>> current_timestamp = datetime.now().timestamp()
Get current timestamp using time
module:
>>> import time
>>>
>>>
>>> current_timestamp = time.time()
8.8.3. Convert timestamp to datetime
Convert timestamp to datetime
:
>>> from datetime import datetime
>>>
>>>
>>> datetime.fromtimestamp(267809220)
datetime.datetime(1978, 6, 27, 17, 27)
JavaScript has timestamp in milliseconds
To convert from milliseconds we have to divide by 1000
Convert JavaScript timestamp to datetime
:
>>> from datetime import datetime
>>>
>>> MILLISECONDS = 1000
>>>
>>> datetime.fromtimestamp(267809220000 / MILLISECONDS)
datetime.datetime(1978, 6, 27, 17, 27)
8.8.4. Assignments
# %% About
# - Name: Datetime Timestamp FromTimestamp
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% English
# 1. Convert timestamp `DATA` to `datetime` object
# 2. Use UTC timezone (pass `tz=timezone.utc` to `fromtimestamp()`)
# 3. Define variable `result: datetime` with the result
# 4. Run doctests - all must succeed
# %% Polish
# 1. Przekonwertuj timestamp `DATA` do obiektu `datetime`
# 2. Użyj strefy czasowej UTC (podaj `tz=timezone.utc` do `fromtimestamp()`)
# 3. Zdefiniuj zmienną `result: datetime` z wynikiem
# 4. Uruchom doctesty - wszystkie muszą się powieść
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> assert type(result) is datetime, \
'Variable `result` has an invalid type; expected: `datetime`.'
>>> result
datetime.datetime(2000, 1, 1, 0, 0, tzinfo=datetime.timezone.utc)
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
from datetime import datetime, timezone
# %% Types
result: datetime
# %% Data
DATA = 946684800.0
# %% Result
result = ...
# %% About
# - Name: Datetime Timestamp ToTimestamp
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% English
# 1. Convert date `DATA` to timestamp
# 2. Define variable `result: float` with the result
# 2. Run doctests - all must succeed
# %% Polish
# 1. Przekonwertuj datę `DATA` do timestamp
# 2. Zdefiniuj zmienną `result: float` z wynikiem
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> assert type(result) is float, \
'Variable `result` has an invalid type; expected: `float`.'
>>> result
946684800.0
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
from datetime import datetime, timezone
# %% Types
result: float
# %% Data
DATA = datetime(2000, 1, 1, 0, 0, 0, tzinfo=timezone.utc)
# %% Result
result = ...