Class: Toybox::Time::Moment

Inherits:
Lang::Object show all

Overview

A Moment is an immutable moment in time.

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

Internally, Moment objects are stored as 32-bit integers representing the number of seconds since the UNIX epoch (January 1, 1970 at 00:00:00 UTC).

See Also:

Since:

  • 1.0.0

Instance Method Summary collapse

Methods inherited from Lang::Object

equals, hashCode, method, toString, weak

Constructor Details

initialize(seconds) ⇒ Toybox::Time::Moment

Creates a Moment object.

Examples:

Create a Moment with a UNIX time stamp

using Toybox.Time;
var garminFounded = new Time.Moment(631065600);

Create a Moment representing today

using Toybox.Time;
var today = new Time.Moment(Time.today().value());

Parameters:

See Also:

Since:

  • 1.1.2


11806
11807
# File 'Monkeybrains.rb', line 11806

def initialize(seconds)
end

Instance Method Details

add(duration) ⇒ Toybox::Time::Moment

Add a Duration to a Moment.

This method functions the same as the Duration.add() method when adding a Duration to a Moment.

Examples:

Add one day to today

using Toybox.Time;
using Toybox.Time.Gregorian;
var today = new Time.Moment(Time.today().value());
var oneDay = new Time.Duration(Gregorian.SECONDS_PER_DAY);
var tomorrow = today.add(oneDay);

Parameters:

Returns:

  • (Toybox::Time::Moment)

    A Moment object that is the sum of the provided Moment and Duration objects

See Also:

Since:

  • 1.0.0


11826
11827
# File 'Monkeybrains.rb', line 11826

def add(duration)
end

compare(moment) ⇒ Toybox::Lang::Number

Determine if a Moment is before or after another Moment.

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

Examples:

using Toybox.System;
using Toybox.Time;
using Toybox.Time.Gregorian;
var today = new Time.Moment(Time.today().value());
var oneDay = new Time.Duration(Gregorian.SECONDS_PER_DAY);
var tomorrow = today.add(oneDay);

System.println(today.compare(tomorrow)); // -86400, or one day in the past
System.println(tomorrow.compare(today)); //  86400, or one day in the future

Parameters:

Returns:

  • (Toybox::Lang::Number)

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

See Also:

Since:

  • 1.0.0


11882
11883
# File 'Monkeybrains.rb', line 11882

def compare(moment)
end

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

Determine if a Moment is greater than another Moment.

Examples:

using Toybox.System;
using Toybox.Time;
using Toybox.Time.Gregorian;
var today = new Time.Moment(Time.today().value());
var oneDay = new Time.Duration(Gregorian.SECONDS_PER_DAY);
var tomorrow = today.add(oneDay);

System.println(today.greaterThan(tomorrow)); // false
System.println(tomorrow.greaterThan(today)); // true

Parameters:

Returns:

  • (Toybox::Lang::Boolean)

    true if this Moment is greater than the Moment supplied for comparison, otherwise false

See Also:

Since:

  • 1.0.0


11922
11923
# File 'Monkeybrains.rb', line 11922

def greaterThan(moment)
end

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

Determine if a Moment is less than another Moment.

Examples:

using Toybox.System;
using Toybox.Time;
using Toybox.Time.Gregorian;
var today = new Time.Moment(Time.today().value());
var oneDay = new Time.Duration(Gregorian.SECONDS_PER_DAY);
var tomorrow = today.add(oneDay);

System.println(today.lessThan(tomorrow)); // true
System.println(tomorrow.lessThan(today)); // false

Parameters:

Returns:

  • (Toybox::Lang::Boolean)

    true if this Moment is less than the Moment supplied for comparison, otherwise false

See Also:

Since:

  • 1.0.0


11902
11903
# File 'Monkeybrains.rb', line 11902

def lessThan(moment)
end

subtract(subtrahend) ⇒ Toybox::Time::Duration, Toybox::Time::Moment

Subtract a Duration or Moment from a Moment.

Examples:

using Toybox.Time;
using Toybox.Time.Gregorian;
var today = new Time.Moment(Time.today().value());
var oneDay = new Time.Duration(Gregorian.SECONDS_PER_DAY);
var tomorrow = today.add(oneDay);

var duration1 = today.subtract(tomorrow);
var duration2 = tomorrow.subtract(today);

System.println(duration1.value()); // 86400, or one day
System.println(duration2.value()); // 86400, or one day

Parameters:

Returns:

  • (Toybox::Time::Duration, Toybox::Time::Moment)

    The Duration between the two Moment objects or the Moment offset by a Duration.

    When subtracting Moments, the computed Duration is always a positive value. The compare() method can be used to determine whether one Moment is before or after another Moment.

See Also:

Since:

  • 1.0.0


11855
11856
# File 'Monkeybrains.rb', line 11855

def subtract(subtrahend)
end

valueToybox::Lang::Number

Get the UTC value of a Moment.

Returns:

See Also:

Since:

  • 1.0.0


11931
11932
# File 'Monkeybrains.rb', line 11931

def value()
end