Class: Toybox.Time.Duration

Inherits:
Toybox.Lang.Object show all

Overview

A Duration is an immutable period of time.

Duration objects are closely related to Moment objects, and are frequently used together for time calculations. While a Moment represents a single point in time, a Duration represents a span of time such as seven days.

Duration objects are stored as a the number of seconds that compose the span of time the Duration represents.

Since:

API Level 1.0.0

Instance Method Summary collapse

Instance Method Details

add(time as Time.Moment or Time.Duration) as Time.Moment or Time.Duration

Add a Moment or another Duration to a Duration.

When adding a Moment to a Duration, this method functions the same as the Moment.add() method.

Parameters:

Example:

Add two Duration objects

using Toybox.Time;
var oneHour    = new Time.Duration(3600);
var twoHours   = new Time.Duration(7200);
var threeHours = oneHour.add(twoHours);

Returns:

  • Time.Duration, Time.Moment

    A Duration or Moment object that is the sum of self and the provided object:

    • Duration + Moment = Moment

    • Duration + Duration = Duration

See Also:

Since:

API Level 1.0.0

compare(duration as Time.Duration) as Lang.Number

Determine if a Duration is shorter or longer than another Duration.

This computes a Number representing the difference between the two Duration objects in seconds. The subtract() method can also be used to get the absolute difference between two Duration objects.

Parameters:

  • duration(Time.Duration)

    The Duration to compare to this Duration

Example:

using Toybox.System;
using Toybox.Time;
var oneHour    = new Time.Duration(3600);
var twoHours   = new Time.Duration(7200);

System.println(oneHour.compare(twoHours)); // -3600, or one minute in the past
System.println(twoHours.compare(oneHour)); //  3600, or one minute in the future

Returns:

  • Lang.Number

    The Number of seconds difference between the two Duration objects. If the Duration supplied for comparison is longer than this Duration, the value will be negative.

Since:

API Level 1.0.0

divide(value as Lang.Number or Lang.Float) as Time.Duration

Divide a Duration by a value.

Parameters:

Example:

using Toybox.Time;
var fourHours  = new Time.Duration(Gregorian.SECONDS_PER_HOUR * 4);
var twoHours = fourHours.divide(2);

Returns:

  • Time.Duration

    The quotient of the Duration and the supplied value

Since:

API Level 1.0.0

greaterThan(duration as Time.Duration) as Lang.Boolean

Determine if a Duration is longer than another Duration.

Parameters:

  • duration(Time.Duration)

    The Duration to compare to this Duration

Example:

using Toybox.System;
using Toybox.Time;
var oneHour    = new Time.Duration(3600);
var twoHours   = new Time.Duration(7200);

System.println(oneHour.greaterThan(twoHours)); // false
System.println(twoHours.greaterThan(oneHour)); // true

Returns:

  • Lang.Boolean

    true if this Duration is longer than the Duration supplied for comparison, otherwise false

Since:

API Level 1.0.0

initialize(value as Lang.Number) Time.Duration

Constructor

Parameters:

  • value(Lang.Number)

    The Number of seconds with which to initialize the Duration

Example:

Create a Duration of one day with a number of seconds

using Toybox.Time;
var oneDay = new Time.Duration(86400);

Returns:

  • Time.Duration

    A Duration representing the specified Number of seconds

See Also:

Since:

API Level 1.0.0

lessThan(duration as Time.Duration) as Lang.Boolean

Determine if a Duration is shorter than another Duration.

Parameters:

  • duration(Time.Duration)

    The Duration to compare to this Duration

Example:

using Toybox.System;
using Toybox.Time;
var oneHour    = new Time.Duration(3600);
var twoHours   = new Time.Duration(7200);

System.println(oneHour.lessThan(twoHours)); // true
System.println(twoHours.lessThan(oneHour)); // false

Returns:

  • Lang.Boolean

    true if this Duration is shorter than the Duration supplied for comparison, otherwise false

Since:

API Level 1.0.0

multiply(value as Lang.Number or Lang.Float) as Time.Duration

Multiply a Duration by a value.

Parameters:

Example:

using Toybox.Time;
var twoHours  = new Time.Duration(7200);
var fourHours = twoHours.multiply(2);

Returns:

  • Time.Duration

    The product of the Duration and the supplied value

Since:

API Level 1.0.0

subtract(duration as Time.Duration) as Time.Duration

Get the absolute difference between two Duration objects.

The computed Duration is always a positive value. The compare() method can also be used to get the difference between two Duration objects.

Parameters:

  • duration(Time.Duration)

    The Duration to subtract from this Duration

Example:

using Toybox.Time;
var twoHours   = new Time.Duration(7200);
var threeHours = new Time.Duration(10800);
var oneHour = threeHours.subtract(twoHours);

Returns:

  • Time.Duration

    The difference between the two Duration objects

Since:

API Level 1.0.0

value() as Lang.Number

Get the value of a Duration.

Returns:

Since:

API Level 1.0.0


Generated Apr 17, 2024 9:40:38 AM