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 โ
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 โ
block_time = sp.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 โ
value = sp.timestamp(10) - sp.timestamp(2)
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 โ
e.add_seconds(10)
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 โ
e.add_minutes(10)
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 โ
e.add_hours(1)
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 โ
e.add_days(1)
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 โ
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 โ
sp.timestamp_from_utc_now()