Class: Toybox.Time.LocalMoment

Inherits:
Toybox.Lang.Object show all

Overview

A LocalMoment is an immutable moment in time.

LocalMoment represents a single point in time at a specific location. It differs from Moment in that it also keeps time zone information in addition to the time.

Example:

// This code will print the following
// to the console:
// 2018-02-23 18:12:00
// day_of_week=6 month=2
// day_of_week=Fri month=Feb

// Saturday Feb 24th, 2018 12:12am UTC
var options = {
    :year   => 2018,
    :month  => 2,
    :day    => 24,
    :hour   => 0,
    :min    => 12
};

var when = Gregorian.moment(options);

var where = new Position.Location({
    :latitude  =>  38.85391,
    :longitude => -94.79630,
    :format    => :degrees,
});

var local = Gregorian.localMoment(where, when);

var info = Gregorian.info(local, Time.FORMAT_SHORT);

// Friday Feb 23th, 2018 6:12pm
Sys.println(Lang.format("$1$-$2$-$3$ $4$:$5$:$6$", [
    info.year.format("%04u"),
    info.month.format("%02u"),
    info.day.format("%02u"),
    info.hour.format("%02u"),
    info.min.format("%02u"),
    info.sec.format("%02u"),
]));

Sys.println(Lang.format("day_of_week=$1$ month=$2$", [
    info.day_of_week,
    info.month
]));

info = Gregorian.info(now, Time.FORMAT_LONG);

Sys.println(Lang.format("day_of_week=$1$ month=$2$", [
    info.day_of_week,
    info.month
]));

Since:

API Level 3.3.0

Supported Devices:

Instance Method Summary collapse

Instance Method Details

add(addend as Time.Duration) as Time.LocalMoment

Add a Duration to a LocalMoment.

Parameters:

  • addend(Time.Duration)

    The Duration to add to this LocalMoment.

Returns:

  • Time.LocalMoment

    A LocalMoment object that is the sum of self and the provided Duration object.

Since:

API Level 3.3.0

compare(moment as Time.LocalMoment) as Lang.Number

Determine if a LocalMoment is before or after another LocalMoment

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

Parameters:

  • moment(Time.LocalMoment)

    The LocalMoment to compare to this LocalMoment

Example:

using Toybox.System;
using Toybox.Time;
using Toybox.Time.Gregorian;
using Toybox.Position;

var where = new Position.Location({
    :latitude  =>  38.85391,
    :longitude => -94.79630,
    :format    => :degrees,
});
var today = Gregorian.localMoment(where, Time.today());
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

Returns:

  • Lang.Number

    The Number of seconds difference between the two LocalMoment objects, without considering time zone rules. If the LocalMoment supplied for comparison is after this LocalMoment, the value will be negative.

See Also:

Since:

API Level 3.3.0

getDaylightSavingsTimeOffset() as Lang.Number

Get the daylight saving time offset from UTC time in seconds.

Returns:

  • Lang.Number

    The daylight saving time offset in seconds.

Since:

API Level 3.3.0

getOffset() as Lang.Number

Get the total time offset from UTC time in seconds

Returns:

  • Lang.Number

    The total offset from UTC time in seconds.

Since:

API Level 3.3.0

getTimeZoneOffset() as Lang.Number

Get the time zone offset from UTC time in seconds.

This is the time zone offset without the daylight saving time offset.

Returns:

  • Lang.Number

    The time zone offset from UTC in seconds. Positive values are East of UTC.

Since:

API Level 3.3.0

greaterThan(moment as Time.LocalMoment) as Lang.Boolean

Determine if a LocalMoment is greater than another LocalMoment.

Parameters:

  • moment(Time.LocalMoment)

    The LocalMoment to compare to this LocalMoment

Returns:

  • Lang.Boolean

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

Since:

API Level 3.3.0

isDaylightSavingsTime() as Lang.Boolean

Get whether the daylight saving time offset is in effect

Returns:

  • Lang.Boolean

    true if daylight saving time is in effect for this time.

Since:

API Level 3.3.0

lessThan(moment as Time.LocalMoment) as Lang.Boolean

Determine if a LocalMoment is less than another LocalMoment.

Parameters:

  • moment(Time.LocalMoment)

    The LocalMoment to compare to this LocalMoment

Returns:

  • Lang.Boolean

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

Since:

API Level 3.3.0

subtract(subtrahend as Time.Duration or Time.LocalMoment) as Time.Duration or Time.LocalMoment

Subtract a Duration or LocalMoment from a LocalMoment.

Parameters:

Returns:

  • Time.Duration, Time.LocalMoment

    The Duration between the two LocalMoment objects or the LocalMoment offset by a Duration. When subtracting LocalMoments, the computed Duration is always a positive value. The compare() method can be used to determine whether one LocalMoment is before or after another LocalMoment.

Since:

API Level 3.3.0

toMoment() as Time.Moment

Get a Moment for this.

Returns:

  • Time.Moment

    The UTC time of the LocalMoment as a Moment

Since:

API Level 3.3.0

value() as Lang.Number

Get the UTC value of a LocalMoment.

Returns:

  • Lang.Number

    The UTC time of the LocalMoment in seconds since the UNIX epoch

See Also:

Since:

API Level 3.3.0


Generated Apr 17, 2024 9:40:38 AM