Skip to content

Commit

Permalink
datetime improved.
Browse files Browse the repository at this point in the history
  • Loading branch information
dojyorin committed Apr 25, 2024
1 parent d446437 commit 159a04e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 43 deletions.
53 changes: 25 additions & 28 deletions src/pure/time.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import {textPadZero} from "./text.ts";

function dateFormat(date:string){
const [y, m, d, h, mi, s] = date.split(/[/ :TZ_.-]/i).map(v => Number(v));

return new Date(y, (m ?? 1) - 1, d ?? 1, h ?? 0, mi ?? 0, s ?? 0);
}

/**
* Wait for specified time.
* Return actual elapsed wait time.
Expand All @@ -17,56 +23,47 @@ export async function delay(time:number):Promise<number>{
}

/**
* UNIX time in seconds.
* UNIX time from `Date` or formatted datetime string.
* If no args will be current time.
* Note that in seconds not milliseconds.
* @example
* ```ts
* const time = timeEncodeEpoch();
* const date = timeDecodeEpoch(time);
* const time = timeEncode();
* const date = timeDecode(time);
* ```
*/
export function timeEncodeEpoch(date?:Date):number{
return Math.floor((date ?? new Date()).getTime() / 1000);
export function timeEncode(dt?:Date | string):number{
return Math.floor((dt instanceof Date ? dt : typeof dt === "string" ? dateFormat(dt) : new Date()).getTime() / 1000);
}

/**
* `Date` from UNIX time.
* `Date` from UNIX time or formatted datetime string.
* Note that in seconds not milliseconds.
* @example
* ```ts
* const time = timeEncodeEpoch();
* const date = timeDecodeEpoch(time);
* const time = timeEncode();
* const date = timeDecode(time);
* ```
*/
export function timeDecodeEpoch(time:number):Date{
return new Date(time * 1000);
}

/**
* Convert from formatted datetime string such as ISO8601 to UNIX time in seconds.
* @example
* ```ts
* const time = timeParseEpoch("2023-05-18T08:31:32.292Z");
* ```
*/
export function timeParseEpoch(ds:string):number{
const [y, m, d, h, mi, s] = ds.split(/[/ :TZ_.-]/i).map(v => Number(v));

return timeEncodeEpoch(new Date(y, (m ?? 1) - 1, d ?? 1, h ?? 0, mi ?? 0, s ?? 0));
export function timeDecode(dt?:number | string):Date{
switch(typeof dt){
case "string": return dateFormat(dt);
case "number": return new Date(dt * 1000);
default: return new Date();
}
}

/**
* Generate serialized string from current or any `Date` to "yyyyMMddhhmmss".
* Generate serialized string from current or any `Date` or UNIX time to "yyyyMMddhhmmss".
* @example
* ```ts
* const format = timeSerial();
* const format = timeFormatSerialize();
* ```
*/
export function timeSerial(date?:Date, split?:boolean):string{
const d = date ?? new Date();
export function timeFormatSerialize(dt?:Date | number | string, split?:boolean):string{
const ss = split ? "/" : "";
const sc = split ? ":" : "";
const date = dt instanceof Date ? dt : timeDecode(dt);

return `${d.getFullYear()}${ss}${textPadZero(d.getMonth() + 1)}${ss}${textPadZero(d.getDate())}${split ? " " : ""}${textPadZero(d.getHours())}${sc}${textPadZero(d.getMinutes())}${sc}${textPadZero(d.getSeconds())}`;
return `${date.getFullYear()}${ss}${textPadZero(date.getMonth() + 1)}${ss}${textPadZero(date.getDate())}${split ? " " : ""}${textPadZero(date.getHours())}${sc}${textPadZero(date.getMinutes())}${sc}${textPadZero(date.getSeconds())}`;
}
4 changes: 2 additions & 2 deletions src/pure_ext/excel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {type RawWorkBook, type RawWorkSheet, type RawWorkCell, xlsxcp, set_cptable, xlsxRead, xlsxWrite, xlsxUtil} from "../../deps.pure_ext.ts";
import {timeSerial} from "../pure/time.ts";
import {timeFormatSerialize} from "../pure/time.ts";

export type {RawWorkBook, RawWorkSheet, RawWorkCell};

Expand Down Expand Up @@ -116,7 +116,7 @@ export function excelDecode(data:Uint8Array, cp?:number, pw?:string):Record<stri
}
else if(column.v instanceof Date){
column.v.setMinutes(new Date().getTimezoneOffset());
columns.push(timeSerial(column.v, true));
columns.push(timeFormatSerialize(column.v, true));
}
else{
columns.push(`${column.v}`);
Expand Down
17 changes: 4 additions & 13 deletions test/pure/time.test.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
import {assertEquals} from "../../deps.test.ts";
import {delay, timeEncodeEpoch, timeDecodeEpoch, timeParseEpoch, timeSerial} from "../../src/pure/time.ts";
import {delay, timeEncode, timeDecode, timeFormatSerialize} from "../../src/pure/time.ts";

const sample = new Date(2000, 0, 1, 0, 0, 0, 0);

Deno.test({
name: "Time: Encode and Decode",
fn(){
const encode = timeEncodeEpoch(sample);
const decode = timeDecodeEpoch(encode);
const encode = timeEncode(sample);
const decode = timeDecode(encode);

assertEquals(decode, sample);
}
});

Deno.test({
name: "Time: Parse",
fn(){
const result = timeParseEpoch(sample.toISOString());

assertEquals(result, 946684800);
}
});

Deno.test({
name: "Time: Delay",
async fn(){
Expand All @@ -34,7 +25,7 @@ Deno.test({
Deno.test({
name: "Time: Serial",
fn(){
const result = timeSerial(sample);
const result = timeFormatSerialize(sample);

assertEquals(result, "20000101000000");
}
Expand Down

0 comments on commit 159a04e

Please sign in to comment.