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


10625
10626
# File 'Monkeybrains.rb', line 10625

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


10645
10646
# File 'Monkeybrains.rb', line 10645

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


10712
10713
# File 'Monkeybrains.rb', line 10712

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


10689
10690
# File 'Monkeybrains.rb', line 10689

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


10746
10747
# File 'Monkeybrains.rb', line 10746

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


10729
10730
# File 'Monkeybrains.rb', line 10729

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


10676
10677
# File 'Monkeybrains.rb', line 10676

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


10663
10664
# File 'Monkeybrains.rb', line 10663

def subtract(duration)
end

valueToybox::Lang::Number

Get the value of a Duration.

Returns:

Since:

  • 1.0.0


10752
10753
# File 'Monkeybrains.rb', line 10752

def value()
end