5. Timestamp

5.1. datetime

5.1.1. Get timestamp

Code Listing 5.24. Get timestamp
from datetime import datetime


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

5.1.2. From epoch timestamp

Code Listing 5.25. From epoch timestamp
from datetime import datetime


hermaszewski_timestamp = 267809220
# Tuesday, June 27, 1978 17:27:00 GMT+02:00

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

5.1.3. From Java Script timestamp

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


timestamp = 267809220000
# JavaScript has timestamp in miliseconds

MILISECONDS = 1e3
# To convert from miliseconds we have to divide by 1e3 = 1000

datetime.fromtimestamp(timestamp / MILISECONDS)
# datetime.datetime(1978, 6, 27, 17, 27)

5.2. time

5.2.1. Timestamp with precision

Code Listing 5.27. Get timestamp
import time


time.time()
# 1496737953.0712671

time.time()
# 1496737954.3189602