@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
6 lines (5 loc) • 4.71 kB
JavaScript
/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.32/esri/copyright.txt for details.
*/
import{createDateTimeZone as t,ArcadeDate as e}from"../../arcade/ArcadeDate.js";import{getLocale as a}from"../../intl/locale.js";import{DateTime as r,FixedOffsetZone as s}from"luxon";function o(t){t=t.replaceAll(/LTS|LT|L{1,4}|l{1,4}/g,"[$&]");let e="";const a=/(\[[^[]*\])|(\\)?([Hh]mm(ss)?|Mo|M{1,4}|Do|DDDo|D{1,4}|d{2,4}|do?|w[o|w]?|W[o|W]?|Qo?|N{1,5}|YYYYYY|YYYYY|YYYY|YY|y{2,4}|yo?|gg(ggg?)?|GG(GGG?)?|e|E|a|A|hh?|HH?|kk?|mm?|ss?|S{1,9}|x|X|zz?|ZZ?|.)/g;for(const r of t.match(a)||[])switch(r){case"D":e+="d";break;case"DD":e+="dd";break;case"DDD":e+="o";break;case"d":e+="c";break;case"ddd":e+="ccc";break;case"dddd":e+="cccc";break;case"M":e+="L";break;case"MM":e+="LL";break;case"MMM":e+="LLL";break;case"MMMM":e+="LLLL";break;case"YY":e+="yy";break;case"Y":case"YYYY":e+="yyyy";break;case"Q":e+="q";break;case"X":case"x":e+=r;break;default:r.length>=2&&"["===r.slice(0,1)&&"]"===r.slice(-1)?e+=`'${r.slice(1,-1)}'`:e+=`'${r}'`}return e}const n="esri.core.sql.dateonly";class i{constructor(t,e,a){this._year=t,this._month=e,this._day=a,this.declaredRootClass=n}static isDateOnly(t){return"object"==typeof t&&null!=t&&"declaredRootClass"in t&&t.declaredRootClass===n}get month(){return this._month}get monthJS(){return this._month-1}get year(){return this._year}get day(){return this._day}get isValid(){return this.toDateTime("unknown").isValid}equals(t){return i.isDateOnly(t)&&t.day===this.day&&t.month===this.month&&t.year===this.year}clone(){return new i(this._year,this._month,this._day)}toDateTime(e){return r.fromObject({day:this.day,month:this.month,year:this.year},{zone:t(e)})}toDateTimeLuxon(e){return r.fromObject({day:this.day,month:this.month,year:this.year},{zone:t(e)})}toString(){return`${this.year.toString().padStart(4,"0")}-${this.month.toString().padStart(2,"0")}-${this.day.toString().padStart(2,"0")}`}toFormat(t=null,r=!0){if(null===t||""===t)return this.toString();if(r&&(t=o(t)),!t)return"";const s=this.toDateTime("unknown");return e.dateTimeToArcadeDate(s).toFormat(t,{locale:a(),numberingSystem:"latn"})}toArcadeDate(){const t=this.toDateTime("unknown");return e.dateTimeToArcadeDate(t)}toNumber(){return this.toDateTime("unknown").toMillis()}toJSDate(){return this.toDateTime("unknown").toJSDate()}toStorageFormat(){return this.toFormat("yyyy-LL-dd",!1)}toSQLValue(){return this.toFormat("yyyy-LL-dd",!1)}toSQLWithKeyword(){return"date '"+this.toFormat("yyyy-LL-dd",!1)+"'"}plus(t,e){return i.fromDateTime(this.toUTCDateTime().plus({[t]:e}))}toUTCDateTime(){return r.utc(this.year,this.month,this.day,0,0,0,0)}difference(t,e){switch(e.toLowerCase()){case"days":case"day":case"d":return this.toUTCDateTime().diff(t.toUTCDateTime(),"days").days;case"months":case"month":return this.toUTCDateTime().diff(t.toUTCDateTime(),"months").months;case"minutes":case"minute":case"m":return"M"===e?this.toUTCDateTime().diff(t.toUTCDateTime(),"months").months:this.toUTCDateTime().diff(t.toUTCDateTime(),"minutes").minutes;case"seconds":case"second":case"s":return this.toUTCDateTime().diff(t.toUTCDateTime(),"seconds").seconds;case"milliseconds":case"millisecond":case"ms":default:return this.toUTCDateTime().diff(t.toUTCDateTime(),"milliseconds").milliseconds;case"hours":case"hour":case"h":return this.toUTCDateTime().diff(t.toUTCDateTime(),"hours").hours;case"years":case"year":case"y":return this.toUTCDateTime().diff(t.toUTCDateTime(),"years").years}}static fromMilliseconds(t){const e=r.fromMillis(t,{zone:s.utcInstance});return e.isValid?i.fromParts(e.year,e.month,e.day):null}static fromSeconds(t){const e=r.fromSeconds(t,{zone:s.utcInstance});return e.isValid?i.fromParts(e.year,e.month,e.day):null}static fromReader(t){if(!t)return null;const e=t.split("-");return 3!==e.length?null:new i(parseInt(e[0],10),parseInt(e[1],10),parseInt(e[2],10))}static fromParts(t,e,a){const r=new i(t,e,a);return!1===r.isValid?null:r}static fromDateJS(t){return i.fromParts(t.getFullYear(),t.getMonth()+1,t.getDay())}static fromDateTime(t){return i.fromParts(t.year,t.month,t.day)}static fromSqlTimeStampOffset(t){return this.fromDateTime(t.toDateTime())}static fromString(t,e=null){if(""===t)return null;if(null===t)return null;const a=[];if(e)(e=o(e))&&a.push(e);else if(null===e||""===e){const e=r.fromISO(t,{setZone:!0});return e.isValid?i.fromParts(e.year,e.month,e.day):null}for(const s of a){const a=r.fromFormat(t,e??s);if(a.isValid)return new i(a.year,a.month,a.day)}return null}static fromNow(e="system"){const a=r.fromJSDate(new Date).setZone(t(e));return new i(a.year,a.month,a.day)}}export{i as DateOnly,o as standardizeDateOnlyFormat};