4. Timestamp

4.1. What is timestamp?

  • Also called "epoch"

  • Unix era

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

  • max 32-bit integer is 2,147,483,647

  • If you add +1 to that, you get -2,147,483,647

  • 2,147,483,647 seconds from 01-01-1970 00:00:00 is January 19, 2038

  • If you add one more second, you get a date somewhere in 1902

4.2. datetime

4.2.1. Get timestamp

Listing 188. Get timestamp
from datetime import datetime

datetime.now().timestamp()
# 1544116827.618863

4.2.2. From epoch timestamp

Listing 189. From epoch timestamp
from datetime import datetime

ts = 267809220

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

4.2.3. From Java Script timestamp

  • JavaScript has timestamp in milliseconds

  • To convert from milliseconds we have to divide by 1000

Listing 190. From Java Script timestamp
from datetime import datetime

MILLISECONDS = 1000
ts = 267809220000

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

4.3. time

4.3.1. Timestamp with precision

Listing 191. Get timestamp
import time

time.time()
# 1496737953.0712671

4.4. Assignments

4.4.1. Create datetime object

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

  2. Przedstaw datę jako timestamp

  3. Co wyszło?

  4. Co to za data?