Temporal Data
Temporal data can be used and stored in Deephaven using DateTimes, Periods, and Durations/Times.
DateTime
A DateTime can be represented with the following patterns: yyyy-MM-ddThh:mm:ss.millis/micros/nanos TZ
. This gets converted into a DBDateTime
.
Period
A Period is a duration of time and can be represented with the following patterns: -#Y#M#W#DT#H#M#S
. The negative sign and partial periods are optional. For example, if you wanted to compute the DBDateTime
1 day and 1 hour from another DBDateTime
, you could use time+'1dT1h'
.
Durations/Times
A Durations/Times
can be represented with the following patterns: -hh:mm:ss.millis/micros/nanos
. The negative is optional. This gets converted into a Long
representing the nanoseconds since January 1, 1970 UTC. For example, '01:02:03.456' is 1 hour, 2 minutes, 3 seconds, and 456 milliseconds.
Durations/Times
can be added to DBDateTimes
.
Binning Intervals
Downsampling time series data can be done by calculating the bin intervals for values and then using firstBy
or lastBy
to select the first or last row for each bin.
For example, using a table named x that contains a DBDateTime
column called Timestamp
, you can get five-minute interval samples from the data using the following:
downsampledx = x.updateView("TimeBin=upperBin(Timestamp, 5 * MINUTE)").lastBy("TimeBin").dropColumns("TimeBin")
This is a little tedious in that you have to add the TimeBin
column to get the lastBy
, and then you drop it because you don't want it as part of your result set. An alternate way to do this is to use DownsampledWhereFilter
, which does the same downsampling, but with a more simple query.
Downsampling
Sampling time-series data on some fixed-time interval is a common method used to reduce the required storage space or for increasing processing speed. While it is possible to perform this operation using a combination of more basic Deephaven operations, there is an existing utility that covers many common applications.
The Downsampler takes a time-series table as input and produces a downsampled version. Other than the timestamp and key columns, each output column is produced by an aggregate: typically either last, min, max, sum, first, last, std, var, avg,
or array
. All aggregates will take match pairs to support renaming the source column (i.e. "Volume=LastSize
" when a sum aggregate is applied).
Timestamps from a specified column are grouped into "bins" using a method determined by the timeBinMode
(defaults to UPPER
, which would put all times between, for example, 3:20 and 3:25 into the 3:25 time bin if 5 minute intervals are used). For some applications, it is useful to have every "time bin" represented, even if there is no data. In this case, one can set the allBins
option. Sometimes, as in stock quote data, if there is no new data, it is desirable that the value of the prior bin should be used instead of null. To produce this behavior, specify the relevant column(s) in the maintainState
option.
The Downsamples
can operate in online mode (default is on), in which case the downsampled table is updated whenever the source changes. If this behavior isn't needed, it is possible to turn off online mode, and run large downsampling jobs using multiple threads in parallel. Online mode supports only single-threaded operation.
Under normal conditions, every time a new row is added to the source table, the downsampled table will "tick". This is because the last time bin is always "active" (i.e., a row at 3:23 will affect the 3:25 bin). This causes a large volume of table updates in the downsampled table (at least one for each new row in the source). Using the excludeLastBin
option, the last time bin can be excluded from the output until it can no longer change (assuming timestamps increase monotonically), one can eliminate this behavior. This is useful when logging the downsampled output to another table.
Typical examples:
Downsample quotes, while filling in "empty" time bins, and maintaining state.
import com.illumon.iris.downsampling.Downsampler
downsampledQuotes = Downsampler.downsample(db, quoteTable, "Timestamp", "00:05:00", "Sym")
.allBins
.last("Bid","BidSize","Ask","AskSize")
.maintainState("Bid","BidSize","Ask","AskSize")
.execute()
Downsample trades, calculating Volume, High and Low per time bin:
import com.illumon.iris.downsampling.Downsampler
downsampledTrades = Downsampler.downsample(db, tradeTable, "Timestamp", "00:05:00", "Symbol")
.last("Price","Size")
.sum("Volume=Size")
.min("Low=Price")
.max("High=Price")
.execute()
Calendars Methods
The Calendars
class holds the available calendar objects in Deephaven. There are three primary methods to access these calendars:
- You can specify the calendar by name. (Note: Use the
calendarNames()
method to get a list of available calendar names.) - You can get the default calendar as specified by the
Calendar.default
property in your configurations, or you can set the default calendar under Settings. - You can use the provided calendars as variables
For example, if you wanted to use the provided USNYSE calendar in your query, you could use the following:
curDayNY = CALENDAR_USYSE.currentDay()
Or equivalently,
from deephaven import * # Calendars module imported as cals
curDayNY = cals.calendar("USNYSE").currentDay()
import com.illumon.util.calendar.Calendars
curDayNY = Calendars.calendar("USNYSE").currentDay()
In query strings, you can use the default calendar methods statically:
t2 = t.where("Date = currentDay()")
Method |
Description |
---|---|
|
Returns a specified business calendar. |
|
Returns the default business calendar. |
|
Returns the names of all available calendars |
Calendar Methods
Many calendar methods are available to help users obtain time and date related information via queries, as shown below. The methods that take no date parameter act on the current date. For example nextDay(5)
is equivalent to nextDay(currentDay(), 5)
.
See also: Business Calendar Methods
Method |
Description |
---|---|
|
Gets the day of the week for a time. For example, dayOfWeek(20170308) would return Wednesday. |
|
Gets the days in a given range. |
|
Returns the amount of time in days between start and end. |
|
Returns the amount of time in nanoseconds between start and end. |
|
Returns the number of years between start and end. |
|
Gets the name of the calendar. |
|
Gets the next date. |
|
Gets the date specified by the number of days after the input date. |
|
Gets the number of days in a given range, end date exclusive. |
|
Gets the number of days in a given range, with the option to make the end date inclusive or exclusive. |
|
Gets the previous date. |
|
Gets the date specified by the number of days before the input date. |
|
Gets the timezone of the calendar. |
Available Timezones
TZ_NY(DateTimeZone.forID("America/New_York"))
TZ_ET(DateTimeZone.forID("America/New_York"))
TZ_MN(DateTimeZone.forID("America/Chicago"))
TZ_CT(DateTimeZone.forID("America/Chicago"))
TZ_MT(DateTimeZone.forID("America/Denver"))
TZ_PT(DateTimeZone.forID("America/Los_Angeles"))
TZ_HI(DateTimeZone.forID("Pacific/Honolulu"))
TZ_BT(DateTimeZone.forID("America/Sao_Paulo"))
TZ_KR(DateTimeZone.forID("Asia/Seoul"))
TZ_HK(DateTimeZone.forID("Asia/Hong_Kong"))
TZ_JP(DateTimeZone.forID("Asia/Tokyo"))
TZ_AT(DateTimeZone.forID("Canada/Atlantic"))
TZ_NF(DateTimeZone.forID("Canada/Newfoundland"))
TZ_AL(DateTimeZone.forID("America/Anchorage"))
TZ_IN(DateTimeZone.forID("Asia/Kolkata"))
TZ_CE(DateTimeZone.forID("Europe/Berlin"))
TZ_SG(DateTimeZone.forID("Asia/Singapore"))
TZ_LON(DateTimeZone.forID("Europe/London"))
TZ_MOS(DateTimeZone.forID("Europe/Moscow"))
TZ_SHG(DateTimeZone.forID("Asia/Shanghai"))
TZ_CH(DateTimeZone.forID("Europe/Zurich"))
TZ_NL(DateTimeZone.forID("Europe/Amsterdam"))
TZ_TW(DateTimeZone.forID("Asia/Taipei"))
TZ_SYD(DateTimeZone.forID("Australia/Sydney"))
TZ_UTC(DateTimeZone.UTC)
Business Calendars
Business calendars are used in Deephaven to state when a business entity is operational (or not), including the hours, days and years of regular operations, as well as holidays.
For example, one of the business calendars installed with Deephaven presents temporal information pertaining to the New York Stock Exchange (NYSE), which is open Monday through Friday, from 9:30 a.m. to 4 p.m. Eastern time. The calendar also includes information about the exchange's nine full-day holidays and its partial holidays, which vary from year to year.
Creating a Custom Business Calendar
In addition to the installed business calendars in Deephaven, you can also create your own custom business calendar.
Business calendars are stored in files that use XML formatting to specify the aspects of the calendar, including the timezone, hours and days of the week the business is operational, as well as holidays.
The root element of the XML file is <calendar>
, and there are four children elements.
<name>
is the name of the business calendar (required)<timeZone>
provides the time zone used for the calendar (required)<default>
provides information about regular hours and days the business is and is not operational<holiday>
provides information about individual holidays (special days/times for which the business is non-operational)
name
In the following example, "Colorado" is the name of the calendar:
<name>Colorado</name>
timeZone
The timeZone
element provides the timezone used for the calendar in Deephaven. For our example calendar, TZ_MT
is the timezone used (U.S. Mountain Time), as shown below.
<timeZone>TZ_MT</timeZone>
default
There are two components included in the default element.
businessPeriod
- provides the hours of the day that business operations are conducted. The values listed are the opening time and closing time separated by a comma, using a 24-hour clock in the timezone noted in thetimeZone
element. Note: More than onebusinessPeriod
can be included in the default element. For example, if a business closes regularly for lunch, there could be twobusinessPeriod
components in the default element - one for the period before lunch and one for the period after lunch.weekend
- provides the day of the week that business operations are not operational. Each weekend day is presented individually. This is an optional component. If there are no weekends included in the default element, Deephaven will assume the business is operational seven days per week. For our example calendar, we will assume that business in Colorado is operational from 9 a.m. to 6 p.m., Monday through Friday, as shown below:
<default>
<businessPeriod>09:00,18:00</businessPeriod>
<weekend>Saturday</weekend>
<weekend>Sunday</weekend>
</default>
holiday
There are two components included in the holiday element.
date
- provides the year, month and date of the holiday in the formatYYYYMMDD
. Each individual holiday must be presented by itself, including a separate record for each year in which it is celebrated. Ifdate
is not included in the holiday element, Deephaven will assume there are no holidays.businessPeriod
- provides the hours of the day that business operations are conducted. The values listed are the starting time and ending time separated by a comma, using a 24-hour clock in the timezone noted above. If business operations are closed for the entire day, this element is not required.
For our example calendar, we will assume Colorado has full-day holidays for Zebulon Pike's birthday on January 5, Colorado Gold Rush Day on July 11, the anniversary of Colorado statehood on August 1 and Stephen Long's birthday on December 30. We will also specify a partial holiday for Dr. Edwin James' birthday on August 27. These holidays (as celebrated in 2017) are shown below:
<holiday>
<date>20170105</date>
</holiday>
<holiday>
<date>20170711</date>
</holiday>
<holiday>
<date>20171230</date>
</holiday>
<holiday>
<date>20170801</date>
</holiday>
<holiday>
<date>20170827</date>
<businessPeriod>09:00,12:00</businessPeriod>
</holiday>
The entire XML file for a Business Calendar named "Colorado" follows:

colorado.calendar
(click to expand)
<calendar>
<name>Colorado</name>
<timeZone>TZ_MT</timeZone>
<default>
<businessPeriod>09:00,18:00</businessPeriod>
<weekend>Saturday</weekend>
<weekend>Sunday</weekend>
</default>
<holiday>
<date>20170105</date>
</holiday>
<holiday>
<date>20170711</date>
</holiday>
<holiday>
<date>20171230</date>
</holiday>
<holiday>
<date>20170801</date>
</holiday>
<holiday>
<date>20170827</date>
<businessPeriod>09:00,12:00</businessPeriod>
</holiday>
</calendar>
Installing Custom Business Calendars
Once a new custom Business Calendar is saved with the .calendar
suffix, your system administrator will need to install the file(s) to the Deephaven server(s). Instructions for the system administrator are included in the Installing Custom Calendars and Custom Plotting Themes.
Business Calendar Methods
As shown below, there are many methods specific to Business Calendars that are available to help users obtain information about the times and dates in which a business operates. The methods that take no date parameter act on the current date, e.g., nextBusinessDay(5)
is equivalent to nextBusinessDay(currentDay(), 5)
.
See also: Calendar Methods.
Method |
Description |
---|---|
businessDaysInRange(DBDateTime start, DBDateTime end) businessDaysInRange(java.lang.String start, java.lang.String end) |
Returns the business days between start and end, inclusive. |
diffBusinessDay(DBDateTime start, DBDateTime end) |
Returns the amount of business time in standard business days between start and end. |
diffBusinessNanos(DBDateTime start, DBDateTime end) |
Returns the amount of business time in nanoseconds between start and end. |
diffBusinessYear(DBDateTime start, DBDateTime end) |
Returns the number of business years between start and end. |
diffNonBusinessDay(DBDateTime start, DBDateTime end) |
Returns the amount of non-business time in standard business days between start and end. |
diffNonBusinessNanos(DBDateTime start, DBDateTime end) |
Returns the amount of non-business time in nanoseconds between start and end. |
fractionOfBusinessDayComplete(DBDateTime time) |
Returns the fraction of the business day complete by the given time. |
fractionOfBusinessDayRemaining(DBDateTime time) |
Returns the fraction of the business day remaining after the given time. |
fractionOfStandardBusinessDay(DBDateTime time) fractionOfStandardBusinessDay(java.lang.String date) |
For the given date, returns the ratio of the business day length and the standard business day length.
|
Note: The above methods replace the now deprecated methods shown below:
|
Gets the indicated business schedule.
|
isBusinessDay(DBDateTime time) |
Does time occur on a business day? |
isBusinessDay() isBusinessDay(java.time.LocalDate date) isBusinessDay(java.lang.String date) |
Is the date a business day? |
isBusinessTime(DBDateTime time) |
Determines if the specified time is a business time. |
isLastBusinessDayOfMonth() isLastBusinessDayOfMonth(DBDateTime time) isLastBusinessDayOfMonth(java.lang.String date) |
Is the time listed the last day of the month, and is there time left in the business day? |
isLastBusinessDayOfWeek() isLastBusinessDayOfWeek(DBDateTime time) isLastBusinessDayOfWeek(java.lang.String date) |
Is the time listed the last day of the week, and is there time left in the business day? |
nextBusinessDay() nextBusinessDay(DBDateTime time) nextBusinessDay(java.lang.String date) |
Gets the next business day. |
nextBusinessDay(int n) nextBusinessDay(DBDateTime time, int n) nextBusinessDay(java.lang.String date, int n) |
Gets the next business date that is |
nextBusinessSchedule() nextBusinessSchedule(DBDateTime time) nextBusinessSchedule(java.lang.String date) |
Gets the next business schedule. A business schedule is both the date and the hours the business is open on that date.
|
nextBusinessSchedule(int n) nextBusinessSchedule(DBDateTime time, int n) nextBusinessSchedule(java.lang.String date, int n) |
Gets the next business schedule that is |
nextNonBusinessDay() nextNonBusinessDay(DBDateTime time) nextNonBusinessDay(java.lang.String date) |
Gets the next non-business day. |
nextNonBusinessDay(int n) nextNonBusinessDay(DBDateTime time, int n) nextNonBusinessDay(java.lang.String date, int n) |
Gets the next non-business date that is |
nonBusinessDaysInRange(DBDateTime start, DBDateTime end) nonBusinessDaysInRange(java.lang.String start, java.lang.String end) |
Returns the non-business days between start and end, inclusive. |
numberOfBusinessDays(DBDateTime start, DBDateTime end) numberOfBusinessDays(java.lang.String start, java.lang.String end) numberOfBusinessDays(DBDateTime start, DBDateTime end, boolean endInclusive) numberOfBusinessDays(java.lang.String start, java.lang.String end, boolean endInclusive) |
Returns the number of business days between start and end. |
numberOfNonBusinessDays(DBDateTime start, DBDateTime end) numberOfNonBusinessDays(DBDateTime start, DBDateTime end, boolean endInclusive) numberOfNonBusinessDays(java.lang.String start, java.lang.String end) numberOfNonBusinessDays(java.lang.String start, java.lang.String end, boolean endInclusive) |
Returns the number of non-business days between start and end. |
previousBusinessDay() previousBusinessDay(DBDateTime time) previousBusinessDay(java.lang.String date) |
Gets the previous business day. |
previousBusinessDay(int n) previousBusinessDay(DBDateTime time, int n) previousBusinessDay(java.lang.String date, int n) |
Gets the next business date that is |
previousBusinessSchedule() previousBusinessSchedule(DBDateTime time) previousBusinessSchedule(java.lang.String date) |
Gets the previous business schedule. |
previousBusinessSchedule(int n) previousBusinessSchedule(DBDateTime time, int n) previousBusinessSchedule(java.lang.String date, int n) |
Gets the next business schedule that is |
previousNonBusinessDay() previousNonBusinessDay(DBDateTime time) previousNonBusinessDay(java.lang.String date) |
Gets the previous non-business day. |
previousNonBusinessDay(int n) previousNonBusinessDay(DBDateTime time, int n) previousNonBusinessDay(java.lang.String date, int n) |
Gets the next non-business date that is |
standardBusinessDayLengthNanos() |
Returns the length of a standard business day in nanoseconds. |
Business Schedule Methods
A BusinessSchedule
is the collection of periods within a 24-hour day when a business is open. BusinessSchedules
may have multiple periods. If a business is open continuously from 8 a.m. to 9 p.m., it would be open for one period. However, if the business is closed daily for lunch, it would have two periods.
For example, on August 21, 2017, using the USNYSE
calendar, the BusinessSchedule
would look like this:
2017-08-21 9:30 NY to 2017-08-21 16:00 NY
Method |
Description |
---|---|
getBusinessPeriods() |
Gets the business periods for the day. |
getSOBD() getStartOfBusinessDay() |
Gets the starting time of the business day. |
getEOBD() getEndOfBusinessDay() |
Gets the end time of the business day. |
getLOBD() getLengthOfBusinessDay() |
Gets the length of the business day in nanoseconds. If the business day has multiple periods, only the time during the periods is counted. |
isBusinessDay() |
Is this day a business day? Returns true if yes; false otherwise. |
isBusinessTime(final DBDateTime time) |
Determines if the specified time is a business time for the day. Returns true if yes; false otherwise. |
businessTimeElapsed(final DBDateTime time) |
Returns the amount of business time in nanoseconds that has elapsed on the given day by the specified time. |
BusinessPeriod Methods
A BusinessPeriod
is a continuous block of time in which the business is open. Available methods give the start, end, and length of a BusinessPeriod
, as well as whether a time occurs during the period.
Method |
Description |
---|---|
getStartTime() |
Returns the start of the period. |
getEndTime() |
Returns the end of the period. |
getLength() |
Returns the length of the period in nanoseconds. |
contains(final DBDateTime time) |
Determines if the specified time is within the business period. |
DBTimeUtils
DBTimeUtils
can be used to create, manipulate, and gather information about DBDateTimes
, time/durations
, and DBPeriods
. All of the functions handle null values.
Method |
Description |
---|---|
long millis(DBDateTime dateTime) |
the datetime in milliseconds |
long nanos(DBDateTime dateTime) |
the datetime in nanoseconds |
boolean isBefore(DBDateTime d1, DBDateTime d2) |
tests whether |
boolean isAfter(DBDateTime d1, DBDateTime d2) |
tests whether |
DBDateTime plus(DBDateTime dateTime, long nanos) |
returns the sum of a |
DBDateTime plus(DBDateTime dateTime, DBPeriod period) |
returns the sum of a |
long minus(DBDateTime d1, DBDateTime d2) |
returns the difference in nanos between two |
long minus(DBDateTime d1, long nanos) |
returns the difference in nanos between two |
long diff(DBDateTime d1, DBDateTime d2) |
returns the difference in nanos from |
DBDateTime dateAtMidnight(DBDateTime dateTime, DBTimeZone timeZone) |
returns a |
String format(DBDateTime dateTime, DBTimeZone timeZone) |
returns a formatted String (date and time) for a |
String formatDate(DBDateTime dateTime, DBTimeZone timeZone) |
returns a formatted String (date only) for a |
String format(long nanos) |
returns a formatted String for this |
int dayOfMonth(DBDateTime dateTime, DBTimeZone timeZone) |
the day of the month for a |
int dayOfWeek(DBDateTime dateTime, DBTimeZone timeZone) |
the day of the week for a |
int dayOfYear(DBDateTime dateTime, DBTimeZone timeZone) |
the day of the year for a |
int hourOfDay(DBDateTime dateTime, DBTimeZone timeZone) |
the hour of the day for a |
int millisOfDay(DBDateTime dateTime, DBTimeZone timeZone) |
the milliseconds of the day for a |
int millisOfSecond(DBDateTime dateTime, DBTimeZone timeZone) |
the milliseconds of the second for a |
long nanosOfDay(DBDateTime dateTime, DBTimeZone timeZone) |
the nanos of the day for a |
long nanosOfSecond(DBDateTime dateTime, DBTimeZone timeZone) |
the nanos of the second for a |
int minuteOfDay(DBDateTime dateTime, DBTimeZone timeZone) |
the minute of the day for a |
int minuteOfHour(DBDateTime dateTime, DBTimeZone timeZone) |
the minute of the hour for a |
int monthOfYear(DBDateTime dateTime, DBTimeZone timeZone) |
the month of the year for a |
int secondOfDay(DBDateTime dateTime, DBTimeZone timeZone) |
the second of the day for a |
int secondOfMinute(DBDateTime dateTime, DBTimeZone timeZone) |
the second of the minute for a |
int year(DBDateTime dateTime, DBTimeZone timeZone) |
the year of a |
int yearOfCentury(DBDateTime dateTime, DBTimeZone timeZone) |
the year of the century for a |
final long millisToNanos(long millis) |
converts milliseconds to nanoseconds |
final long nanosToMillis(long nanos) |
converts nanoseconds to milliseconds |
final DBDateTime millisToTime(long millis) |
converts milliseconds into a |
final DBDateTime nanosToTime(long nanos) |
converts nanoseconds into a |
DBDateTime lowerBin(DBDateTime dateTime, long intervalNanos) |
bins the |
DBDateTime upperBin(DBDateTime dateTime, long intervalNanos) |
bins the |
DBDateTime convertDateTime(String s) |
parses a String into a |
long convertTime(String s) |
parses a String into a |
DBPeriod convertPeriod(String s) |
parses a String into a |
DBDateTime convertDateTimeQuiet(String s) |
parses a String into a |
long convertTimeQuiet(String s) |
parses a String into a |
DBPeriod convertPeriodQuiet(String s) |
parses a String into a |