Skip to main content

Time operations cheat sheet

Formats#

hh:mm:ss#

Deephaven time tables use timestamps in the hh:mm:ss format to specify ticking intervals.

from deephaven.TableTools import timeTable
result = timeTable('00:00:01')

yyyy-mm-ddT:hh:mm:ss.[millis|micros|nanos] TZ#

Deephaven time tables may also use the yyyy-mm-ddT:hh:mm:ss.[millis|micros|nanos] TZ format to specify a start date.

TZ represents the time zone. A few examples are:

  • UTC
  • NY (New York)
  • JP (Tokyo)
from deephaven.TableTools import timeTable
result = timeTable("2021-08-06T13:21:00 NY", "00:00:01")

Date-time constants#

Deephaven comes with some predefined constants:

  • DAY
  • HOUR
  • MINUTE
  • SECOND
  • WEEK
  • YEAR

These constants all show the time in nanoseconds.

from deephaven.DBTimeUtils import DAY, HOUR, MINUTE, SECOND, WEEK, YEAR
print(DAY)print(HOUR)print(MINUTE)print(SECOND)print(WEEK)print(YEAR)

See the Pydoc or Javadoc for further details.

Convert string to Deephaven date-time object#

Deephaven supports converting a string to a Deephaven date-time object via the convertDateTime method.

Strings should be in the yyyy-mm-ddT:hh:mm:ss.[millis|micros|nanos] TZ format.

from deephaven.DBTimeUtils import convertDateTime
dateTimeObject = convertDateTime("2021-07-04T08:00:00 NY")

Convert nanoseconds to Deephaven date-time object#

Deephaven supports converting a long representing nanoseconds since the Epoch to a Deephaven date-time object via the autoEpochToTime method.

from deephaven.DBTimeUtils import autoEpochToTime
dateTimeObject = autoEpochToTime(1631045972)

Date-time columns#

Deephaven tables have built-in support for Deephaven date-time objects via the dateTimeCol method.

from deephaven.DBTimeUtils import convertDateTimefrom deephaven.TableTools import newTable, dateTimeCol
firstTime = convertDateTime("2021-07-04T08:00:00 NY")secondTime = convertDateTime("2021-09-06T12:30:00 NY")thirdTime = convertDateTime("2021-12-25T21:15:00 NY")
result = newTable(    dateTimeCol("DateTimes", firstTime, secondTime, thirdTime))

Timestamp comparison#

Deephaven's filtering has support for timestamp comparisons.

note

Query strings require single quotes ' around timestamps.

from deephaven.DBTimeUtils import convertDateTimefrom deephaven.TableTools import newTable, dateTimeCol
firstTime = convertDateTime("2021-07-04T08:00:00 NY")secondTime = convertDateTime("2021-09-06T12:30:00 NY")thirdTime = convertDateTime("2021-12-25T21:15:00 NY")
result = newTable(    dateTimeCol("DateTimes", firstTime, secondTime, thirdTime))
filtered = result.where("DateTimes >= '2021-09-06T12:30:00 NY'")

See our How to use filters guide for more information.

Downsampling temporal data via time binning#

Downsampling time series data may be accomplished by calculating binning-intervals for time values and using appropriate aggregation methods, grouped by the binned interval.

from deephaven.DBTimeUtils import convertDateTime, upperBin, lowerBinfrom deephaven.TableTools import newTable, dateTimeCol, intColfrom deephaven import ComboAggregateFactory as caf
firstTime = convertDateTime("2021-09-06T12:29:58 NY")secondTime = convertDateTime("2021-09-06T12:30:00 NY")thirdTime = convertDateTime("2021-09-06T12:30:01 NY")fourthTime = convertDateTime("2021-09-06T12:30:05 NY")fifthTime = convertDateTime("2021-09-06T12:30:09 NY")
dataTable = newTable(    dateTimeCol("DateTimes", firstTime, secondTime, thirdTime, fourthTime, fifthTime),    intCol("DataCol", 0, 1, 2, 3, 4),)
binnedTable = dataTable.updateView("UpperBin=upperBin(DateTimes, 5 * SECOND)", "LowerBin=lowerBin(DateTimes, 5 * SECOND)")
groupedUpper = binnedTable.by(caf.AggCombo(caf.AggFirst("FirstDateTime=DateTimes"), caf.AggLast("LastDateTime=DateTimes"), caf.AggAvg("DataCol"), ), "UpperBin")
groupedLower = binnedTable.by(caf.AggCombo(caf.AggFirst("FirstDateTime=DateTimes"), caf.AggLast("LastDateTime=DateTimes"), caf.AggAvg("DataCol"), ), "LowerBin")