Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: replace @js-joda/core with temporal in Date #1473

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -43,6 +43,7 @@
"@azure/identity": "^2.0.4",
"@azure/keyvault-keys": "^4.4.0",
"@js-joda/core": "^5.2.0",
"@js-temporal/polyfill": "^0.4.2",
"@types/es-aggregate-error": "^1.0.2",
"bl": "^5.0.0",
"es-aggregate-error": "^1.0.8",
Expand Down
25 changes: 14 additions & 11 deletions src/data-types/date.ts
@@ -1,12 +1,11 @@
import { DataType } from '../data-type';
import { ChronoUnit, LocalDate } from '@js-joda/core';
import { Temporal } from '@js-temporal/polyfill';

// globalDate is to be used for JavaScript's global 'Date' object to avoid name clashing with the 'Date' constant below
const globalDate = global.Date;
const EPOCH_DATE = LocalDate.ofYearDay(1, 1);
const EPOCH_DATE = new Temporal.PlainDate(1, 1, 1);
const NULL_LENGTH = Buffer.from([0x00]);
const DATA_LENGTH = Buffer.from([0x03]);

console.log(EPOCH_DATE.toString());
const Date: DataType = {
id: 0x28,
type: 'DATEN',
Expand Down Expand Up @@ -37,27 +36,31 @@ const Date: DataType = {

let date;
if (options.useUTC) {
date = LocalDate.of(value.getUTCFullYear(), value.getUTCMonth() + 1, value.getUTCDate());
date = new Temporal.PlainDate(value.getUTCFullYear(), value.getUTCMonth() + 1, value.getUTCDate());
} else {
date = LocalDate.of(value.getFullYear(), value.getMonth() + 1, value.getDate());
date = new Temporal.PlainDate(value.getFullYear(), value.getMonth() + 1, value.getDate());
}

const days = EPOCH_DATE.until(date, ChronoUnit.DAYS);
const days = EPOCH_DATE.until(date).days;
const buffer = Buffer.alloc(3);
buffer.writeUIntLE(days, 0, 3);
yield buffer;
},

// TODO: value is techincally of type 'unknown'.
validate: function(value): null | Date {
validate: function(value): null | Temporal.PlainDate {
if (value == null) {
return null;
}

if (!(value instanceof globalDate)) {
value = new globalDate(globalDate.parse(value));
try {
if (!(value instanceof Temporal.PlainDate)) {
value = Temporal.PlainDate.from(value);
}
} catch {
throw new TypeError('Invalid date.');
}


if (isNaN(value)) {
throw new TypeError('Invalid date.');
}
Expand Down