Skip to content
On this page

Timestamps โ€‹

The type of timestamps in SmartPy is sp.TTimestamp.

The corresponding type in Michelson is

Michelson timestamp.

See reference Timestamps template.

Literals โ€‹

sp.timestamp(...)
A literal timestamp is defined by doing sp.timestamp(i) where i is an integer representing the number of seconds since epoch (January 1st 1970)

Example โ€‹

python
timestamp1 = sp.timestamp(0)     # Thu, 01 Jan 1970 00:00:00 GMT
timestamp2 = sp.timestamp(10)    # Thu, 01 Jan 1970 00:00:10 GMT

Global properties โ€‹

Get block timestamp โ€‹

sp.now
The minimal injection time on the stack for the current block/priority. For all reasonable purposes, this is a technical detail and sp.now should be understood as the timestamp of the block whose validation triggered the execution.

Example โ€‹

python
block_time = sp.now
Michelson NOW

Operations โ€‹

Subtraction โ€‹

expr1 - expr2
Subtract two sp.TTimestamp values, expr1 and expr2.

Return the difference in seconds of type sp.TInt between two timestamps.

Example โ€‹

python
value = sp.timestamp(10) - sp.timestamp(2)
Michelson SUB

Add seconds โ€‹

<timestamp>.add_seconds(seconds)
Return a timestamp with seconds added to timestamp, where timestamp must be a sp.TTimestamp and seconds a sp.TInt.

Example โ€‹

python
e.add_seconds(10)
Michelson ADD

Add minutes โ€‹

<timestamp>.add_minutes(minutes)
Return a timestamp with minutes added to timestamp, where timestamp must be a sp.TTimestamp and minutes a sp.TInt.

Example โ€‹

python
e.add_minutes(10)
Michelson ADD

Add hours โ€‹

<timestamp>.add_hours(hours)
Return a timestamp with hours added to timestamp, where timestamp must be a sp.TTimestamp and hours a sp.TInt.

Example โ€‹

python
e.add_hours(1)
Michelson ADD

Add days โ€‹

<timestamp>.add_days(days)
Return a timestamp with days added to timestamp, where timestamp must be a sp.TTimestamp and days a sp.TInt.

Example โ€‹

python
e.add_days(1)
Michelson ADD

Utils (meta-programming) โ€‹

Build a timestamp from UTC โ€‹

sp.timestamp_from_utc(year, month, day, hours, minutes, seconds)
Compute a constant timestamp corresponding to an UTC datetime.

Example โ€‹

python
sp.timestamp_from_utc(2021, 12, 31, 23, 59, 59) # Fri, 31 Dec 2021 23:59:59 GMT

Get current UTC timestamp โ€‹

sp.timestamp_from_utc_now()
Compute a constant timestamp corresponding to now. This is fixed at contract or test evaluation time.

Example โ€‹

python
sp.timestamp_from_utc_now()