Skip to content

Commit

Permalink
Fix nanoseconds
Browse files Browse the repository at this point in the history
  • Loading branch information
zadeviggers committed Oct 7, 2023
1 parent 3b127a4 commit faef377
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 44 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "miniseed",
"version": "0.1.5",
"version": "0.1.6",
"type": "module",
"scripts": {
"build": "vite build",
Expand Down
9 changes: 5 additions & 4 deletions src/miniseed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,17 @@ export function serialiseToMiniSEEDBuffer<T extends keyof typeof encodingTypes>(
let metadata = { ...metadataDefaults, ..._metadata };

if (metadata.startTime instanceof Date) {
// From https://stackoverflow.com/a/8619946
if (isNaN(metadata.startTime.getTime()))
throw new Error("Invalid Date provided for metadata.timestamp");
metadata.startTime = {
year: metadata.startTime.getFullYear(),
dayOfYear: getDayOfYear(metadata.startTime),
hour: metadata.startTime.getHours(),
minute: metadata.startTime.getMinutes(),
second: metadata.startTime.getSeconds(),
// Date's don't do nanoseconds :(
// One day we may get Temporal and all will be well
nanoSecond: 0,
// This isn't super precise.
// One day we may get Temporal and all will be well.
nanoSecond: metadata.startTime.getMilliseconds() * 1000000,
};
}

Expand Down
2 changes: 1 addition & 1 deletion tests/serialise.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe("Size", () => {
});
});

describe("Serialisation", () => {
describe("Basic serialisation", () => {
it("Works with basic text", () => {
const serialised = serialiseToMiniSEEDBuffer("beans", {
sourceIdentifier: "",
Expand Down
87 changes: 87 additions & 0 deletions tests/vaid-files.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { PromiseWithChild, exec as _exec } from "node:child_process";
import { promisify } from "node:util";
import { writeFile, mkdir, rm } from "node:fs/promises";
import { join } from "node:path";
import { describe, it, expect, beforeAll, afterAll } from "vitest";
import { serialiseToMiniSEEDUint8Array } from "../src/miniseed";

const exec = promisify(_exec);

const tempFolder = join(__dirname, "../test-temp/");

beforeAll(async () => {
await mkdir(tempFolder);
});

afterAll(async () => {
await rm(tempFolder, { recursive: true });
});

async function checkData(data: Uint8Array, name: string) {
const file = join(tempFolder, `${name}.mseed`);
await writeFile(file, data);
let error = false;
let child: PromiseWithChild<{ stdout: string; stderr: string }>["child"];
const promise = exec(`mseed3-validator ${file}`);
child = promise.child;
try {
const { stderr } = await promise;
expect(stderr).toBeFalsy();
} catch (err) {
error = true;
const stdout = child.stdout?.read();
const stderr = child.stderr?.read();
console.error(
name +
": " +
err +
(stdout
? "\nstdout: " + new TextDecoder().decode(stdout)
: "") +
(stdout ? "\nstderr: " + new TextDecoder().decode(stderr) : "")
);
}
expect(error).toBe(false);
}

describe("Basic data validity", () => {
it("Works with simple data", async () => {
const data = [1, 2, 3, 4, 5, 6, 7, 8];
const serialised = serialiseToMiniSEEDUint8Array(data, {
sourceIdentifier: "https://zade.viggers.net/example",
startTime: new Date(),
});
await checkData(serialised, "1-2-3");
});
});

describe("Date validity", () => {
it("Works with a 0 date", async () => {
const serialised = serialiseToMiniSEEDUint8Array([], {
sourceIdentifier: "https://zade.viggers.net/example",
startTime: new Date(0),
});
await checkData(serialised, "date-0-auto");
});
// it("Works with a manually set 0 date", async () => {
// const serialised = serialiseToMiniSEEDUint8Array([], {
// sourceIdentifier: "https://zade.viggers.net/example",
// startTime: {
// dayOfYear: 0,
// year: 0,
// hour: 0,
// minute: 0,
// second: 0,
// nanoSecond: 0,
// },
// });
// await checkData(serialised, "date-0-manual");
// });
it("Works with a new date right now", async () => {
const serialised = serialiseToMiniSEEDUint8Array([], {
sourceIdentifier: "https://zade.viggers.net/example",
startTime: new Date(),
});
await checkData(serialised, "date-utc");
});
});
38 changes: 0 additions & 38 deletions tests/vaid.test.ts

This file was deleted.

0 comments on commit faef377

Please sign in to comment.