Skip to content

Commit

Permalink
Don't convert timestamps to dates
Browse files Browse the repository at this point in the history
  • Loading branch information
domoritz committed Apr 2, 2024
1 parent 66fb984 commit 45a8a87
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 16 deletions.
2 changes: 1 addition & 1 deletion js/src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ type Timestamps = Type.Timestamp | Type.TimestampSecond | Type.TimestampMillisec
/** @ignore */
interface Timestamp_<T extends Timestamps = Timestamps> extends DataType<T> {
TArray: Int32Array;
TValue: number | Date;
TValue: number;
ArrayType: TypedArrayConstructor<Int32Array>;
}

Expand Down
6 changes: 1 addition & 5 deletions js/src/visitor/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,7 @@ const getDate = <T extends Date_>(data: Data<T>, index: number): T['TValue'] =>
/** @ignore */
const getTimestampSecond = <T extends TimestampSecond>({ values }: Data<T>, index: number): T['TValue'] => 1000 * epochMillisecondsLongToMs(values, index * 2);
/** @ignore */
const getTimestampMillisecond = <T extends TimestampMillisecond>({ values, type }: Data<T>, index: number): T['TValue'] => {
const value = epochMillisecondsLongToMs(values, index * 2);
// js dates are timezone agnostic so we only convert to date if there is no timezome
return type.timezone ? value : epochMillisecondsToDate(value);
};
const getTimestampMillisecond = <T extends TimestampMillisecond>({ values }: Data<T>, index: number): T['TValue'] => epochMillisecondsLongToMs(values, index * 2);
/** @ignore */
const getTimestampMicrosecond = <T extends TimestampMicrosecond>({ values }: Data<T>, index: number): T['TValue'] => epochMicrosecondsLongToMs(values, index * 2);
/** @ignore */
Expand Down
8 changes: 4 additions & 4 deletions js/src/visitor/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,13 @@ export const setDate = <T extends Date_>(data: Data<T>, index: number, value: T[
};

/** @ignore */
export const setTimestampSecond = <T extends TimestampSecond>({ values }: Data<T>, index: number, value: T['TValue']): void => setEpochMsToMillisecondsLong(values, index * 2, (value as number) / 1000);
export const setTimestampSecond = <T extends TimestampSecond>({ values }: Data<T>, index: number, value: T['TValue']): void => setEpochMsToMillisecondsLong(values, index * 2, value / 1000);
/** @ignore */
export const setTimestampMillisecond = <T extends TimestampMillisecond>({ values }: Data<T>, index: number, value: T['TValue']): void => setEpochMsToMillisecondsLong(values, index * 2, value instanceof Date ? value.getTime() : value);
export const setTimestampMillisecond = <T extends TimestampMillisecond>({ values }: Data<T>, index: number, value: T['TValue']): void => setEpochMsToMillisecondsLong(values, index * 2, value);
/** @ignore */
export const setTimestampMicrosecond = <T extends TimestampMicrosecond>({ values }: Data<T>, index: number, value: T['TValue']): void => setEpochMsToMicrosecondsLong(values, index * 2, value as number);
export const setTimestampMicrosecond = <T extends TimestampMicrosecond>({ values }: Data<T>, index: number, value: T['TValue']): void => setEpochMsToMicrosecondsLong(values, index * 2, value);
/** @ignore */
export const setTimestampNanosecond = <T extends TimestampNanosecond>({ values }: Data<T>, index: number, value: T['TValue']): void => setEpochMsToNanosecondsLong(values, index * 2, value as number);
export const setTimestampNanosecond = <T extends TimestampNanosecond>({ values }: Data<T>, index: number, value: T['TValue']): void => setEpochMsToNanosecondsLong(values, index * 2, value);
/* istanbul ignore next */
/** @ignore */
export const setTimestamp = <T extends Timestamp>(data: Data<T>, index: number, value: T['TValue']): void => {
Expand Down
5 changes: 1 addition & 4 deletions js/test/generate-test-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,10 +415,7 @@ function generateTimestamp<T extends Timestamp>(this: TestDataVectorGenerator, t
type.unit === TimeUnit.MICROSECOND ? 1000000 :
type.unit === TimeUnit.MILLISECOND ? 1000 : 1;
const data = createTimestamp(length, nullBitmap, multiple, values);
return {
values: () => type.unit === TimeUnit.MILLISECOND && !type.timezone ? values.map((x) => x == null ? null : new Date(x)) : values,
vector: new Vector([makeData({ type, length, nullCount, nullBitmap, data })])
};
return { values: () => values, vector: new Vector([makeData({ type, length, nullCount, nullBitmap, data })]) };
}

function generateTime<T extends Time>(this: TestDataVectorGenerator, type: T, length = 100, nullCount = Math.trunc(length * 0.2)): GeneratedVector<T> {
Expand Down
4 changes: 2 additions & 2 deletions js/test/unit/vector/date-vector-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@

import { DateDay, DateMillisecond, TimestampMillisecond, RecordBatchReader, Table, vectorFromArray } from 'apache-arrow';

describe(`TimeStampVector`, () => {
describe(`TimestampVector`, () => {
test(`Dates are stored in TimestampMillisecond`, () => {
const date = new Date('2023-02-01T12:34:56Z');
const vec = vectorFromArray([date]);
expect(vec.type).toBeInstanceOf(TimestampMillisecond);
expect(vec.get(0)).toBeInstanceOf(Date);
expect(vec.get(0)).toBe(date.valueOf());
});
});

Expand Down

0 comments on commit 45a8a87

Please sign in to comment.