Uh oh! We couldn’t find any match.

Please try other search keywords.

Bixby Developer Center

Guides

DateTime (viv.time)

Many Bixby capsules need to not only work with date and time values, but need to be able to handle complicated requests from users. For example, "3:00 pm next Tuesday"; "4 weeks from now"; "in 3 hours"; "starting Saturday afternoon and ending Wednesday morning". To handle dates and times, Bixby provides the viv.time library capsule. This offers a set of concepts and actions that allow natural language date-time expressions to be converted to fully resolved objects, and provides a special abstract structure concept for training.

Note

While the native JavaScript Date object is supported, the platform-provided dates JavaScript API provides convenience functions for working with date and time models, as well as for returning valid viv.time concepts.

Types are always in the viv.\* namespace, and in the viv.time.\* namespace unless another namespace is specified (e.g., viv.rating.\*). All properties have a cardinality of min (Required), max (One) unless otherwise specified.

Date and Time Concepts

The viv.time capsule include the following major concepts. (There are many other concepts used internally in viv.time, and some of the models below have private fields used for processing, but the ones below are the ones your capsule is most likely to interact with.)

Date

A precise date: for example, March 23, 2018. It can optionally include a "fuzzy" period (for example, ±3 days), which your function can use if appropriate. (For example, a search for airline flights might specify a departure date, but also include flights on the day before and the day after.)

PropertyTypeKindNotes
yearYearintegerallowable values 1900–2100
monthMonthintegerallowable values 1–12
dayDayintegerallowable values 1–31
fuzzyFactorFuzzyFactorstructurerole-of Period

Time

A precise point in time during a day, without a date specified. It can optionally include a "fuzzy" period (for example, ±3 minutes), which your function can use if appropriate.

PropertyTypeKindNotes
hourHourintegerallowable values 0–23
minuteMinuteintegerallowable values 0–59
secondSecondintegerallowable values 0–59
millisecondMillisecondintegerallowable values 0–999
timezoneTimeZoneIdstring
fuzzyFactorFuzzyFactorstructurerole-of Period

A TimeZoneId is a string in the "Area/Location" format defined by the IANA Time Zone Database, such as "America/Los_Angeles" or "Asia/Seoul".

DateTime

A structure containing both a required date and time, as well as the Unix time expressed in milliseconds.

PropertyTypeKindNotes
dateDatestructure
timeTimestructure
utcInstantUTCInstantintegermilliseconds from epoch
calculatedFromNowCalculatedFromNowboolean(1)
  1. The calculatedFromNow field indicates whether a Date, DateTime, or any Interval type is calculated from now by using a DateTimeExpression such as "in the next 3 hours" or "tomorrow".

Period

A structure representing a span of time, for example, 3 years 5 months 2 days and 7 hours. This is frequently used as a structure within other structures, such as the fuzzyFactor for Date and Time concepts.

Note

A period cannot be converted to a millisecond duration without associating it with a DateTime, since '1 month' will vary depending on the month.

PropertyTypeKindNotes
periodYearsPeriodYearsinteger
periodMonthsPeriodMonthsinteger
periodWeeksPeriodWeeksinteger
periodDaysPeriodYearsinteger
periodNightsPeriodNightsinteger(1)
periodHoursPeriodHoursinteger
periodMinutesPeriodMinutesinteger
periodSecondsPeriodSecondsinteger
periodMillisecondsPeriodMillisecondsinteger
  1. The periodNights field is useful for travel searches such as "I want to stay for three nights". It equates to one day longer than the number of nights: a period of three nights is a period of four days.

DateInterval

An interval with a start Date field and an end Date field: for example, March 23, 2018 to March 26, 2018.

PropertyTypeKindNotes
startStartDatestructure (Date)
endEndDatestructure (Date)

TimeInterval

An interval with a start Time field and an end Time field, with no date specified: for example, 10:00 am to 11:00 am.

PropertyTypeKindNotes
startStartTimestructure (Time)
endEndTimestructure (Time)

DateTimeInterval

An interval with a start DateTime field and an end DateTime field. This is the most complete representation of an interval from one millisecond instant to another instant, complete with time zone.

PropertyTypeKindNotes
startStartDateTimestructure (DateTime)
endEndDateTimestructure (DateTime)

DurationPeriod

This is a transient variant of Period, and should be used by actions as the input type for a duration.

DateTimeExpression

A convenience structure containing the four major viv.time input types. This is an abstraction designed for natural language input; read Training below for more details.

PropertyTypeKindNotes
dateDatestructureoptional
dateTimeDateTimestructureoptional
dateIntervalDateIntervalstructureoptional
dateTimeIntervalDateTimeIntervalstructureoptional

Action Modeling

This example supposes an earthquake search capsule, which can accept queries searching for quakes that occurred on a given date or during specified intervals, with or without times. The FindEarthquakes action could be modeled like this:

