From d7f9b1f5eec6e02c1f77e3169570e9ea51d4ce08 Mon Sep 17 00:00:00 2001 From: Gerard Burns Date: Sat, 20 Apr 2024 09:33:42 +0100 Subject: [PATCH] better date parsing for candlestick --- .../subjects/DatesAsNumbers.svelte | 3 +- .../subjects/StringsAsDates.svelte | 50 +++++++++++++++++++ .../settings/controls/FieldSelect.svelte | 2 +- packages/client/manifest.json | 44 ++++++++++++++-- .../app/charts/CandleStickChart.svelte | 43 +++++++++++++++- 5 files changed, 133 insertions(+), 9 deletions(-) create mode 100644 packages/builder/src/components/design/settings/controls/Explanation/DetailsModal/subjects/StringsAsDates.svelte diff --git a/packages/builder/src/components/design/settings/controls/Explanation/DetailsModal/subjects/DatesAsNumbers.svelte b/packages/builder/src/components/design/settings/controls/Explanation/DetailsModal/subjects/DatesAsNumbers.svelte index 3effc88640..7a2f1ed9ef 100644 --- a/packages/builder/src/components/design/settings/controls/Explanation/DetailsModal/subjects/DatesAsNumbers.svelte +++ b/packages/builder/src/components/design/settings/controls/Explanation/DetailsModal/subjects/DatesAsNumbers.svelte @@ -9,7 +9,6 @@ const updateTimeStamp = () => { timestamp = Date.now(); - console.log(timestamp); if (run) { setTimeout(updateTimeStamp, 200) } @@ -25,7 +24,7 @@
- A date can be used in place of a numeric value, but it will be parsed as a UNIX epoch timestamp, which is the number of milliseconds since Jan 1st 1970. A more recent moment in time will be a higher number. + A date can be used in place of a numeric value, but it will be parsed as a UNIX time timestamp, which is the number of milliseconds since Jan 1st 1970. A more recent moment in time will be a higher number.
+ import { onMount } from "svelte" + import { ExampleSection, ExampleLine, Block, Subject, Section } from './components' + + let timestamp = Date.now(); + + onMount(() => { + let run = true; + + const updateTimeStamp = () => { + timestamp = Date.now(); + console.log(timestamp); + if (run) { + setTimeout(updateTimeStamp, 200) + } + } + + updateTimeStamp(); + + return () => { + run = false; + } + }) + + + +
+ A date can be used in place of a numeric value, but it will be parsed as a UNIX epoch timestamp, which is the number of milliseconds since Jan 1st 1970. A more recent moment in time will be a higher number. +
+ + + + 1st Jan 2000: 946684800000 + + + 1st Jan 2020: 1577836800000 + + + Now: {timestamp} + + +
+ + diff --git a/packages/builder/src/components/design/settings/controls/FieldSelect.svelte b/packages/builder/src/components/design/settings/controls/FieldSelect.svelte index 9691b4caf2..28abd66764 100644 --- a/packages/builder/src/components/design/settings/controls/FieldSelect.svelte +++ b/packages/builder/src/components/design/settings/controls/FieldSelect.svelte @@ -110,7 +110,7 @@ offset={20} > { + const value = row[dateColumn] + + if (dataProvider?.schema?.[dateColumn]?.type === 'datetime') { + return Date.parse(value); + } + + // Unix epoch + if (typeof value === "number") { + return value; + } + + const isString = typeof value === "string"; + // "2025" could be either an ISO 8601 date time string or Unix time. + // There's no way to tell the user's intent without providing more + // granular controls. + // We'll just assume any string without dashes is Unix time. + + if (isString && value.includes("-")) { + const unixTime = Date.parse(value); + + if (isNaN(unixTime)) { + return null + } + + return unixTime + } + + if (isString) { + const unixTime = parseInt(value, 10); + + if (isNaN(unixTime)) { + return null + } + + return unixTime + } + + return null; + } + const getSeries = ( dataProvider, dateColumn, @@ -81,7 +122,7 @@ const close = parseFloat(row[closeColumn]) return [ - Date.parse(row[dateColumn]), + getValueAsUnixEpoch(dataProvider, dateColumn, row), isNaN(open) ? 0 : open, isNaN(high) ? 0 : high, isNaN(low) ? 0 : low,