@hakit/core
Version:
A collection of React hooks and helpers for Home Assistant to easily communicate with the Home Assistant WebSocket API.
3 lines (2 loc) • 7.1 kB
JavaScript
;Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const u=require("./subscribe/frontend_data.js"),_=Intl.DateTimeFormat?.().resolvedOptions?.().timeZone,T=_??"UTC",r=(t,e)=>t==="local"&&_?T:e;var l=(t=>(t.language="language",t.system="system",t.am_pm="12",t.twenty_four="24",t))(l||{}),M=(t=>(t.local="local",t.server="server",t))(M||{});const i=t=>{if(t.time_format==="language"||t.time_format==="system"){const e=t.time_format==="language"?t.language:void 0;return new Date("January 1, 2023 22:00:00").toLocaleString(e).includes("10")}return t.time_format==="12"},$=864e5,N=36e5,Z=6e4,E=1e3,I={ms:1,s:E,min:Z,h:N,d:$},v=(t,e)=>F(parseFloat(t)*I[e])||"0",g=(t,e=2)=>{let n=""+t;for(let o=1;o<e;o++)n=parseInt(n)<10**o?`0${n}`:n;return n};function F(t){const e=Math.floor(t/1e3/3600),n=Math.floor(t/1e3%3600/60),o=Math.floor(t/1e3%3600%60),a=Math.floor(t%1e3);return e>0?`${e}:${g(n)}:${g(o)}`:n>0?`${n}:${g(o)}`:o>0||a>0?`${o}${a>0?`.${g(a,3)}`:""}`:null}const W=/^\d{4}-(0[1-9]|1[0-2])-([12]\d|0[1-9]|3[01])[T| ](((([01]\d|2[0-3])((:?)[0-5]\d)?|24:?00)([.,]\d+(?!:))?)(\8[0-5]\d([.,]\d+)?)?([zZ]|([+-])([01]\d|2[0-3]):?([0-5]\d)?)?)$/,C=t=>W.test(t),z="^\\d{4}-(0[1-9]|1[0-2])-([12]\\d|0[1-9]|3[01])",L=new RegExp(z+"$"),P=new RegExp(z),Y=(t,e=!1)=>e?P.test(t):L.test(t);function O(t){return t?t instanceof Date&&!isNaN(t.valueOf()):!1}function m(t){let e=null,n=null;return(...o)=>(e&&e.length===o.length&&e.every((a,s)=>a===o[s])||(e=o,n=t(...o)),n)}const k=m((t,e)=>new Intl.DateTimeFormat(t.language,{year:"numeric",month:"long",day:"numeric",timeZone:r(t.time_zone,e)})),A=m((t,e)=>new Intl.DateTimeFormat(t.language,{hour:i(t)?"numeric":"2-digit",minute:"2-digit",hourCycle:i(t)?"h12":"h23",timeZone:r(t.time_zone,e)})),x=m((t,e)=>new Intl.DateTimeFormat(t.language,{hour:"2-digit",minute:"2-digit",hourCycle:"h23",timeZone:r(t.time_zone,e)})),V=m((t,e)=>new Intl.DateTimeFormat(t.language,{hour:i(t)?"numeric":"2-digit",hourCycle:i(t)?"h12":"h23",timeZone:r(t.time_zone,e)})),R=m((t,e)=>new Intl.DateTimeFormat(t.language,{minute:"2-digit",timeZone:r(t.time_zone,e)})),U=m((t,e)=>new Intl.DateTimeFormat(t.language,{second:"2-digit",timeZone:r(t.time_zone,e)})),H=m((t,e)=>new Intl.DateTimeFormat(t.language,{year:"numeric",month:"long",day:"numeric",hour:i(t)?"numeric":"2-digit",minute:"2-digit",hourCycle:i(t)?"h12":"h23",timeZone:r(t.time_zone,e)})),B=m((t,e)=>new Intl.DateTimeFormat(t.language,{year:"numeric",month:"long",day:"numeric",hour:i(t)?"numeric":"2-digit",minute:"2-digit",second:"2-digit",hourCycle:i(t)?"h12":"h23",timeZone:r(t.time_zone,e)})),q=m((t,e)=>new Intl.DateTimeFormat(t.language,{year:"numeric",month:"short",day:"numeric",hour:i(t)?"numeric":"2-digit",minute:"2-digit",hourCycle:i(t)?"h12":"h23",timeZone:r(t.time_zone,e)})),J=m((t,e)=>new Intl.DateTimeFormat(t.language,{month:"short",day:"numeric",hour:i(t)?"numeric":"2-digit",minute:"2-digit",hourCycle:i(t)?"h12":"h23",timeZone:r(t.time_zone,e)})),b=m(()=>new Intl.DateTimeFormat(void 0,{year:"numeric",month:"long",day:"numeric",hour:"2-digit",minute:"2-digit"})),G=m((t,e)=>new Intl.DateTimeFormat(t.language,{weekday:"long",month:"long",day:"numeric",timeZone:r(t.time_zone,e)})),K=m((t,e)=>new Intl.DateTimeFormat(t.language,{year:"numeric",month:"short",day:"numeric",timeZone:r(t.time_zone,e)})),Q=m((t,e)=>new Intl.DateTimeFormat(t.language,{day:"numeric",month:"short",timeZone:r(t.time_zone,e)})),X=m((t,e)=>new Intl.DateTimeFormat(t.language,{month:"long",year:"numeric",timeZone:r(t.time_zone,e)})),j=m((t,e)=>new Intl.DateTimeFormat(t.language,{month:"long",timeZone:r(t.time_zone,e)})),tt=m((t,e)=>new Intl.DateTimeFormat(t.language,{year:"numeric",timeZone:r(t.time_zone,e)})),et=m((t,e)=>new Intl.DateTimeFormat(t.language,{weekday:"long",timeZone:r(t.time_zone,e)})),nt=m((t,e)=>new Intl.DateTimeFormat(t.language,{weekday:"short",timeZone:r(t.time_zone,e)})),ot=m((t,e)=>{const n=t.date_format===u.DateFormat.system?void 0:t.language;return new Intl.DateTimeFormat(n,{year:"numeric",month:"numeric",day:"numeric",timeZone:r(t.time_zone,e)})}),rt=(t,e,n)=>k(n,e.time_zone).format(t),S=(t,e,n)=>A(n,e.time_zone).format(t),mt=(t,e,n)=>x(n,e.time_zone).format(t),at=(t,e,n)=>V(n,e.time_zone).formatToParts(t).find(a=>a.type==="hour")?.value||"",it=(t,e,n)=>R(n,e.time_zone).formatToParts(t).find(a=>a.type==="minute")?.value||"",st=(t,e,n)=>U(n,e.time_zone).formatToParts(t).find(a=>a.type==="second")?.value||"",ut=(t,e,n)=>H(n,e.time_zone).format(t),ft=(t,e,n)=>B(e,n.time_zone).format(t),w=(t,e,n)=>J(e,n.time_zone).format(t),p=(t,e,n)=>q(e,n.time_zone).format(t),ct=(t,e,n)=>new Date().getFullYear()===t.getFullYear()?w(t,e,n):p(t,e,n),dt=t=>b().format(t),gt=(t,e,n)=>`${new Intl.DateTimeFormat(e.language,{year:"numeric",month:"numeric",day:"numeric",timeZone:r(e.time_zone,n.time_zone)}).format(t)}, ${S(t,n,e)}`,ht=(t,e,n)=>G(e,n.time_zone).format(t),Dt=(t,e,n)=>K(e,n.time_zone).format(t),yt=(t,e,n)=>Q(e,n.time_zone).format(t),_t=(t,e,n)=>X(e,n.time_zone).format(t),Tt=(t,e,n)=>j(e,n.time_zone).format(t),lt=(t,e,n)=>tt(e,n.time_zone).format(t),Mt=(t,e,n)=>et(e,n.time_zone).format(t),It=(t,e,n)=>nt(e,n.time_zone).format(t),Ft=(t,e,n)=>{const o=ot(e,n.time_zone);if(e.date_format===u.DateFormat.language||e.date_format===u.DateFormat.system)return o.format(t);const a=o.formatToParts(t),s=a.find(f=>f.type==="literal")?.value||"/",c=a.find(f=>f.type==="day")?.value||"",h=a.find(f=>f.type==="month")?.value||"",D=a.find(f=>f.type==="year")?.value||"",y=a[a.length-1];let d=y?.type==="literal"?y.value:"";return e.language==="bg"&&e.date_format===u.DateFormat.YMD&&(d=""),{[u.DateFormat.DMY]:`${c}${s}${h}${s}${D}${d}`,[u.DateFormat.MDY]:`${h}${s}${c}${s}${D}${d}`,[u.DateFormat.YMD]:`${D}${s}${h}${s}${c}${d}`,[u.DateFormat.language]:o.format(t),[u.DateFormat.system]:o.format(t)}[e.date_format]},zt=(t,e,n)=>{try{return new Intl.DateTimeFormat(e.language,{hour:"numeric",hour12:!0,timeZone:r(e.time_zone,n.time_zone)}).formatToParts(t).find(c=>c.type==="dayPeriod")?.value||(t.getHours()>=12?"PM":"AM")}catch{return t.getHours()>=12?"PM":"AM"}};exports.LOCAL_TIME_ZONE=T;exports.TimeFormat=l;exports.TimeZone=M;exports.UNIT_TO_MILLISECOND_CONVERT=I;exports.checkValidDate=O;exports.formatAmPmSuffix=zt;exports.formatDate=rt;exports.formatDateMonth=Tt;exports.formatDateMonthYear=_t;exports.formatDateNumeric=Ft;exports.formatDateShort=Dt;exports.formatDateTime=ut;exports.formatDateTimeNumeric=gt;exports.formatDateTimeWithBrowserDefaults=dt;exports.formatDateTimeWithSeconds=ft;exports.formatDateVeryShort=yt;exports.formatDateWeekday=Mt;exports.formatDateWeekdayDay=ht;exports.formatDateWeekdayShort=It;exports.formatDateYear=lt;exports.formatDuration=v;exports.formatHour=at;exports.formatMinute=it;exports.formatSeconds=st;exports.formatShortDateTime=w;exports.formatShortDateTimeWithConditionalYear=ct;exports.formatShortDateTimeWithYear=p;exports.formatTime=S;exports.formatTimeWithoutAmPm=mt;exports.isDate=Y;exports.isTimestamp=C;exports.millisecondsToDuration=F;exports.resolveTimeZone=r;exports.shouldUseAmPm=i;
//# sourceMappingURL=date.js.map