Functions - thyme
add |
Easier version of
time:addDuration .
Example:
|
difference |
Find the difference between two
time:Time .
Example:
|
duration |
Create a
Duration from a map.
Example:
|
fromMillis |
Convert from milliseconds to a unit.
Example:
|
fromTimestamp |
Create
time:Time from a unix timestamp.
Examples:
|
now |
A simple wrapper to get a new Datetime object from
time:currentTime .
|
set |
Update a
time:Time attribute Note: Not a conversion, just directly changing attributes.
Don't expect this function to handle converting everything if you change timezone.
Example:
|
setFromMap |
Update a
time:Time with a map of the fields to change.
Example:
|
subtract |
Easier version of
time:subtractDuration .
Example:
|
toMillis |
Convert from a unit to milliseconds.
Example:
|
time:addDuration
.
Example:
time:Time now = time:currentTime();
Duration minuteRice = duration({minute: 1});
time:Time doneCooking = add(now, minuteRice);
-
Return Type
(Time) A new
time:Time
time:Time
.
Example:
time:Time now = time:parse("09:40:00", "HH:mm:ss");
time:Time before = time:parse("09:55:00", "HH:mm:ss");
Duration d = difference(before, now);
-
Return Type
(Duration) A
Duration
representing the difference in milliseconds, it cannot be negative as a duration is a scalar, the user has to understand the direction it goes in time.
Duration
from a map.
Example:
Duration d = duration({seconds: 5, minutes: 10});
Parameters
- args map
-
Map of valid units from
TIME_UNIT_OPTIONS
and desired values. A duration is a scalar, the user has to understand the direction they want it to go in time, so negative values will be converted.
-
Return Type
(Duration) Duration
float|error years = fromMillis("year", 5000000);
Parameters
- unit string
-
A valid unit from
TIME_UNIT_OPTIONS
- milliseconds int
-
Current number of milliseconds
-
Return Type
(float | error) float
if given a valid unit, else anerror
time:Time
from a unix timestamp.
Examples:
time:Time t = fromTimestamp("1587869442");
time:Time t = fromTimestamp("1587869442.345678");
time:Time t = fromTimestamp(1587869442);
time:Time t = fromTimestamp(1587869442.345678);
Parameters
- timestamp any
-
Unix timestamp of any flavor:
int
,float
, or a string that parses to eitherint
orfloat
.
-
Return Type
(Time | error) A
time:Time
with zone UTC
time:currentTime
.
-
Return Type
(Datetime) Datetime
time:Time
attribute Note: Not a conversion, just directly changing attributes.
Don't expect this function to handle converting everything if you change timezone.
Example:
time:Time now = time:currentTime();
time:Time updated = set(now, "zoneId", "America/Indiana/Knox");
Parameters
- t Time
-
time:Time
to modify
- attr string
-
Field to be changed, errors if not in TIME_UNIT_OPTIONS
- value int
-
Value to set the field to
-
Return Type
(Time | error) time:Time
time:Time
with a map of the fields to change.
Example:
time:Time updated = setFromMap({
year: 2025,
hour: 15,
zoneId: "America/Indiana/Knox"
});
Parameters
- t Time
-
time:Time
to modify
- args map
-
fields to be changed, ignores keys not in
TIME_UNIT_OPTIONS
-
Return Type
(Time | error) time:Time
orerror
iftime
library fails
time:subtractDuration
.
Example:
time:Time now = time:currentTime();
Duration aLongTimeAgo = duration({day: 5});
time:Time whenIShouldHaveCleanedRoom = subtract(now, aLongTimeAgo);
-
Return Type
(Time) time:Time
int|error millis = toMillis("year", 2.5);
Parameters
- unit string
-
A valid unit from
TIME_UNIT_OPTIONS
- value int
-
An integer of the current unit's value
-
Return Type
(int | error) int
if a valid unit, else anerror