UNPKG

@arcgis/core

Version:

ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API

3 lines (2 loc) 5.07 kB
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */ import{toStringEnumKey as e}from"./enum.js";import{replace as t}from"../core/string.js";import{UnknownTimeZone as n}from"../core/sql/UnknownTimeZone.js";import{DateTime as r,Zone as s,FixedOffsetZone as i,IANAZone as o}from"luxon";const a={TimeZoneNotRecognized:"Timezone identifier has not been recognized."};class d extends Error{constructor(e,n){super(t(a[e],n)),this.declaredRootClass="esri.arcade.arcadedate.dateerror",Error.captureStackTrace&&Error.captureStackTrace(this,d)}}function u(e,t,n){return e<t?e-t:e>n?e-n:0}function c(e,t,n){return e<t?t:e>n?n:e}class m{constructor(e){this._date=e,this.declaredRootClass="esri.arcade.arcadedate"}static fromParts(e=0,t=1,n=1,s=0,i=0,o=0,a=0,d){if(isNaN(e)||isNaN(t)||isNaN(n)||isNaN(s)||isNaN(i)||isNaN(o)||isNaN(a))return null;const l=r.local(e,t).daysInMonth;let f=r.fromObject({day:c(n,1,l),year:e,month:c(t,1,12),hour:c(s,0,23),minute:c(i,0,59),second:c(o,0,59),millisecond:c(a,0,999)},{zone:h(d)});return f=f.plus({months:u(t,1,12),days:u(n,1,l),hours:u(s,0,23),minutes:u(i,0,59),seconds:u(o,0,59),milliseconds:u(a,0,999)}),new m(f)}static get systemTimeZoneCanonicalName(){return Intl.DateTimeFormat().resolvedOptions().timeZone??"system"}static arcadeDateAndZoneToArcadeDate(e,t){const r=h(t);return e.isUnknownTimeZone||r===n.instance?m.fromParts(e.year,e.monthJS+1,e.day,e.hour,e.minute,e.second,e.millisecond,r):new m(e._date.setZone(r))}static dateJSToArcadeDate(e){return new m(r.fromJSDate(e,{zone:"system"}))}static dateJSAndZoneToArcadeDate(e,t="system"){const n=h(t);return new m(r.fromJSDate(e,{zone:n}))}static unknownEpochToArcadeDate(e){return new m(r.fromMillis(e,{zone:n.instance}))}static unknownDateJSToArcadeDate(e){return new m(r.fromMillis(e.getTime(),{zone:n.instance}))}static epochToArcadeDate(e,t="system"){const n=h(t);return new m(r.fromMillis(e,{zone:n}))}static dateTimeToArcadeDate(e){return new m(e)}clone(){return new m(this._date)}changeTimeZone(e){const t=h(e);return m.dateTimeToArcadeDate(this._date.setZone(t))}static dateTimeAndZoneToArcadeDate(e,t){const r=h(t);return e.zone===n.instance||r===n.instance?m.fromParts(e.year,e.month,e.day,e.hour,e.minute,e.second,e.millisecond,r):new m(e.setZone(r))}static nowToArcadeDate(e){const t=h(e);return new m(r.fromJSDate(new Date,{zone:t}))}static nowUTCToArcadeDate(){return new m(r.utc())}get isSystem(){return"system"===this.timeZone||this.timeZone===m.systemTimeZoneCanonicalName}equals(e){return this.isSystem&&e.isSystem?this.toNumber()===e.toNumber():this.isUnknownTimeZone===e.isUnknownTimeZone&&this._date.equals(e._date)}get isUnknownTimeZone(){return this._date.zone===n.instance}get isValid(){return this._date.isValid}get hour(){return this._date.hour}get second(){return this._date.second}get day(){return this._date.day}get dayOfWeekISO(){return this._date.weekday}get dayOfWeekJS(){let e=this._date.weekday;return e>6&&(e=0),e}get millisecond(){return this._date.millisecond}get monthISO(){return this._date.month}get weekISO(){return this._date.weekNumber}get yearISO(){return this._date.weekYear}get monthJS(){return this._date.month-1}get year(){return this._date.year}get minute(){return this._date.minute}get zone(){return this._date.zone}get timeZoneOffset(){return this.isUnknownTimeZone?0:this._date.offset}get timeZone(){if(this.isUnknownTimeZone)return"unknown";if("system"===this._date.zone.type)return"system";const e=this.zone;return"fixed"===e.type?0===e.fixed?"UTC":e.formatOffset(0,"short"):e.name}stringify(){return JSON.stringify(this.toJSDate())}plus(e){return new m(this._date.plus(e))}diff(e,t){return this._date.diff(e._date,t)[t]}toISODate(){return this._date.toISODate()}toISOString(e){return e?this._date.toISO({suppressMilliseconds:!0,includeOffset:!this.isUnknownTimeZone}):this._date.toISO({includeOffset:!this.isUnknownTimeZone})}toISOTime(e,t){return this._date.toISOTime({suppressMilliseconds:e,includeOffset:t&&!this.isUnknownTimeZone})}toFormat(e,t){return this.isUnknownTimeZone&&(e=e.replaceAll("Z","")),this._date.toFormat(e,t)}toJSDate(){return this._date.toJSDate()}toSQLValue(){return this._date.toFormat("yyyy-LL-dd HH:mm:ss")}toSQLWithKeyword(){return`timestamp '${this.toSQLValue()}'`}toDateTime(){return this._date}toNumber(){return this._date.toMillis()}getTime(){return this._date.toMillis()}toUTC(){return new m(this._date.toUTC())}toLocal(){return new m(this._date.toLocal())}toString(){return this.toISOString(!0)}static fromReaderAsTimeStampOffset(e){if(!e)return null;const t=r.fromISO(e,{setZone:!0});return new m(t)}}function h(t,r=!0){if(t instanceof s)return t;switch(e(t)){case"system":return"system";case"utc":return"UTC";case"unknown":return n.instance}if(/^[+-]?[0-9]{1,2}(:[0-9]{2})?$/.test(t)){const e=i.parseSpecifier("UTC"+(t.startsWith("+")||t.startsWith("-")?"":"+")+t);if(e)return e}const a=o.create(t);if(!a.isValid){if(r)throw new d("TimeZoneNotRecognized");return null}return a}export{m as ArcadeDate,d as ArcadeDateError,a as arcadeDateErrorMessages,h as createDateTimeZone};