5. Timestamp

5.1. What is timestamp?

  • Also called “epoch”

  • Seconds since 1970-01-01T00:00:00.000000+0000

  • Unix era

5.2. datetime

5.2.1. Get timestamp

Code Listing 5.22. Get timestamp
from datetime import datetime

now = datetime.now()
now.timestamp()
# 1544116827.618863

5.2.2. From epoch timestamp

Code Listing 5.23. From epoch timestamp
from datetime import datetime

ts = 267809220

datetime.fromtimestamp(ts)
# datetime.datetime(1978, 6, 27, 15, 27)

5.2.3. From Java Script timestamp

  • JavaScript has timestamp in milliseconds

  • To convert from milliseconds we have to divide by 1000

Code Listing 5.24. From Java Script timestamp
from datetime import datetime

MILLISECONDS = 1000
ts = 267809220000

datetime.fromtimestamp(ts / MILLISECONDS)
# datetime.datetime(1978, 6, 27, 17, 27)

5.3. time

5.3.1. Timestamp with precision

Code Listing 5.25. Get timestamp
import time

time.time()
# 1496737953.0712671

5.4. Assignments

5.4.1. Create datetime object

  • Filename: datetime_create.py

  • Lines of code to write: 1 lines

  • Estimated time of completion: 5 min

  1. Stwórz obiekt datetime z datą “1970-01-01T00:00:00.000000+0000”

  2. Co to za data?