turbocommons-ts
Version:
General purpose library that implements frequently used and generic software development tasks
1 lines • 5.4 kB
JavaScript
;Object.defineProperty(exports,"__esModule",{value:!0}),exports.DateTimeObject=void 0;var StringUtils_1=require("../utils/StringUtils"),NumericUtils_1=require("../utils/NumericUtils"),DateTimeObject=function(){function t(t){void 0===t&&(t=""),this._dateTimeString="",this._dateTimeStringExploded=[],StringUtils_1.StringUtils.isEmpty(t)?this._dateTimeStringExploded=this._explodeISO8601String((new Date).toISOString()):this._dateTimeStringExploded=this._explodeISO8601String(this._dateTimeString)}return t.isValidDateTime=function(t){if(StringUtils_1.StringUtils.isString(t)&&t.substr(-1).match(/^[a-z0-9]+$/i)){if(t.match(/^([\+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24\:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d([\.,]\d+)?)?([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/)){var e=t.split("-");if(e.length>=3){var r=new Date(Number(e[0]),Number(e[1]),Number(e[2].substr(0,2)));return r.getMonth()+1===Number(e[1])&&r.getDate()===Number(e[2].substr(0,2))&&r.getFullYear()===Number(e[0])}return!0}}return"function"==typeof t},t.isEqual=function(e,r){return 0===t.compare(e,r)},t.getMonthName=function(t){if(!NumericUtils_1.NumericUtils.isNumeric(t)||t>12||t<1)throw new Error("Provided value is not a valid month number between 1 and 12");return["JANUARY","FEBRUARY","MARCH","APRIL","MAY","JUNE","JULY","AUGUST","SEPTEMBER","OCTOBER","NOVEMBER","DECEMBER"][t-1]},t.getDayName=function(t){if(!NumericUtils_1.NumericUtils.isNumeric(t)||t>7||t<1)throw new Error("Provided value is not a valid day number between 1 and 7");return["SUNDAY","MONDAY","TUESDAY","WEDNESDAY","THURSDAY","FRIDAY","SATURDAY"][t-1]},t.getCurrentYear=function(){return(new Date).getFullYear()},t.getCurrentMonth=function(){return(new Date).getMonth()+1},t.getCurrentDay=function(){return(new Date).getDate()},t.getCurrentDayOfWeek=function(){},t.getCurrentHour=function(){return(new Date).getHours()},t.getCurrentMinute=function(){return(new Date).getMinutes()},t.getCurrentSecond=function(){return(new Date).getSeconds()},t.getCurrentMiliSecond=function(){return Math.round((new Date).getMilliseconds()/1e3)},t.getCurrentMicroSecond=function(){},t.getCurrentTimeZoneOffset=function(){return(new Date).getTimezoneOffset()},t.compare=function(e,r){if(t.isValidDateTime(e)&&t.isValidDateTime(r)){StringUtils_1.StringUtils.isString(e)&&(e=new t(e)),StringUtils_1.StringUtils.isString(r)&&(r=new t(r)),e.setUTC(),r.setUTC();var i=e.toString(),n=r.toString();if(i===n)return 0;var o=[i,n];return o.sort(),o[0]===i?2:1}throw new Error("Provided value is not a valid ISO 8601 date time format")},t.prototype.getYear=function(){return Number(this._dateTimeStringExploded[0])},t.prototype.getMonth=function(){return Number(this._dateTimeStringExploded[1])},t.prototype.getDay=function(){return Number(this._dateTimeStringExploded[2])},t.prototype.getDayOfWeek=function(){},t.prototype.getHour=function(){return Number(this._dateTimeStringExploded[3])},t.prototype.getMinute=function(){return Number(this._dateTimeStringExploded[4])},t.prototype.getSecond=function(){return Number(this._dateTimeStringExploded[5])},t.prototype.getMiliSecond=function(){return Math.round(this.getMicroSecond()/1e3)},t.prototype.getMicroSecond=function(){return Number(this._dateTimeStringExploded[6])},t.prototype.getTimeZoneOffset=function(){return new Date(this._dateTimeString).getTimezoneOffset()},t.prototype.getFirstDayOfMonth=function(){},t.prototype.getLastDayOfMonth=function(){},t.prototype.setLocalTimeZone=function(){},t.prototype.isUTC=function(){return 0===this.getTimeZoneOffset()},t.prototype.setUTC=function(){return this._dateTimeStringExploded=this._explodeISO8601String(this._dateTimeString),this},t.prototype.toString=function(t){void 0===t&&(t="Y-M-DTH:N:S.UOffset");var e=this._dateTimeStringExploded;return t=StringUtils_1.StringUtils.replace(t,"Offset",e[7].substr(0)),t=StringUtils_1.StringUtils.replace(t,"Y",e[0]),t=StringUtils_1.StringUtils.replace(t,"y",e[0].substr(2)),t=StringUtils_1.StringUtils.replace(t,"M",e[1]),t=StringUtils_1.StringUtils.replace(t,"m",String(Number(e[1]))),t=StringUtils_1.StringUtils.replace(t,"D",e[2]),t=StringUtils_1.StringUtils.replace(t,"d",String(Number(e[2]))),t=StringUtils_1.StringUtils.replace(t,"H",e[3]),t=StringUtils_1.StringUtils.replace(t,"h",String(Number(e[3]))),t=StringUtils_1.StringUtils.replace(t,"N",e[4]),t=StringUtils_1.StringUtils.replace(t,"n",String(Number(e[4]))),t=StringUtils_1.StringUtils.replace(t,"S",e[5]),t=StringUtils_1.StringUtils.replace(t,"s",String(Number(e[5]))),t=StringUtils_1.StringUtils.replace(t,"u",StringUtils_1.StringUtils.pad(String(Math.min(999,Math.round(Number(e[6])/1e3))),3,"0")),StringUtils_1.StringUtils.replace(t,"U",StringUtils_1.StringUtils.pad(e[6],6,"0","RIGHT"))},t.prototype.compareTo=function(e){return t.compare(this,e)},t.prototype.isEqualTo=function(e){return 0===t.compare(this,e)},t.prototype._explodeISO8601String=function(t){var e=["","01","01","00","00","00","000000",""];"z"===t.substr(t.length-1,1).toLowerCase()&&(t=t.substr(0,t.length-1)+"+00:00");for(var r=t.split(/[+-.: TZ]/),i=0;r.length>0&&i<6;)e[i]=r.shift(),i++;var n=r.length;return 1!==n&&3!==n||(e[6]=r.shift()+"000"),2!==n&&3!==n||(e[7]=t.substr(t.length-6,1)+r[0]+":"+r[1]),e},t}();exports.DateTimeObject=DateTimeObject;