UNPKG

patro

Version:

AD and BS Calendar

2 lines (1 loc) 15.7 kB
import{jsx as t,jsxs as e}from"react/jsx-runtime";import{useState as a,useEffect as r,useCallback as n,useMemo as o}from"react";const s="1921-04-13",h=1978,d=+(new Date).getFullYear(),_=+(new Date).getMonth()+1,i=+(new Date).getDate(),l={en:{months:{January:"Jan",February:"Feb",March:"Mar",April:"Apr",May:"May",June:"Jun",July:"Jul",August:"Aug",September:"Sep",October:"Oct",November:"Nov",December:"Dec"},days:{Sunday:"Sun",Monday:"Mon",Tuesday:"Tue",Wednesday:"Wed",Thursday:"Thu",Friday:"Fri",Saturday:"Sat"}},np:{months:{"बैशाख":"बै","जेष्ठ":"जे","आषाढ":"आषा","श्रावण":"श्रा","भाद्र":"भा","आश्विन":"आश","कार्तिक":"का","मंसिर":"मं","पौष":"पौ","माघ":"मा","फाल्गुन":"फा","चैत्र":"चै"},days:{"आइतवार":"आइ","सोमवार":"सोम","मंगलवार":"मंगल","बुधवार":"बुध","बिहिवार":"बिहि","शुक्रवार":"शुक्र","शनिवार":"शनि"},numbers:{0:"०",1:"१",2:"२",3:"३",4:"४",5:"५",6:"६",7:"७",8:"८",9:"९"}}},p=Object.values(l.en.days).length,c=[];c[1978]=[1978,31,31,32,31,31,31,30,29,30,29,30,30],c[1979]=[1979,31,31,32,32,31,30,30,29,30,29,30,30],c[1980]=[1980,31,32,31,32,31,30,30,30,29,29,30,31],c[1981]=[1981,31,31,31,32,31,31,29,30,30,29,30,30],c[1982]=[1982,31,31,32,31,31,31,30,29,30,29,30,30],c[1983]=[1983,31,31,32,32,31,30,30,29,30,29,30,30],c[1984]=[1984,31,32,31,32,31,30,30,30,29,29,30,31],c[1985]=[1985,31,31,31,32,31,31,29,30,30,29,30,30],c[1986]=[1986,31,31,32,31,31,31,30,29,30,29,30,30],c[1987]=[1987,31,32,31,32,31,30,30,29,30,29,30,30],c[1988]=[1988,31,32,31,32,31,30,30,30,29,29,30,31],c[1989]=[1989,31,31,31,32,31,31,30,29,30,29,30,30],c[1990]=[1990,31,31,32,31,31,31,30,29,30,29,30,30],c[1991]=[1991,31,32,31,32,31,30,30,29,30,29,30,30],c[1992]=[1992,31,32,31,32,31,30,30,30,29,30,29,31],c[1993]=[1993,31,31,31,32,31,31,30,29,30,29,30,30],c[1994]=[1994,31,31,32,31,31,31,30,29,30,29,30,30],c[1995]=[1995,31,32,31,32,31,30,30,30,29,29,30,30],c[1996]=[1996,31,32,31,32,31,30,30,30,29,30,29,31],c[1997]=[1997,31,31,32,31,31,31,30,29,30,29,30,30],c[1998]=[1998,31,31,32,31,31,31,30,29,30,29,30,30],c[1999]=[1999,31,32,31,32,31,30,30,30,29,29,30,31],c[2e3]=[2e3,30,32,31,32,31,30,30,30,29,30,29,31],c[2001]=[2001,31,31,32,31,31,31,30,29,30,29,30,30],c[2002]=[2002,31,31,32,32,31,30,30,29,30,29,30,30],c[2003]=[2003,31,32,31,32,31,30,30,30,29,29,30,31],c[2004]=[2004,30,32,31,32,31,30,30,30,29,30,29,31],c[2005]=[2005,31,31,32,31,31,31,30,29,30,29,30,30],c[2006]=[2006,31,31,32,32,31,30,30,29,30,29,30,30],c[2007]=[2007,31,32,31,32,31,30,30,30,29,29,30,31],c[2008]=[2008,31,31,31,32,31,31,29,30,30,29,29,31],c[2009]=[2009,31,31,32,31,31,31,30,29,30,29,30,30],c[2010]=[2010,31,31,32,32,31,30,30,29,30,29,30,30],c[2011]=[2011,31,32,31,32,31,30,30,30,29,29,30,31],c[2012]=[2012,31,31,31,32,31,31,29,30,30,29,30,30],c[2013]=[2013,31,31,32,31,31,31,30,29,30,29,30,30],c[2014]=[2014,31,31,32,32,31,30,30,29,30,29,30,30],c[2015]=[2015,31,32,31,32,31,30,30,30,29,29,30,31],c[2016]=[2016,31,31,31,32,31,31,29,30,30,29,30,30],c[2017]=[2017,31,31,32,31,31,31,30,29,30,29,30,30],c[2018]=[2018,31,32,31,32,31,30,30,29,30,29,30,30],c[2019]=[2019,31,32,31,32,31,30,30,30,29,30,29,31],c[2020]=[2020,31,31,31,32,31,31,30,29,30,29,30,30],c[2021]=[2021,31,31,32,31,31,31,30,29,30,29,30,30],c[2022]=[2022,31,32,31,32,31,30,30,30,29,29,30,30],c[2023]=[2023,31,32,31,32,31,30,30,30,29,30,29,31],c[2024]=[2024,31,31,31,32,31,31,30,29,30,29,30,30],c[2025]=[2025,31,31,32,31,31,31,30,29,30,29,30,30],c[2026]=[2026,31,32,31,32,31,30,30,30,29,29,30,31],c[2027]=[2027,30,32,31,32,31,30,30,30,29,30,29,31],c[2028]=[2028,31,31,32,31,31,31,30,29,30,29,30,30],c[2029]=[2029,31,31,32,31,32,30,30,29,30,29,30,30],c[2030]=[2030,31,32,31,32,31,30,30,30,29,29,30,31],c[2031]=[2031,30,32,31,32,31,30,30,30,29,30,29,31],c[2032]=[2032,31,31,32,31,31,31,30,29,30,29,30,30],c[2033]=[2033,31,31,32,32,31,30,30,29,30,29,30,30],c[2034]=[2034,31,32,31,32,31,30,30,30,29,29,30,31],c[2035]=[2035,30,32,31,32,31,31,29,30,30,29,29,31],c[2036]=[2036,31,31,32,31,31,31,30,29,30,29,30,30],c[2037]=[2037,31,31,32,32,31,30,30,29,30,29,30,30],c[2038]=[2038,31,32,31,32,31,30,30,30,29,29,30,31],c[2039]=[2039,31,31,31,32,31,31,29,30,30,29,30,30],c[2040]=[2040,31,31,32,31,31,31,30,29,30,29,30,30],c[2041]=[2041,31,31,32,32,31,30,30,29,30,29,30,30],c[2042]=[2042,31,32,31,32,31,30,30,30,29,29,30,31],c[2043]=[2043,31,31,31,32,31,31,29,30,30,29,30,30],c[2044]=[2044,31,31,32,31,31,31,30,29,30,29,30,30],c[2045]=[2045,31,32,31,32,31,30,30,29,30,29,30,30],c[2046]=[2046,31,32,31,32,31,30,30,30,29,29,30,31],c[2047]=[2047,31,31,31,32,31,31,30,29,30,29,30,30],c[2048]=[2048,31,31,32,31,31,31,30,29,30,29,30,30],c[2049]=[2049,31,32,31,32,31,30,30,30,29,29,30,30],c[2050]=[2050,31,32,31,32,31,30,30,30,29,30,29,31],c[2051]=[2051,31,31,31,32,31,31,30,29,30,29,30,30],c[2052]=[2052,31,31,32,31,31,31,30,29,30,29,30,30],c[2053]=[2053,31,32,31,32,31,30,30,30,29,29,30,30],c[2054]=[2054,31,32,31,32,31,30,30,30,29,30,29,31],c[2055]=[2055,31,31,32,31,31,31,30,29,30,29,30,30],c[2056]=[2056,31,31,32,31,32,30,30,29,30,29,30,30],c[2057]=[2057,31,32,31,32,31,30,30,30,29,29,30,31],c[2058]=[2058,30,32,31,32,31,30,30,30,29,30,29,31],c[2059]=[2059,31,31,32,31,31,31,30,29,30,29,30,30],c[2060]=[2060,31,31,32,32,31,30,30,29,30,29,30,30],c[2061]=[2061,31,32,31,32,31,30,30,30,29,29,30,31],c[2062]=[2062,31,31,31,32,31,31,29,30,29,30,29,31],c[2063]=[2063,31,31,32,31,31,31,30,29,30,29,30,30],c[2064]=[2064,31,31,32,32,31,30,30,29,30,29,30,30],c[2065]=[2065,31,32,31,32,31,30,30,30,29,29,30,31],c[2066]=[2066,31,31,31,32,31,31,29,30,30,29,29,31],c[2067]=[2067,31,31,32,31,31,31,30,29,30,29,30,30],c[2068]=[2068,31,31,32,32,31,30,30,29,30,29,30,30],c[2069]=[2069,31,32,31,32,31,30,30,30,29,29,30,31],c[2070]=[2070,31,31,31,32,31,31,29,30,30,29,30,30],c[2071]=[2071,31,31,32,31,31,31,30,29,30,29,30,30],c[2072]=[2072,31,32,31,32,31,30,30,29,30,29,30,30],c[2073]=[2073,31,32,31,32,31,30,30,30,29,29,30,31],c[2074]=[2074,31,31,31,32,31,31,30,29,30,29,30,30],c[2075]=[2075,31,31,32,31,31,31,30,29,30,29,30,30],c[2076]=[2076,31,32,31,32,31,30,30,30,29,29,30,30],c[2077]=[2077,31,32,31,32,31,30,30,30,29,30,29,31],c[2078]=[2078,31,31,31,32,31,31,30,29,30,29,30,30],c[2079]=[2079,31,31,32,31,31,31,30,29,30,29,30,30],c[2080]=[2080,31,32,31,32,31,30,30,30,29,29,30,30],c[2081]=[2081,31,31,32,32,31,30,30,30,29,30,30,30],c[2082]=[2082,30,32,31,32,31,30,30,30,29,30,30,30],c[2083]=[2083,31,31,32,31,31,30,30,30,29,30,30,30],c[2084]=[2084,31,31,32,31,31,30,30,30,29,30,30,30],c[2085]=[2085,31,32,31,32,30,31,30,30,29,30,30,30],c[2086]=[2086,30,32,31,32,31,30,30,30,29,30,30,30],c[2087]=[2087,31,31,32,31,31,31,30,30,29,30,30,30],c[2088]=[2088,30,31,32,32,30,31,30,30,29,30,30,30],c[2089]=[2089,30,32,31,32,31,30,30,30,29,30,30,30],c[2090]=[2090,30,32,31,32,31,30,30,30,29,30,30,30],c[2091]=[2091,31,31,32,31,31,31,30,30,29,30,30,30],c[2092]=[2092,30,31,32,32,31,30,30,30,29,30,30,30],c[2093]=[2093,30,32,31,32,31,30,30,30,29,30,30,30],c[2094]=[2094,31,31,32,31,31,30,30,30,29,30,30,30],c[2095]=[2095,31,31,32,31,31,31,30,29,30,30,30,30],c[2096]=[2096,30,31,32,32,31,30,30,29,30,29,30,30],c[2097]=[2097,31,32,31,32,31,30,30,30,29,30,30,30],c[2098]=[2098,31,31,32,31,31,31,29,30,29,30,29,31],c[2099]=[2099,31,32,31,32,31,30,30,30,29,29,30,31];const m=t=>{const{year:e,month:a,day:r}=Y(t);if((t=>t<h||t>2099)(e))throw new Error("BS Date out of range");if(((t,e,a)=>t>c[e][a])(r,e,a))throw new Error(`No ${r} day exits in this month.`);return I(e,a,r)},y=t=>{const[e,a,r]=m(t).split("-").map((t=>parseInt(t)));let n=0;for(let t=h;t<=e;t++)if(t===e){for(let e=1;e<a;e++)n+=c[t][e];n+=r-1}else for(let e=1;e<=12;e++)n+=c[t][e];return((t,e)=>{const a=new Date(t);a.setDate(a.getDate()+e);const r=a.getFullYear(),n=a.getMonth()+1,o=a.getDate();return`${r}-${n>9?n:"0"+n}-${o>9?o:"0"+o}`})(s,n)},{en:u,np:f}=l,D=t=>{for(let e=0;e<t.length;e++){const a=t[e].findIndex((t=>"०१"===t.extraDate));if(a>-1)return a}},g=(t,e)=>{if(t>12){const t=e+1;return{bsYearDates:c[t],bsMonthCount:c[t][1]}}if(t<1){const t=e-1;return{bsYearDates:c[t],bsMonthCount:c[t][12]}}return{bsYearDates:c[e],bsMonthCount:c[e][t]}},w=(...t)=>{const[e,a,r,n,o,s]=t,{year:h,month:d}=S(r,n);let _=[];const i=[],[l,c,m]=e.split("-"),u=D(v(h,d,o,s)),f=D(v(h,d+1,o,s)),{bsMonthCount:w}=g(parseInt(F(c))-1,parseInt(l)),{bsMonthCount:M}=g(parseInt(F(c)),parseInt(l)),{bsMonthCount:C}=g(parseInt(F(c))+1,parseInt(l)),b=((...t)=>{const[e,a,r,n]=t,{year:o,month:s}=S(r,n);return 0!=a?Array.from({length:e},((t,e)=>({dayOfMonth:j(String(e+1)),year:r,month:s,adDate:y(I(o,s,e+1)),bsDate:I(o,s,e+1),extraDate:y(I(o,s,e+1)).split("-")[2],type:"_patro_dates_date _patro_dates_current_month_date _patro_dates_date_offset _patro_dates_prev_month_date_offset"}))).slice(-a):[]})(w,u,l,+F(c)-1),x=((...t)=>{const[e,a,r]=t;return Array.from({length:e},((t,e)=>({dayOfMonth:j(String(e+1)),year:a,month:r,adDate:y(I(a,r,e+1)),bsDate:I(a,r,e+1),extraDate:y(I(a,r,e+1)).split("-")[2],type:"_patro_dates_date _patro_dates_current_month_date"})))})(M,l,+F(c)),A=((...t)=>{const[e,a,r,n]=t,{year:o,month:s}=S(r,n);return Array.from({length:e},((t,e)=>({dayOfMonth:j(String(e+1)),year:o,month:s+1,adDate:y(I(o,s,e+1)),bsDate:I(o,s,e+1),extraDate:y(I(o,s,e+1)).split("-")[2],type:"_patro_dates_date _patro_dates_current_month_date _patro_dates_date_offset _patro_dates_next_month_date_offset"}))).slice(0,p-a)})(C,f,l,+F(c)+1),N=[...b,...x,...A];return N.forEach(((t,e)=>{e%p!=0||(i.push(_),_=[]),_.push(t),e===N.length-1&&i.push(_)})),i},{np:M,en:C}=l,b=t=>{const e=new Date(s),a=new Date((t=>{const{year:e,month:a,day:r}=Y(t);if((t=>t>2040)(e))throw new Error("AD Date out of range");if((t=>t>31)(a))throw new Error("Month is invalid.");return I(e,a,r)})(t)),r=Math.floor((a.getTime()-e.getTime())/864e5);if(r<0)throw new Error("AD Date out of range");return(t=>{let e=0,a=0,r=0,n=0,o=!1;for(let s=h;s<2100&&!o;s++)for(let h=1;h<=12;h++)if(n+=c[s][h],t-n<0){r=t-n+c[s][h]+1,o=!0,e=s,a=h;break}return I(e,a,r)})(r)},v=(...t)=>{let e=[];const a=[],[r,n,o,s]=t,{year:h,month:d}=S(r,n),_=N(d-1,h),i=N(d,h),l=N(d+1,h),c=O(d,h),m=O(d+1,h),y=((...t)=>{const[e,a,r,n]=t,o=r-1<=0?"01":r-1;return 0!=a?Array.from({length:e},((t,e)=>({dayOfMonth:e+1,year:n,month:r-1,adDate:I(n,r-1,e+1),extraDate:j(b(I(n,+o,e+1)).split("-")[2]),type:"_patro_dates_date _patro_dates_current_month_date _patro_dates_date_offset _patro_dates_prev_month_date_offset"}))).slice(-a):[]})(_,c,d,h),u=((...t)=>{const[e,a,r,n]=t,{year:o,month:s}=S(n,r+1);return Array.from({length:e},((t,e)=>({dayOfMonth:e+1,year:o,month:s+1,adDate:I(o,s,e+1),extraDate:j(b(I(o,s,e+1)).split("-")[2]),type:"_patro_dates_date _patro_dates_current_month_date _patro_dates_date_offset _patro_dates_next_month_date_offset"}))).slice(0,p-a)})(l,m,d,h),f=[...y,...$(i,d,h,o,s),...u];return f.forEach(((t,r)=>{r%p!=0||(a.push(e),e=[]),e.push(t),r===f.length-1&&a.push(e)})),a},{en:x,np:A}=l,N=(t=_,e=d)=>new Date(e,t,0).getDate(),O=(t=_,e=d)=>+new Date(`${e}-${E(t)}-01`).getDay(),S=(t,e)=>e>12?{year:+t+1,month:1}:e<=0?{year:+t-1,month:12}:{year:t,month:e},$=(...t)=>{const[e,a,r,n,o]=t,{month:s}=o;return Array.from({length:e},((t,e)=>{const o=e+1,h=I(r,a,e+1),d=j(b(I(r,a,e+1)).split("-")[2]);return{dayOfMonth:o,year:r,month:a,adDate:h,extraDate:d,type:"_patro_dates_date _patro_dates_current_month_date "+(a===s&&n===e+1?"_patro_dates_current_date":"")}}))},E=t=>("0"+t).slice(-2),I=(t,e,a)=>`${t}-${E(e)}-${E(a)||"01"}`,Y=t=>{if(3!==t.split("-").length)throw new Error("Date format is invalid");const[e,a,r]=t.split("-").map((t=>parseInt(t)));if((t=>t>32||t<1||!t)(r))throw new Error("Day is invalid.");if((t=>t>12||t<1||!t)(a))throw new Error("Month is invalid.");if((t=>!t||t.toString().length<4)(e))throw new Error("Year is invalid.");return{year:e,month:a,day:r}},j=t=>t.split("").map((t=>A.numbers[t])).join(""),F=t=>t.replace(/^0+/,""),L=(t,...e)=>"AD"===t?((t,e,a,r)=>{const n=parseInt(F(t)),o=Object.keys(M.months),s=n-2;return{oMonth:`${o[s<0?11:s]}/${o[n-1]} - `,month:Object.keys(C.months)[e-1],oYear:a,year:r}})(...e):((t,e,a,r)=>{const n=parseInt(F(t)),o=Object.values(u.months),s=e-1;return{oMonth:`${o[s<0?11:s]}/${o[12===e?0:e]} - `,month:Object.keys(f.months)[n-1],oYear:r,year:a}})(...e),{en:k,np:T}=l,B=()=>t("svg",{width:"26",height:"26",viewBox:"0 0 20 20",fill:"none",xmlns:"http://www.w3.org/2000/svg",children:t("path",{d:"M12.9499 9.40832L8.23328 4.69999C8.15581 4.62188 8.06364 4.55989 7.96209 4.51758C7.86054 4.47527 7.75162 4.45349 7.64161 4.45349C7.5316 4.45349 7.42268 4.47527 7.32113 4.51758C7.21958 4.55989 7.12741 4.62188 7.04994 4.69999C6.89474 4.85613 6.80762 5.06734 6.80762 5.28749C6.80762 5.50764 6.89474 5.71885 7.04994 5.87499L11.1749 10.0417L7.04994 14.1667C6.89474 14.3228 6.80762 14.534 6.80762 14.7542C6.80762 14.9743 6.89474 15.1855 7.04994 15.3417C7.12712 15.4204 7.21916 15.483 7.32072 15.526C7.42229 15.5689 7.53135 15.5912 7.64161 15.5917C7.75187 15.5912 7.86094 15.5689 7.9625 15.526C8.06406 15.483 8.1561 15.4204 8.23328 15.3417L12.9499 10.6333C13.0345 10.5553 13.102 10.4606 13.1482 10.3552C13.1944 10.2497 13.2182 10.1359 13.2182 10.0208C13.2182 9.90574 13.1944 9.7919 13.1482 9.68648C13.102 9.58107 13.0345 9.48636 12.9499 9.40832Z",fill:"#121212"})}),J=()=>t("svg",{width:"26",height:"26",viewBox:"0 0 20 20",fill:"none",xmlns:"http://www.w3.org/2000/svg",children:t("path",{d:"M7.08325 10.6666L11.8333 15.3333C12.1666 15.6666 12.6666 15.6666 12.9999 15.3333C13.3333 15 13.3333 14.5 12.9999 14.1666L8.91659 9.99996L12.9999 5.83329C13.3333 5.49996 13.3333 4.99996 12.9999 4.66663C12.8333 4.49996 12.6666 4.41663 12.4166 4.41663C12.1666 4.41663 11.9999 4.49996 11.8333 4.66663L7.08325 9.33329C6.74992 9.74996 6.74992 10.25 7.08325 10.6666C7.08325 10.5833 7.08325 10.5833 7.08325 10.6666Z",fill:"#121212"})}),W=s=>{const{onChange:h,calendarType:l="AD",showExtra:p}=s,[c,m]=a(new Date),[y,u,f,D]=((t,e)=>{const[s,h]=a(d),[l,p]=a(_),[c,m]=a(i),[y,u]=a({month:_,year:d,date:i});r((()=>{e&&"getMonth"in e&&(p((()=>e.getMonth()+1)),h((()=>e.getFullYear())),m((()=>e.getDate())),u({month:e.getMonth(),year:e.getFullYear(),date:e.getDate()}))}),[]);const f=n((()=>{12===l?(h((t=>t+1)),p((()=>1))):p((t=>t+1))}),[l]),D=n((()=>{l<=1?(h((t=>t-1)),p((()=>12))):p((t=>t-1))}),[l]),g=o((()=>v(s,l,c,y)),[s,l,c]),M=o((()=>b(I(s,l,c))),[s,l,c]),[C,x]=M.split("-"),A={adDate:I(s,l,c),bsDate:M,headerMonths:{...L(t,x,l,C,String(s))}},N=o((()=>w(M,g,s,l,c,y)),[M,g,s,l,c,y]);return["BS"===t?N:g,A,f,D]})(l,c),{headerMonths:{month:g,oMonth:M,oYear:C,year:x}}=u||{};return t("div",{className:"App",children:e("div",{className:"_patro_container _patro",children:[e("header",{className:"_patro_header",children:[t("button",{className:"_patro_header_actionBtn",onClick:()=>D(),children:t(J,{})}),e("h3",{className:"_patro_header_info",children:[e("p",{className:"_patro_header_info_month_year",children:[g," ",x]}),e("p",{className:"_patro_header_info_month_year",children:[M," ",C]})]}),t("button",{className:"_patro_header_actionBtn",onClick:()=>f(),children:t(B,{})})]}),t("div",{className:"_patro_days",children:Object.values("AD"===l?k.days:T.days).map(((e,a)=>t("div",{className:"_patro_days_day",children:e},a+e)))}),y.map(((a,r)=>t("div",{className:"_patro_dates",children:Array.isArray(a)&&a.map(((a,n)=>e("p",{title:a.adDate,onClick:()=>(t=>{m((()=>new Date(t.adDate))),h&&h(t)})(a),className:a.type,children:[a.dayOfMonth,p&&t("sub",{children:a.extraDate})]},r+n)))},r)))]})})};export{W as Calendar};