| 
									
										
										
										
											2020-10-04 04:40:16 +03:00
										 |  |  | /********************************************************************** | 
					
						
							|  |  |  | *  | 
					
						
							|  |  |  | * | 
					
						
							|  |  |  | * | 
					
						
							| 
									
										
										
										
											2020-10-06 01:32:33 +03:00
										 |  |  | **********************************************/  /* c8 ignore next 2 */ | 
					
						
							| 
									
										
										
										
											2020-10-04 04:40:16 +03:00
										 |  |  | ((typeof define)[0]=='u'?function(f){module.exports=f(require)}:define) | 
					
						
							|  |  |  | (function(require){ var module={} // make module AMD/node compatible...
 | 
					
						
							|  |  |  | /*********************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 23:50:05 +03:00
										 |  |  | var object = require('ig-object') | 
					
						
							| 
									
										
										
										
											2020-10-04 04:40:16 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*********************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 23:50:05 +03:00
										 |  |  | var DateMixin = | 
					
						
							|  |  |  | module.DateMixin = | 
					
						
							|  |  |  | object.Mixin('DateMixin', 'soft', { | 
					
						
							|  |  |  | 	timeStamp: function(...args){ | 
					
						
							|  |  |  | 		return (new this()).getTimeStamp(...args) }, | 
					
						
							|  |  |  | 	fromTimeStamp: function(ts){ | 
					
						
							|  |  |  | 		return (new this()).setTimeStamp(ts) }, | 
					
						
							| 
									
										
										
										
											2021-04-01 14:14:52 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-04 04:40:16 +03:00
										 |  |  | 	// convert string time period to milliseconds...
 | 
					
						
							| 
									
										
										
										
											2020-11-22 23:50:05 +03:00
										 |  |  | 	str2ms: function(str, dfl){ | 
					
						
							| 
									
										
										
										
											2020-10-04 04:40:16 +03:00
										 |  |  | 		dfl = dfl || 'ms' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-01 14:14:52 +03:00
										 |  |  | 		// number -- dfl unit...
 | 
					
						
							| 
									
										
										
										
											2020-10-04 04:40:16 +03:00
										 |  |  | 		if(typeof(str) == typeof(123)){ | 
					
						
							|  |  |  | 			var val = str | 
					
						
							|  |  |  | 			str = dfl | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-01 14:14:52 +03:00
										 |  |  | 		// 00:00:00:00:000 format...
 | 
					
						
							|  |  |  | 		} else if(str.includes(':')){ | 
					
						
							|  |  |  | 			var units = str.split(/\s*:\s*/g).reverse() | 
					
						
							|  |  |  | 			// parse units...
 | 
					
						
							|  |  |  | 			var ms = units[0].length == 3 ? | 
					
						
							|  |  |  | 				parseFloat(units.shift() || 0) | 
					
						
							|  |  |  | 				: 0 | 
					
						
							|  |  |  | 			var [s=0, m=0, h=0, d=0] = units | 
					
						
							|  |  |  | 			// merge...
 | 
					
						
							|  |  |  | 			return ((((parseFloat(d || 0)*24  | 
					
						
							|  |  |  | 				+ parseFloat(h || 0))*60  | 
					
						
							|  |  |  | 					+ parseFloat(m || 0))*60  | 
					
						
							|  |  |  | 						+ parseFloat(s || 0))*1000 + ms) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-02 11:25:42 +03:00
										 |  |  | 		// 00sec format...
 | 
					
						
							| 
									
										
										
										
											2020-10-04 04:40:16 +03:00
										 |  |  | 		} else { | 
					
						
							|  |  |  | 			var val = parseFloat(str) | 
					
						
							|  |  |  | 			str = str.trim() | 
					
						
							|  |  |  | 			// check if a unit is given...
 | 
					
						
							| 
									
										
										
										
											2020-10-07 03:03:41 +03:00
										 |  |  | 			str = str == val ?  | 
					
						
							|  |  |  | 				dfl  | 
					
						
							|  |  |  | 				: str } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// NOTE: this is a small hack to avoid overcomplicating the 
 | 
					
						
							|  |  |  | 		// 		pattern to still match the passed dfl unit...
 | 
					
						
							|  |  |  | 		str = ' '+str | 
					
						
							|  |  |  | 		var c =  | 
					
						
							|  |  |  | 			(/[^a-z](m(illi)?(-)?s(ec(ond(s)?)?)?)$/i.test(str) | 
					
						
							| 
									
										
										
										
											2022-02-23 23:55:27 +03:00
										 |  |  | 		   			|| /^([0-9]*\.)?[0-9]+$/.test(str) ) ?  | 
					
						
							| 
									
										
										
										
											2020-10-04 04:40:16 +03:00
										 |  |  | 				1 | 
					
						
							| 
									
										
										
										
											2020-10-07 03:03:41 +03:00
										 |  |  | 			: /[^a-z]s(ec(ond(s)?)?)?$/i.test(str) ?  | 
					
						
							| 
									
										
										
										
											2020-10-04 04:40:16 +03:00
										 |  |  | 				1000 | 
					
						
							| 
									
										
										
										
											2020-10-07 03:03:41 +03:00
										 |  |  | 			: /[^a-z]m(in(ute(s)?)?)?$/i.test(str) ?  | 
					
						
							| 
									
										
										
										
											2020-10-04 04:40:16 +03:00
										 |  |  | 				1000*60 | 
					
						
							| 
									
										
										
										
											2020-10-07 03:03:41 +03:00
										 |  |  | 			: /[^a-z]h(our(s)?)?$/i.test(str) ?  | 
					
						
							| 
									
										
										
										
											2020-10-04 04:40:16 +03:00
										 |  |  | 				1000*60*60 | 
					
						
							| 
									
										
										
										
											2020-10-07 03:03:41 +03:00
										 |  |  | 			: /[^a-z]d(ay(s)?)?$/i.test(str) ?  | 
					
						
							| 
									
										
										
										
											2020-10-04 04:40:16 +03:00
										 |  |  | 				1000*60*60*24 | 
					
						
							|  |  |  | 			: null | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return c ?  | 
					
						
							|  |  |  | 			val * c  | 
					
						
							| 
									
										
										
										
											2020-11-22 23:50:05 +03:00
										 |  |  | 			: NaN }, | 
					
						
							| 
									
										
										
										
											2021-04-01 14:14:52 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	isPeriod: function(str){ | 
					
						
							|  |  |  | 		return !isNaN(this.str2ms(str)) }, | 
					
						
							| 
									
										
										
										
											2022-02-23 23:55:27 +03:00
										 |  |  | 	isDateStr: function(str){ | 
					
						
							|  |  |  | 		return !isNaN(new Date(str).valueOf()) }, | 
					
						
							| 
									
										
										
										
											2020-11-22 23:50:05 +03:00
										 |  |  | }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-04 04:40:16 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 23:50:05 +03:00
										 |  |  | // XXX should this be flat???
 | 
					
						
							|  |  |  | var DateProtoMixin = | 
					
						
							|  |  |  | module.DateProtoMixin = | 
					
						
							|  |  |  | object.Mixin('DateProtoMixin', 'soft', { | 
					
						
							|  |  |  | 	toShortDate: function(show_ms){ | 
					
						
							|  |  |  | 		return ''  | 
					
						
							|  |  |  | 			+ this.getFullYear() | 
					
						
							|  |  |  | 			+'-'+ ('0'+(this.getMonth()+1)).slice(-2) | 
					
						
							|  |  |  | 			+'-'+ ('0'+this.getDate()).slice(-2) | 
					
						
							|  |  |  | 			+' '+ ('0'+this.getHours()).slice(-2) | 
					
						
							|  |  |  | 			+':'+ ('0'+this.getMinutes()).slice(-2) | 
					
						
							|  |  |  | 			+':'+ ('0'+this.getSeconds()).slice(-2) | 
					
						
							|  |  |  | 			+ (show_ms ?  | 
					
						
							|  |  |  | 				':'+(('000'+this.getMilliseconds()).slice(-3)) | 
					
						
							|  |  |  | 				: '') }, | 
					
						
							|  |  |  | 	getTimeStamp: function(show_ms){ | 
					
						
							|  |  |  | 		return ''  | 
					
						
							|  |  |  | 			+ this.getFullYear() | 
					
						
							|  |  |  | 			+ ('0'+(this.getMonth()+1)).slice(-2) | 
					
						
							|  |  |  | 			+ ('0'+this.getDate()).slice(-2) | 
					
						
							|  |  |  | 			+ ('0'+this.getHours()).slice(-2) | 
					
						
							|  |  |  | 			+ ('0'+this.getMinutes()).slice(-2) | 
					
						
							|  |  |  | 			+ ('0'+this.getSeconds()).slice(-2) | 
					
						
							|  |  |  | 			+ (show_ms ?  | 
					
						
							|  |  |  | 				('000'+this.getMilliseconds()).slice(-3) | 
					
						
							|  |  |  | 				: '') }, | 
					
						
							|  |  |  | 	setTimeStamp: function(ts){ | 
					
						
							|  |  |  | 		ts = ts.replace(/[^0-9]*/g, '') | 
					
						
							|  |  |  | 		this.setFullYear(ts.slice(0, 4)) | 
					
						
							|  |  |  | 		this.setMonth(ts.slice(4, 6)*1-1) | 
					
						
							|  |  |  | 		this.setDate(ts.slice(6, 8)) | 
					
						
							|  |  |  | 		this.setHours(ts.slice(8, 10)) | 
					
						
							|  |  |  | 		this.setMinutes(ts.slice(10, 12)) | 
					
						
							|  |  |  | 		this.setSeconds(ts.slice(12, 14)) | 
					
						
							|  |  |  | 		this.setMilliseconds(ts.slice(14, 17) || 0) | 
					
						
							|  |  |  | 		return this }, | 
					
						
							|  |  |  | }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //---------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // NOTE: repatching a date should not lead to any side effects as this
 | 
					
						
							|  |  |  | // 		does not add any state...
 | 
					
						
							|  |  |  | // NOTE: this is done differently as there are contexts where there may 
 | 
					
						
							|  |  |  | // 		be multiple Date objects in different contexts (nw/electron/..)
 | 
					
						
							|  |  |  | var patchDate = | 
					
						
							|  |  |  | module.patchDate =  | 
					
						
							|  |  |  | function(date){ | 
					
						
							|  |  |  | 	date = date || Date | 
					
						
							|  |  |  | 	DateMixin(date) | 
					
						
							|  |  |  | 	DateProtoMixin(date.prototype) | 
					
						
							| 
									
										
										
										
											2020-10-04 04:40:16 +03:00
										 |  |  | 	return date } | 
					
						
							| 
									
										
										
										
											2020-11-22 23:50:05 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //---------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-04 04:40:16 +03:00
										 |  |  | // patch the root date...
 | 
					
						
							|  |  |  | patchDate() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /********************************************************************** | 
					
						
							|  |  |  | * vim:set ts=4 sw=4 :                               */ return module }) |