Time
add-time
Use add-time
to add a specified number of seconds
to a given time
.
Basic syntax
To add seconds
to a time
, use the following syntax:
(add-time time seconds)
(add-time time seconds)
Arguments
Use the following arguments to specify the time
to which you want to add seconds
using the add-time
Pact function.
Argument | Type | Description |
---|---|---|
time | time | Specifies the time to which you want to add seconds . |
seconds | decimal or integer | Specifies the number of seconds to add to the time . |
Return values
The add-time
function returns the resulting time after adding the specified number of seconds
to the specified time
.
Examples
The following example adds 120 seconds to the specified time "2024-06-22T12:00:00Z" in the Pact REPL:
pact> (add-time (time "2024-06-22T12:00:00Z") 120)"2024-06-22T12:02:00Z"
pact> (add-time (time "2024-06-22T12:00:00Z") 120)"2024-06-22T12:02:00Z"
In most cases, you use the add-time
function in combination with other functions such hours
or minutes
as follows:
pact> (add-time (time "2024-06-22T12:00:00Z") (hours 1))"2024-06-22T13:00:00Z" (add-time (time "2024-06-26T12:00:00Z") (minutes 35))"2024-06-26T12:35:00Z"
pact> (add-time (time "2024-06-22T12:00:00Z") (hours 1))"2024-06-22T13:00:00Z" (add-time (time "2024-06-26T12:00:00Z") (minutes 35))"2024-06-26T12:35:00Z"
days
Use days
to specify a number of days.
You can use this function in conjunction with the add-time
function to add a specified number of days to a specified time.
Basic syntax
To specify a number of days, use the following syntax:
(days n)
(days n)
Arguments
Use the following argument to specify the n
number of days for the days
Pact function.
Argument | Type | Description |
---|---|---|
n | decimal or integer | Specifies the number of days to add. |
Return values
The days
function returns the number of seconds in the given number of days as a decimal value.
Example
The following example demonstrates how to use the days
function to return the number of seconds in one day as a decimal value:
(days 1)86400.0
(days 1)86400.0
The following example demonstrates how to use the days
function in combination with add-time
:
(add-time (time "2024-07-22T12:00:00Z") (days 1))"2024-07-23T12:00:00Z"
(add-time (time "2024-07-22T12:00:00Z") (days 1))"2024-07-23T12:00:00Z"
In this example, (days 1)
adds one day to the specified time "2024-07-22T12:00:00Z" using the add-time
function, enabling straightforward manipulation of time-based information in smart contracts.
diff-time
Use diff-time
to compute the difference between time1
and time2
in seconds.
Basic syntax
To compute the difference between time1
and time2
in seconds, use the following syntax:
(diff-time time1 time2)
(diff-time time1 time2)
Arguments
Use the following arguments to specify the times for the diff-time
Pact function:
Argument | Type | Description |
---|---|---|
time1 | time | Specifies the first time for the calculation. |
time2 | time | Specifies the second time for the calculation. |
Return values
The diff-time
function returns the difference between time1
and time2
in seconds as a decimal.
Examples
The following example demonstrates how to use the diff-time
function to compute the difference between the times "16:00:00" and "09:30:00" in seconds:
pact> (diff-time (parse-time "%T" "16:00:00") (parse-time "%T" "09:30:00"))23400.0
pact> (diff-time (parse-time "%T" "16:00:00") (parse-time "%T" "09:30:00"))23400.0
In this example, the function returns the result of this computation as a decimal, representing the time difference between the two specified times.
format-time
Use format-time
to format a time
value using a specified format
.
The format-time
function is useful for converting time values to human-readable formats in Pact contracts.
The parse-time
and format-time
functions accept format codes that are derived from the formatting time strftime
function time templates with some extensions.
You can use the following template specifiers that are introduced by using a single percentage (%
) character to format time strings:
Specifier | Description |
---|---|
%a | The abbreviated short name for the day of the week as specified for the current locale, for example,"Sun" –"Sat" . |
%A | The full name for the day of the week as specified for the current locale, for example, "Sunday" –"Saturday" . |
%b | The abbreviated month name as specified for the current locale, for example, "Jan" –"Dec" . |
%B | The full month name as specified for the current locale, for example, "January" –"December" . |
%c | The preferred calendar time representation as specified for the current locale, for example, %a %b %e %H:%M:%S %Z %Y . |
%C | The century of the year with no padding. |
%d | The day of the month as a decimal number zero-padded to two characters, for example, "01" –"31" . |
%D | The date using the format %m/%d/%y . |
%e | The day of the month as a decimal number space-padded to two characters, for example, " 1" –"31" . |
%f | Century for the week date format with no padding. |
%F | The date using the format %Y-%m-%d as specified in the ISO 8601 standard. |
%g | The year corresponding to the ISO week number, without the century, zero-padded to two characters, for example, "00" –"99" . |
%G | The year corresponding to the ISO week number with no padding. |
%h | The abbreviated month name as specified for the current locale, for example, "Jan" –"Dec" . This format is the same as using the %b template. |
%H | The hour of the day as a decimal number, using a 24-hour clock, zero-padded to two characters, for example, "00" –"23" . |
%I | The hour of the day as a decimal number, using a 12-hour clock, zero-padded to two characters, for example, "01" –"12" . |
%j | The day of the year as a decimal number, zero-padded to three characters, for example, "001" –"366" . |
%k | The hour of the day as a decimal number, using a 24-hour clock, space-padded to two characters, for example, " 0" –"23" . |
%l | The hour as a decimal number using a 12-hour clock, space-padded to two characters, for example, " 1" –"12" . |
%m | The month of the year, zero-padded to two characters, for example, "01" –"12" . |
%M | The minute of the hour, zero-padded to two characters, for example, "00" –"59" . |
%N | Numeric representation of the time zone using the ISO 8601 standard, for example, "-06:00" or "+01:00" . |
%p | The half of day representation of "AM" or "PM" or the corresponding strings specified for the current locale. Noon is treated as ‘PM’ and midnight as ‘AM’. If "AM" and "PM" aren't supported, the %p template returns an empty string. |
%P | The half of day representation of "AM" or "PM" or the corresponding strings specified for the current locale, converted to lowercase, for example, "am" and "pm" . Noon is treated as ‘PM’ and midnight as ‘AM’. If "am" and "pm" aren't supported, the %P template returns an empty string. |
%Q | The fraction of a second, up to six second decimal places, without trailing zeros. For a whole number of seconds, %Q produces an empty string. |
%r | The complete calendar time including the AM/PM format as specified for the current locale, for example, %I:%M:%S %p . |
%R | The hour and minute in decimal numbers using the format %H:%M . |
%s | The number of whole seconds since the UNIX epoch (since 1970-01-01 00:00:00 UTC). For times before the UNIX epoch, this is a negative number. Note that in %s.%q and %s%Q formats, the decimals are positive, not negative. For example, 0.9 seconds before the UNIX epoch is formatted as "-1.1" with %s%Q . |
%S | The seconds of a minute, zero-padded to two characters, for example, "00" –"60" . |
%T | The time of day using the format %H:%M:%S . |
%u | The day of the week as a number with Monday being 1, for example, "1" –"7" . |
%U | The week number of the year as a number, starting with the first Sunday as the first day of the first week, zero-padded to two characters, for example, "00" –"53" . |
%v | The microsecond of a second, zero-padded to six characters, for example, "000000" –"999999" . |
%V | The week number as specified using the ISO 8601:1988 standard where weeks start with Monday and end with Sunday. zero-padded to two characters, for example,"01" –"53" . |
%w | The day of the week as a number, starting with Sunday as 0, for example,"0" (= Sunday) – "6" (= Saturday). |
%W | The week number of the year as a number (range 00 through 53), starting with the first Monday as the first day of the first week. zero-padded to two characters, for example, "00" –"53" . |
%x | The preferred date representation as specified for the current locale , for example, %m\/%d\/%y . |
%X | The preferred time of day representation as specified for the current locale, for example, %H:%M:%S . |
%y | The year without a century as a number, zero-padded to two characters, for example, "00" –"99" . |
%Y | The full year as a number with no padding. |
%z | Numeric representation of the time zone using the RFC 822/ISO 8601:1988 standard, for example, "-0600" or "+0100" . |
%Z | The time zone abbreviation or empty if the time zone can't be determined. |
Note that two percentage characters (%%
) are interpretted as a literal percentage sign (%
), not a time formatting template.
In addition, the template for picoseconds with zero padding (%q
) doesn't work properly, so it's not included as a supported template for formatting time strings.
Basic syntax
To format a time value using a specified format, use the following syntax:
(format-time format time)
(format-time format time)
Arguments
Use the following arguments to specify the format and time for the format-time
Pact function:
Argument | Type | Description |
---|---|---|
format | string | Specifies the format string for the time. |
time | time | Specifies the time value to format. |
Return values
The format-time
function returns a new string with the formatted time value.
Examples
The following example demonstrates how to use the format-time
function to format the time value (time "2016-07-22T12:00:00Z")
using the specified format:
pact> (format-time "%F" (time "2016-07-22T12:00:00Z"))"2016-07-22"
pact> (format-time "%F" (time "2016-07-22T12:00:00Z"))"2016-07-22"
In this example, "%F"
is the format string specifying the format of the output.
The result of this operation is a formatted string representing the date in the format YYYY-MM-DD
.
The following example demonstrates how to replace the numeric representing the month of the year with the short name for the month:
pact> (format-time "%Y-%b-%d" (time "2024-07-24T13:30:45Z"))"2024-Jul-24"
pact> (format-time "%Y-%b-%d" (time "2024-07-24T13:30:45Z"))"2024-Jul-24"
hours
Use the hours
function to calculate a time duration in hours.
You can use this function in combination with the add-time
function to add a specific number of hours to a given time.
Basic syntax
The syntax for the hours
function is as follows:
(hours n)
Arguments
Argument | Type | Description |
---|---|---|
n | integer or decimal | Specifies the number of hours as either a decimal or an integer. |
Return Value
The hours
function returns a decimal value representing the specified number of hours.
Examples
Adding hours to a time:
(add-time (time "2016-07-22T12:00:00Z") (hours 1))
(add-time (time "2016-07-22T12:00:00Z") (hours 1))
In this example, the add-time
function is used to add one hour to the time represented by the string "2016-07-22T12:00:00Z"
.
Specifying hours as an integer:
pact>(hours 3)10800.0
pact>(hours 3)10800.0
In this example, the hours
function specifies 3 hours as an integer value.
Specifying hours as a decimal:
pact>(hours 2.5)9000.0
pact>(hours 2.5)9000.0
In this example, the hours
function specifies 2.5 hours as a decimal value.
The hours
function is useful for performing time calculations in Pact contracts, such as adding or subtracting specific durations from timestamps.
minutes
Use minutes
to represent a duration of a specified number of minutes.
You can use this function in combination with the add-time
function to add a specific number of minutes to a given time.
Basic syntax
To represent a duration of N minutes, use the following syntax:
(minutes n)
Argument
Use the following argument to specify the number of minutes for the duration using the minutes
Pact function.
Argument | Type | Description |
---|---|---|
n | decimal or integer | Specifies the number of minutes for the duration. |
Return value
The minutes
function returns the duration in decimal format.
Examples
The following example demonstrates the use of minutes
in combination with add-time
in the Pact REPL:
pact> (add-time (time "2016-07-22T12:00:00Z") (minutes 1))2016-07-22 12:01:00 UTC
pact> (add-time (time "2016-07-22T12:00:00Z") (minutes 1))2016-07-22 12:01:00 UTC
parse-time
Use parse-time
to construct a UTC formatted time string from an input time that uses the specified format.
The parse-time
and format-time
functions accept format codes that are derived from the formatting time strftime
function time templates with some extensions.
You can use the following template specifiers that are introduced by using a single percentage (%
) character to format time strings:
Specifier | Description |
---|---|
%a | The abbreviated short name for the day of the week as specified for the current locale, for example,"Sun" –"Sat" . |
%A | The full name for the day of the week as specified for the current locale, for example, "Sunday" –"Saturday" . |
%b | The abbreviated month name as specified for the current locale, for example, "Jan" –"Dec" . |
%B | The full month name as specified for the current locale, for example, "January" –"December" . |
%c | The preferred calendar time representation as specified for the current locale, for example, %a %b %e %H:%M:%S %Z %Y . |
%C | The century of the year with no padding. |
%d | The day of the month as a decimal number zero-padded to two characters, for example, "01" –"31" . |
%D | The date using the format %m/%d/%y . |
%e | The day of the month as a decimal number space-padded to two characters, for example, " 1" –"31" . |
%f | Century for the week date format with no padding. |
%F | The date using the format %Y-%m-%d as specified in the ISO 8601 standard. |
%g | The year corresponding to the ISO week number, without the century, zero-padded to two characters, for example, "00" –"99" . |
%G | The year corresponding to the ISO week number with no padding. |
%h | The abbreviated month name as specified for the current locale, for example, "Jan" –"Dec" . This format is the same as using the %b template. |
%H | The hour of the day as a decimal number, using a 24-hour clock, zero-padded to two characters, for example, "00" –"23" . |
%I | The hour of the day as a decimal number, using a 12-hour clock, zero-padded to two characters, for example, "01" –"12" . |
%j | The day of the year as a decimal number, zero-padded to three characters, for example, "001" –"366" . |
%k | The hour of the day as a decimal number, using a 24-hour clock, space-padded to two characters, for example, " 0" –"23" . |
%l | The hour as a decimal number using a 12-hour clock, space-padded to two characters, for example, " 1" –"12" . |
%m | The month of the year, zero-padded to two characters, for example, "01" –"12" . |
%M | The minute of the hour, zero-padded to two characters, for example, "00" –"59" . |
%N | Numeric representation of the time zone using the ISO 8601 standard, for example, "-06:00" or "+01:00" . |
%p | The half of day representation of "AM" or "PM" or the corresponding strings specified for the current locale. Noon is treated as ‘PM’ and midnight as ‘AM’. If "AM" and "PM" aren't supported, the %p template returns an empty string. |
%P | The half of day representation of "AM" or "PM" or the corresponding strings specified for the current locale, converted to lowercase, for example, "am" and "pm" . Noon is treated as ‘PM’ and midnight as ‘AM’. If "am" and "pm" aren't supported, the %P template returns an empty string. |
%Q | The fraction of a second, up to six second decimal places, without trailing zeros. For a whole number of seconds, %Q produces an empty string. |
%r | The complete calendar time including the AM/PM format as specified for the current locale, for example, %I:%M:%S %p . |
%R | The hour and minute in decimal numbers using the format %H:%M . |
%s | The number of whole seconds since the UNIX epoch (since 1970-01-01 00:00:00 UTC). For times before the UNIX epoch, this is a negative number. Note that in %s.%q and %s%Q formats, the decimals are positive, not negative. For example, 0.9 seconds before the UNIX epoch is formatted as "-1.1" with %s%Q . |
%S | The seconds of a minute, zero-padded to two characters, for example, "00" –"60" . |
%T | The time of day using the format %H:%M:%S . |
%u | The day of the week as a number with Monday being 1, for example, "1" –"7" . |
%U | The week number of the year as a number, starting with the first Sunday as the first day of the first week, zero-padded to two characters, for example, "00" –"53" . |
%v | The microsecond of a second, zero-padded to six characters, for example, "000000" –"999999" . |
%V | The week number as specified using the ISO 8601:1988 standard where weeks start with Monday and end with Sunday. zero-padded to two characters, for example,"01" –"53" . |
%w | The day of the week as a number, starting with Sunday as 0, for example,"0" (= Sunday) – "6" (= Saturday). |
%W | The week number of the year as a number (range 00 through 53), starting with the first Monday as the first day of the first week. zero-padded to two characters, for example, "00" –"53" . |
%x | The preferred date representation as specified for the current locale , for example, %m\/%d\/%y . |
%X | The preferred time of day representation as specified for the current locale, for example, %H:%M:%S . |
%y | The year without a century as a number, zero-padded to two characters, for example, "00" –"99" . |
%Y | The full year as a number with no padding. |
%z | Numeric representation of the time zone using the RFC 822/ISO 8601:1988 standard, for example, "-0600" or "+0100" . |
%Z | The time zone abbreviation or empty if the time zone can't be determined. |
Note that two percentage characters (%%
) are interpreted as a literal percentage sign (%
), not a time formatting template.
In addition, the template for picoseconds with zero padding (%q
) doesn't work properly, so it's not included as a supported template for formatting time strings.
Basic syntax
To construct a UTC time string from a input time that uses a specified format, use the following syntax:
(parse-time format input)
Arguments
Use the following arguments to specify the format and input time value for constructing time using the parse-time
Pact function.
Argument | Type | Description |
---|---|---|
format | string | Specifies the format for parsing the input time to construct the time as a UTC string. |
input | string | Specifies the input time to be parsed. |
Return value
The parse-time
function returns a UTC formatted time constructed from the provided input value that uses the specified format.
Examples
The following example demonstrates the use of parse-time
in the Pact REPL:
pact> (parse-time "%F" "2024-11-06")"2024-11-06T00:00:00Z"
pact> (parse-time "%F" "2024-11-06")"2024-11-06T00:00:00Z"
In this example, the parse-time
function constructs a UTC-formatted time value from the input value "2024-11-06".
The input string uses the %Y-%m-%d
format specified in the ISO 8601 standard and identified with the "%F" format argument.
In the following example, the input string uses the %D and %T format specifiers for date and time:
(parse-time "%D %T" "11/07/24 08:09:10")"2024-11-07T08:09:10Z"
(parse-time "%D %T" "11/07/24 08:09:10")"2024-11-07T08:09:10Z"
The following example parses an input string that uses the abbreviated month name (%b), the day of the month as a zero-padded string (%d), and the full year (%Y) to a UTC time:
(parse-time "%b %d %Y %T" "Nov 07 2024 08:09:10")"2024-11-07T08:09:10Z"
(parse-time "%b %d %Y %T" "Nov 07 2024 08:09:10")"2024-11-07T08:09:10Z"
time
Use the time
function to construct a time object from a UTC value using the ISO 8601 standard Universal Time Coordinated (UTC) date time format:
%Y-%m-%dT%H:%M:%SZ
%Y-%m-%dT%H:%M:%SZ
This format is the default time format in Pact for all time-related functions.
Internally, the time object supports up to microsecond resolution.
However, the values returned from the Pact interpreter as JSON are be serialized using the default format.
If you need higher resolution, you can explicitly format times with the %v
template and related codes.
Basic syntax
To construct a time object from a UTC value, use the following syntax:
(time UTC)
Arguments
Use the following argument to specify the UTC value for constructing the time object using the time
Pact function.
Argument | Type | Description |
---|---|---|
UTC | string | Specifies the UTC value in ISO8601 format (%Y-%m-%dT%H:%M:%SZ) . |
Return value
The time
function returns a time object constructed from the provided UTC value.
Examples
The following example demonstrates the usage of the time
function within the Pact REPL. It constructs a time object from the UTC value "2016-07-22T11:26:35Z"
:
pact>(time "2016-07-22T11:26:35Z")2016-07-22 11:26:35 UTC
pact>(time "2016-07-22T11:26:35Z")2016-07-22 11:26:35 UTC
This example illustrates how to use the time
function to create a time object from a UTC value using the ISO8601 format in Pact.