UNPKG

@cloudquery/plugin-sdk-javascript

Version:

This is the high-level package to use for developing CloudQuery plugins in JavaScript

71 lines 2.23 kB
import { Timestamp as ArrowTimestamp, TimeUnit } from '@apache-arrow/esnext-esm'; import { DateTime } from 'luxon'; import { FormatError } from '../errors/errors.js'; import { isInvalid, NULL_VALUE } from './util.js'; export class Timestamp { _valid = false; _value = null; _unit = TimeUnit.NANOSECOND; constructor(unit, v) { this.value = v; if (unit) { this._unit = unit; } return this; } get dataType() { return new ArrowTimestamp(this._unit); } get valid() { return this._valid; } get value() { if (!this._valid) { return null; } return this._value; } set value(value) { if (isInvalid(value)) { this._valid = false; return; } if (value instanceof Timestamp) { this._valid = value.valid; this._value = value.value; return; } let dateValue = null; if (typeof value === 'string') { dateValue = DateTime.fromFormat(value, 'yyyy-MM-dd HH:mm:ss.SSSSSSSSS ZZZZ', { setZone: true }); if (!dateValue.isValid) { dateValue = DateTime.fromFormat(value, 'yyyy-MM-dd HH:mm:ss.SSSSSSSSS', { zone: 'utc' }); } if (!dateValue.isValid) { dateValue = DateTime.fromFormat(value, "yyyy-MM-dd HH:mm:ss.SSSSSSSSS'Z'", { zone: 'utc' }); } if (!dateValue.isValid) { dateValue = DateTime.fromISO(value, { setZone: true }); } } if (value instanceof DateTime) { dateValue = value; } if (value instanceof globalThis.Date) { dateValue = DateTime.fromJSDate(value, { zone: 'utc' }); } if (dateValue && dateValue.isValid) { this._value = dateValue.toJSDate(); this._valid = true; return; } throw new FormatError(`Unable to set Timestamp from value`, { props: { value } }); } toString() { if (this._valid) { return this._value.toISOString(); } return NULL_VALUE; } } //# sourceMappingURL=timestamp.js.map