Comparing version 2.0.5 to 2.0.6
import { Date } from "./Date"; | ||
import { Locale } from "./Locale"; | ||
import { TimeSpan } from "./TimeSpan"; | ||
import { TimeZone } from "./TimeZone"; | ||
export declare type DateTime = string; | ||
@@ -11,3 +12,23 @@ export declare namespace DateTime { | ||
function now(): DateTime; | ||
function localize(value: DateTime | globalThis.Date, locale?: Locale, timezone?: string): DateTime; | ||
interface Format { | ||
localeMatcher?: "best fit" | "lookup" | undefined; | ||
weekday?: "long" | "short" | "narrow" | undefined; | ||
era?: "long" | "short" | "narrow" | undefined; | ||
year?: "numeric" | "2-digit" | undefined; | ||
month?: "numeric" | "2-digit" | "long" | "short" | "narrow" | undefined; | ||
day?: "numeric" | "2-digit" | undefined; | ||
hour?: "numeric" | "2-digit" | undefined; | ||
minute?: "numeric" | "2-digit" | undefined; | ||
second?: "numeric" | "2-digit" | undefined; | ||
timeZoneName?: "short" | "long" | "shortOffset" | "longOffset" | "shortGeneric" | "longGeneric" | undefined; | ||
formatMatcher?: "best fit" | "basic" | undefined; | ||
hour12?: boolean | undefined; | ||
timeZone?: TimeZone | undefined; | ||
} | ||
function localize(value: DateTime | globalThis.Date, format: Format, locale?: Locale): string; | ||
function localize(value: DateTime | globalThis.Date, locale?: Locale, timeZone?: TimeZone): string; | ||
function timeZone(value: DateTime): TimeZone | ""; | ||
type Precision = "hours" | "minutes" | "seconds" | "milliseconds"; | ||
function precision(value: DateTime): Precision; | ||
function truncate(value: DateTime, precision: Precision): DateTime; | ||
function epoch(value: DateTime | globalThis.Date, resolution?: "days" | "hours" | "minutes" | "seconds" | "milliseconds"): number; | ||
@@ -14,0 +35,0 @@ function next(time: DateTime, span?: number | TimeSpan): DateTime; |
@@ -0,10 +1,42 @@ | ||
import { Date } from "./Date"; | ||
import { TimeZone } from "./TimeZone"; | ||
export var DateTime; | ||
(function (DateTime) { | ||
function isHours(v) { | ||
return (v[0] >= "0" && v[0] <= "1" && v[1] >= "0" && v[1] <= "9") || (v[0] == "2" && v[1] >= "0" && v[1] <= "3"); | ||
} | ||
function isMinutes(v) { | ||
return v[0] >= "0" && v[0] <= "5" && v[1] >= "0" && v[1] <= "9"; | ||
} | ||
function isSeconds(v) { | ||
return (v[0] >= "0" && v[0] <= "5" && v[1] >= "0" && v[1] <= "9") || v == "60" || v == "61"; | ||
} | ||
function is(value) { | ||
const length = value.length; | ||
return (typeof value == "string" && | ||
/^(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+)|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d)|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d)$/.test(value)); | ||
length >= 13 && | ||
length <= 29 && | ||
Date.is(value.substring(0, 10)) && | ||
value[10] == "T" && | ||
isHours(value.substring(11, 13)) && | ||
(length == 13 || | ||
TimeZone.is(value.substring(13)) || | ||
(value[13] == ":" && | ||
length >= 16 && | ||
isMinutes(value.substring(14, 16)) && | ||
(length == 16 || | ||
TimeZone.is(value.substring(16)) || | ||
(value[16] == ":" && | ||
length >= 19 && | ||
isSeconds(value.substring(17, 19)) && | ||
(length == 19 || | ||
TimeZone.is(value.substring(19)) || | ||
(value[19] == "." && | ||
length >= 23 && | ||
[...value.substring(20, 23)].every(c => c >= "0" && c <= "9") && | ||
(length == 23 || TimeZone.is(value.substring(23)))))))))); | ||
} | ||
DateTime.is = is; | ||
function parse(value) { | ||
return new globalThis.Date(value); | ||
return new globalThis.Date(DateTime.truncate(value, "milliseconds")); | ||
} | ||
@@ -34,15 +66,83 @@ DateTime.parse = parse; | ||
DateTime.now = now; | ||
function localize(value, locale, timezone) { | ||
const localeString = locale ? locale : Intl.DateTimeFormat().resolvedOptions().locale; | ||
return (is(value) ? parse(value) : value).toLocaleString(localeString, { | ||
year: "numeric", | ||
month: "2-digit", | ||
day: "2-digit", | ||
hour: "2-digit", | ||
minute: "2-digit", | ||
second: "2-digit", | ||
timeZone: timezone ?? Intl.DateTimeFormat().resolvedOptions().timeZone, | ||
}); | ||
function localize(value, locale, timeZone) { | ||
let result; | ||
if (typeof locale == "object") { | ||
const localeString = timeZone ? timeZone : Intl.DateTimeFormat().resolvedOptions().locale; | ||
result = (is(value) ? parse(value) : value).toLocaleString(localeString, locale); | ||
} | ||
else { | ||
const precision = is(value) ? DateTime.precision(value) : "milliseconds"; | ||
result = localize(value, { | ||
year: "numeric", | ||
month: "2-digit", | ||
day: "2-digit", | ||
hour: "2-digit", | ||
minute: precision == "minutes" || precision == "seconds" || precision == "milliseconds" ? "2-digit" : undefined, | ||
second: precision == "seconds" || precision == "milliseconds" ? "2-digit" : undefined, | ||
timeZone: timeZone, | ||
}, locale); | ||
} | ||
return result; | ||
} | ||
DateTime.localize = localize; | ||
function timeZone(value) { | ||
const result = value[value.length - 1] == "Z" ? "Z" : value.substring(value.length - 6); | ||
return TimeZone.is(result) ? result : ""; | ||
} | ||
DateTime.timeZone = timeZone; | ||
function precision(value) { | ||
const zone = timeZone(value); | ||
const time = value.substring(0, value.length - zone.length).split("T", 2)[1]; | ||
let result; | ||
switch (time.length) { | ||
case 2: | ||
result = "hours"; | ||
break; | ||
case 5: | ||
result = "minutes"; | ||
break; | ||
case 8: | ||
result = "seconds"; | ||
break; | ||
default: | ||
case 12: | ||
result = "milliseconds"; | ||
break; | ||
} | ||
return result; | ||
} | ||
DateTime.precision = precision; | ||
function truncate(value, precision) { | ||
const zone = timeZone(value); | ||
let [date, time] = value.split("T", 2); | ||
time = time.substring(0, time.length - zone.length); | ||
switch (time.length) { | ||
case 2: | ||
time += ":00:00.000"; | ||
break; | ||
case 5: | ||
time += ":00.000"; | ||
break; | ||
case 8: | ||
time += ".000"; | ||
break; | ||
} | ||
let result; | ||
switch (precision) { | ||
case "hours": | ||
result = time.substring(0, 2); | ||
break; | ||
case "minutes": | ||
result = time.substring(0, 5); | ||
break; | ||
case "seconds": | ||
result = time.substring(0, 8); | ||
break; | ||
case "milliseconds": | ||
result = time.substring(0, 12); | ||
break; | ||
} | ||
return date + "T" + result + zone; | ||
} | ||
DateTime.truncate = truncate; | ||
function epoch(value, resolution = "seconds") { | ||
@@ -49,0 +149,0 @@ let result = (typeof value == "string" ? parse(value) : value).getTime(); |
{ | ||
"name": "isoly", | ||
"version": "2.0.5", | ||
"version": "2.0.6", | ||
"description": "Datatypes and functions specified by ISO-standards.", | ||
@@ -5,0 +5,0 @@ "author": "Utily Contributors", |
Sorry, the diff of this file is not supported yet
913710
217
18866