Comparing version 2.0.19 to 2.0.20
@@ -12,2 +12,7 @@ import { Date } from "./Date"; | ||
function create(value: globalThis.Date): DateTime; | ||
/** | ||
* Return local time with offset. | ||
* Note: During DST-change, this might be wrong. | ||
*/ | ||
function fromLocalDateTime(localDateTime: DateTime, timeZone: TimeZone): string; | ||
function now(): DateTime; | ||
@@ -14,0 +19,0 @@ type Format = Intl.DateTimeFormatOptions; |
@@ -79,2 +79,26 @@ import { Date } from "./Date"; | ||
DateTime.create = create; | ||
/** | ||
* Return local time with offset. | ||
* Note: During DST-change, this might be wrong. | ||
*/ | ||
function fromLocalDateTime(localDateTime, timeZone) { | ||
// Cut off any time-zone-information: | ||
// TODO: Use the information, and just change offset. | ||
localDateTime = localDateTime.replace(/(Z|([+-].{5}))?$/, ""); | ||
// Create a Date object with the specified time as UTC | ||
const utcDateTime = new globalThis.Date(`${localDateTime}Z`); | ||
const localDate = new globalThis.Date(utcDateTime.toLocaleString("sv-SE", { timeZone: timeZone }).replace(" ", "T") + "Z"); | ||
// Calculate the time difference in minutes | ||
const diffInMinutes = (localDate.getTime() - utcDateTime.getTime()) / 60000; | ||
// Calculate the timezone's offset in hours and minutes | ||
const offsetHours = Math.floor(Math.abs(diffInMinutes) / 60) | ||
.toString() | ||
.padStart(2, "0"); | ||
const offsetMinutes = (Math.abs(diffInMinutes) % 60).toString().padStart(2, "0"); | ||
// Create the timezone string | ||
const timeZoneString = `${diffInMinutes >= 0 ? "+" : "-"}${offsetHours}:${offsetMinutes}`; | ||
// Return the formatted date string with timezone information | ||
return `${localDateTime}${timeZoneString}`; | ||
} | ||
DateTime.fromLocalDateTime = fromLocalDateTime; | ||
function now() { | ||
@@ -81,0 +105,0 @@ return create(new globalThis.Date()); |
@@ -1,2 +0,2 @@ | ||
/** IANA format. */ | ||
/** IANA format. Takes any time zone Stockholm, London and UTC is only examples. */ | ||
export type TimeZone = "Europe/Stockholm" | "Europe/London" | "UTC" | (string & Record<never, never>); | ||
@@ -3,0 +3,0 @@ export declare namespace TimeZone { |
{ | ||
"name": "isoly", | ||
"version": "2.0.19", | ||
"version": "2.0.20", | ||
"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
916650
18874