Class: Toybox::Time::Duration

Inherits:
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:

  • 1.0.0

Instance Method Summary collapse

Methods inherited from Lang::Object

equals, hashCode, method, toString, weak

Constructor Details

initialize(value) ⇒ Toybox::Time::Duration

Creates a Duration object.

Examples:

Create a Duration of one day with a number of seconds

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

Parameters:

See Also:

Since:

  • 1.0.0


11959
11960
# File 'Monkeybrains.rb', line 11959

def initialize(value)
end

Instance Method Details

add(time) ⇒ Toybox::Time::Duration, Toybox::Time::Moment

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.

Examples:

Add two Duration objects

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

Parameters:

Returns:

See Also:

Since:

  • 1.0.0


11979
11980
# File 'Monkeybrains.rb', line 11979

def add(time)
end

compare(duration) ⇒ Toybox::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.

Examples:

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

Parameters:

Returns:

  • (Toybox::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:

  • 1.0.0


12046
12047
# File 'Monkeybrains.rb', line 12046

def compare(duration)
end

divide(value) ⇒ Toybox::Time::Duration

Divide a Duration by a value.

Examples:

using Toybox.Time;
var fourHours  = new Time.Duration(14800);
var twoHours = fourHours.divide(2);

Parameters:

Returns:

Since:

  • 1.0.0


12023
12024
# File 'Monkeybrains.rb', line 12023

def divide(value)
end

greaterThan(duration) ⇒ Toybox::Lang::Boolean

Determine if a Duration is longer than another Duration.

Examples:

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

Parameters:

Returns:

  • (Toybox::Lang::Boolean)

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

Since:

  • 1.0.0


12080
12081
# File 'Monkeybrains.rb', line 12080

def greaterThan(duration)
end

lessThan(duration) ⇒ Toybox::Lang::Boolean

Determine if a Duration is shorter than another Duration.

Examples:

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

Parameters:

Returns:

  • (Toybox::Lang::Boolean)

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

Since:

  • 1.0.0


12063
12064
# File 'Monkeybrains.rb', line 12063

def lessThan(duration)
end

multiply(value) ⇒ Toybox::Time::Duration

Multiply a Duration by a value.

Examples:

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

Parameters:

Returns:

Since:

  • 1.0.0


12010
12011
# File 'Monkeybrains.rb', line 12010

def multiply(value)
end

subtract(duration) ⇒ Toybox::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.

Examples:

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

Parameters:

Returns:

Since:

  • 1.0.0


11997
11998
# File 'Monkeybrains.rb', line 11997

def subtract(duration)
end

valueToybox::Lang::Number

Get the value of a Duration.

Returns:

Since:

  • 1.0.0


12086
12087
# File 'Monkeybrains.rb', line 12086

def value()
end