UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

2 lines • 2.52 kB
//!src/infra/format-time/format-datetime.ts let validatedTimezone;let timeZoneDayKeyFormatter;function resolveTimezone(e){try{if(e!==validatedTimezone){Intl.DateTimeFormat(`en-US`,{timeZone:e});validatedTimezone=e}return e}catch{return}}function createTimeZoneDayKeyFormatter(e){const t=typeof e===`string`&&timeZoneDayKeyFormatter?.timeZone===e?timeZoneDayKeyFormatter.formatter:new Intl.DateTimeFormat(`en-US-u-ca-iso8601-nu-latn`,{timeZone:e,year:`numeric`,month:`2-digit`,day:`2-digit`});if(typeof e===`string`)timeZoneDayKeyFormatter={timeZone:e,formatter:t};return e=>{const n=t.formatToParts(e);const pick=e=>n.find(t=>t.type===e)?.value;const r=pick(`year`);const i=pick(`month`);const a=pick(`day`);if(!r||!i||!a)throw new Error(`Intl.DateTimeFormat omitted required calendar-day parts`);return`${r.padStart(4,`0`)}-${i}-${a}`}}function resolveTimeZoneDayStartMs(e,t){const n=/^(\d{4})-(\d{2})-(\d{2})$/.exec(e);if(!n)return;const r=Date.UTC(Number(n[1]),Number(n[2])-1,Number(n[3]));const i=createTimeZoneDayKeyFormatter(t);const a=1728e5;let o=r-a;let s=r+a;while(o<s){const t=o+Math.floor((s-o)/2);if(i(new Date(t))<e)o=t+1;else s=t}return i(new Date(o))===e?o:void 0}let zonedTimestampFormatter;function formatUtcTimestamp(e,t){const n=String(e.getUTCFullYear()).padStart(4,`0`);const r=String(e.getUTCMonth()+1).padStart(2,`0`);const i=String(e.getUTCDate()).padStart(2,`0`);const a=String(e.getUTCHours()).padStart(2,`0`);const o=String(e.getUTCMinutes()).padStart(2,`0`);if(!t?.displaySeconds)return`${n}-${r}-${i}T${a}:${o}Z`;return`${n}-${r}-${i}T${a}:${o}:${String(e.getUTCSeconds()).padStart(2,`0`)}Z`}function formatZonedTimestamp(e,t){try{const{timeZone:n,displaySeconds:r=false,displayWeekday:i=false}=t??{};const a=`${n}|${r}|${i}`;let o=n!==void 0&&zonedTimestampFormatter?.key===a?zonedTimestampFormatter.formatter:void 0;if(!o){o=new Intl.DateTimeFormat(`en-US`,{timeZone:n,year:`numeric`,month:`2-digit`,day:`2-digit`,hour:`2-digit`,minute:`2-digit`,hourCycle:`h23`,timeZoneName:`short`,second:r?`2-digit`:void 0,weekday:i?`short`:void 0});if(n!==void 0)zonedTimestampFormatter={key:a,formatter:o}}const s=Object.fromEntries(o.formatToParts(e).map(({type:e,value:t})=>[e,t]));const{year:c,month:l,day:u,hour:d,minute:f,second:p,weekday:m}=s;const h=r?`:${p}`:``;return`${i?`${m} `:``}${c}-${l}-${u} ${d}:${f}${h} ${s.timeZoneName}`}catch{return}}export{resolveTimezone as a,resolveTimeZoneDayStartMs as i,formatUtcTimestamp as n,formatZonedTimestamp as r,createTimeZoneDayKeyFormatter as t};