From f9a77c5c27aede4e5978eb55d9b7af781b680a1d Mon Sep 17 00:00:00 2001 From: João Augusto Costa Branco Marado Torres Date: Tue, 24 Jun 2025 12:08:41 -0300 Subject: feat!: initial commit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: João Augusto Costa Branco Marado Torres --- src/utils/datetime.ts | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/utils/datetime.ts (limited to 'src/utils/datetime.ts') diff --git a/src/utils/datetime.ts b/src/utils/datetime.ts new file mode 100644 index 0000000..3a2cd25 --- /dev/null +++ b/src/utils/datetime.ts @@ -0,0 +1,43 @@ +export function toIso8601Full(date: Date): string { + const yearN = date.getFullYear(); + const isNegativeYear = yearN <= 0; + const year = isNegativeYear ? pad(1 - yearN, 6) : pad(yearN, 6); + const signedYear = (isNegativeYear ? "-" : "+") + year; + + const month = pad(date.getMonth() + 1); + const day = pad(date.getDate()); + const hour = pad(date.getHours()); + const minute = pad(date.getMinutes()); + const second = pad(date.getSeconds()); + const ms = pad(date.getMilliseconds(), 3); + + const dateString = + `${signedYear}-${month}-${day}T${hour}:${minute}:${second}.${ms}`; + const tzOffset = -date.getTimezoneOffset(); + if (tzOffset === 0) { + return `${dateString}Z`; + } else { + const offsetSign = tzOffset > 0 ? "+" : "-"; + const offsetHours = pad(Math.floor(Math.abs(tzOffset) / 60)); + const offsetMinutes = pad(Math.abs(tzOffset) % 60); + return `${dateString}${offsetSign}${offsetHours}:${offsetMinutes}`; + } +} + +export function toIso8601FullUTC(date: Date): string { + const yearN = date.getUTCFullYear(); + const isNegativeYear = yearN <= 0; + const year = isNegativeYear ? pad(1 - yearN, 6) : pad(yearN, 6); + const signedYear = (isNegativeYear ? "-" : "+") + year; + + const month = pad(date.getUTCMonth() + 1); + const day = pad(date.getUTCDate()); + const hour = pad(date.getUTCHours()); + const minute = pad(date.getUTCMinutes()); + const second = pad(date.getUTCSeconds()); + const ms = pad(date.getUTCMilliseconds(), 3); + + return `${signedYear}-${month}-${day}T${hour}:${minute}:${second}.${ms}Z`; +} + +const pad = (num: number, len = 2) => String(Math.abs(num)).padStart(len, "0"); -- cgit v1.2.3