action (earthquake.FindEarthquakes) {
description (Action to find an earthquake based on when it occurred [required], where, the magnitude and/or classfication.)

input (dateTimeExpression) {
type (time.DateTimeExpression)
}

input (where) {
type (geo.SearchRegion)
}
input (minMagnitude) {
type (earthquake.Magnitude)
}
input (classification) {
type (earthquake.Classification)
}

output (earthquake.Earthquake)
}

Handling Dates and Times in JavaScript

The other side of handling dates and times is using them in your JavaScript implementations. The concepts from viv.time will be passed to your functions as JavaScript objects, and can be manipulated easily.

Depending on your capsule's design, you might need to perform other date and time functions, particularly when working with external APIs. Tasks such as converting to JSON or parsing dates are provided in Bixby's JavaScript API. Bixby's dates class can create ZonedDateTime objects, which have a variety of methods that include date/time addition and subtraction, comparisons, conditional tests, and formatting functions.

The JavaScript implementation for the FindEarthquakes action above could be coded like this:

module.exports = function findEarthquakes(
where, dateTimeExpression, minMagnitude, classification
) {
var whenStart;
var whenEnd;

if (dateTimeExpression.date) {
whenStart = dates.ZonedDateTime.fromDate(dateTimeExpression.date);
whenEnd = whenStart.withHour(23).withMinute(59).withSecond(59);
}
else if (dateTimeExpression.dateInterval) {
whenStart = dates.ZonedDateTime.of(
dateTimeExpression.dateInterval.start.year,
dateTimeExpression.dateInterval.start.month,
dateTimeExpression.dateInterval.start.day);
whenEnd = dates.ZonedDateTime.of(
dateTimeExpression.dateInterval.end.year,
dateTimeExpression.dateInterval.end.month,
dateTimeExpression.dateInterval.end.day,
23, 59, 59);
}
else if (dateTimeExpression.dateTimeInterval) {
whenStart = dates.ZonedDateTime.of(
dateTimeExpression.dateTimeInterval.start.date.year,
dateTimeExpression.dateTimeInterval.start.date.month,
dateTimeExpression.dateTimeInterval.start.date.day,
dateTimeExpression.dateTimeInterval.start.time.hour,
dateTimeExpression.dateTimeInterval.start.time.minute,
dateTimeExpression.dateTimeInterval.start.time.second);
whenEnd = dates.ZonedDateTime.of(
dateTimeExpression.dateTimeInterval.end.date.year,
dateTimeExpression.dateTimeInterval.end.date.month,
dateTimeExpression.dateTimeInterval.end.date.day,
dateTimeExpression.dateTimeInterval.end.time.hour,
dateTimeExpression.dateTimeInterval.end.time.minute,
dateTimeExpression.dateTimeInterval.end.time.second);
}

var start = whenStart.toIsoString();
var end = whenEnd.toIsoString();

// code continues...
}

Training

While it's possible to train your capsule using any of the viv.time concepts as utterance annotations, it's usually best to use viv.time.DateTimeExpression rather than Date, DateTime, or either of the interval types.

viv.time.DateTimeExpression markup

Because "this weekend" in the above example is annotated as a DateTimeExpression, this could match utterances such as "March 8th", "four weeks from now", "tomorrow at 8pm", or "Christmas morning". This gives your capsule tremendous flexibility in understanding user requests involving dates and times with very little work on your part. Bixby will simply provide the proper DateTime concept to your action.

Aligned natural language queries that could be used for the FindEarthquakes action might include:

show earthquakes[g:viv.earthquake.Earthquake] around
(San Francisco)[v:viv.geo.LocalityName]
(on October 17, 1989)[v:viv.time.DateTimeExpression]

show earthquakes[g:viv.earthquake.Earthquake] of at least
(5.0)[v:viv.earthquake.Magnitude:5.0] magnitude around
(San Francisco)[v:viv.geo.LocalityName]
(last month)[v:viv.time.DateTimeExpression]

find earthquakes[g:viv.earthquake.Earthquake] in
(Puerto Rico)[v:viv.geo.CountryDivisionName:'Puerto Rico']
(yesterday afternoon)[v:viv.time.DateTimeExpression]

The first of these queries would resolve to a Date, and fill in the date field of the returned DateTimeExpression. The second would resolve to a DateInterval; the third would resolve to a DateTimeInterval. When you train on DateTimeExpression, only one of the four fields will be set at a time, based on the value derived from the user's utterance. Your JavaScript action implementation must test for values in each field of DateTimeExpression and take appropriate action. The example implementation above for findEarthquakes demonstrates this.

Bixby Views Components

Bixby Views provide input components that include interactive components for selecting dates and times.

  • The date picker returns a viv.time.Date concept.

  • The time picker returns a viv.time.Time concept.

  • The calendar returns either a viv.time.Date or a viv.time.DateInterval concept.

The components are customizable, letting you select the initial values and the range of allowed values. They require you to import viv.time into your capsule.

For details and examples of how to use either picker, read the date picker and time picker entries in the Developer Center's References.