Functions - thyme

add
Easier version of time:addDuration. Example:
 time:Time now = time:currentTime();
 Duration minuteRice = duration({minute: 1});
 time:Time doneCooking = add(now, minuteRice);
difference
Find the difference between two 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);
duration
Create a Duration from a map. Example:
 Duration d = duration({seconds: 5, minutes: 10});
fromMillis
Convert from milliseconds to a unit. Example:
 float|error years = fromMillis("year", 5000000);
fromTimestamp
Create 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);
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:
 time:Time now = time:currentTime();
 time:Time updated = set(now, "zoneId", "America/Indiana/Knox");
setFromMap
Update a time:Time with a map of the fields to change. Example:
 time:Time updated = setFromMap({
   year: 2025,
   hour: 15,
   zoneId: "America/Indiana/Knox"
 });
subtract
Easier version of time:subtractDuration. Example:
 time:Time now = time:currentTime();
 Duration aLongTimeAgo = duration({day: 5});
 time:Time whenIShouldHaveCleanedRoom = subtract(now, aLongTimeAgo);
toMillis
Convert from a unit to milliseconds. Example:
 int|error millis = toMillis("year", 2.5);

add

(Time originalTime, Duration d)

returns Time
Easier version of time:addDuration. Example:
 time:Time now = time:currentTime();
 Duration minuteRice = duration({minute: 1});
 time:Time doneCooking = add(now, minuteRice);

Parameters

  • originalTime Time
  • Base time:Time you would like to modify

  • Return Type

    (Time)
  • A new time:Time

difference

(Time t1, Time t2)

returns Duration
Find the difference between two 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);

Parameters

  • t1 Time
  • First time:Time

  • t2 Time
  • Second time:Time

  • 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

(map args)

returns Duration
Create a 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.

fromMillis

(string unit, int milliseconds)

returns float | error
Convert from milliseconds to a unit. Example:
 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 an error

fromTimestamp

(any timestamp)

returns Time | error
Create 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 either int or float.

  • Return Type

    (Time | error)
  • A time:Time with zone UTC

now

()

returns Datetime
A simple wrapper to get a new Datetime object from time:currentTime.

set

(Time t, string attr, int value)

returns Time | error
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:
 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

setFromMap

(Time t, map args)

returns Time | error
Update a 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 or error if time library fails

subtract

(Time originalTime, Duration d)

returns Time
Easier version of time:subtractDuration. Example:
 time:Time now = time:currentTime();
 Duration aLongTimeAgo = duration({day: 5});
 time:Time whenIShouldHaveCleanedRoom = subtract(now, aLongTimeAgo);

Parameters

  • originalTime Time
  • Base time:Time you would like to modify

  • Return Type

    (Time)
  • time:Time

toMillis

(string unit, int value)

returns int | error
Convert from a unit to milliseconds. Example:
 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 an error