datex.js 1.0.10 → 1.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,135 @@
1
+ import {periodKey,periodTime} from './map/period';
2
+
3
+ export default function(datex,proto){
4
+
5
+ Object.assign(proto,{
6
+ toDate(){
7
+ return this._date;
8
+ },
9
+ toObject(){
10
+ let _ = this._date;
11
+ return {
12
+ 'year':_.getFullYear(),
13
+ 'month':_.getMonth()+1,
14
+ 'day':_.getDate(),
15
+ 'hour':_.getHours(),
16
+ 'minute':_.getMinutes(),
17
+ 'second':_.getSeconds(),
18
+ 'millsecond':_.getMilliseconds(),
19
+ 'timestamp':_.getTime(),
20
+ 'week':_.getDay()
21
+ };
22
+ },
23
+ toArray(){
24
+ let $ = this.toObject();
25
+ return periodKey.map(name=>$[name]);
26
+ },
27
+ toString(){
28
+ return this._date.toString();
29
+ },
30
+ toISOString(){
31
+ return this._date.toISOString();
32
+ },
33
+ getTime(){
34
+ return this._date.getTime();
35
+ },
36
+ getUnix(){
37
+ return ~~(this._date.getTime()/1000);
38
+ },
39
+ clone(){
40
+ let that = this;
41
+ let clone = datex(this.getTime());
42
+ Object.getOwnPropertyNames(that).forEach(function(name){
43
+ if(name!='_date'){
44
+ clone[name] = that[name];
45
+ }
46
+ });
47
+ return clone;
48
+ },
49
+ isValid(){
50
+ return !isNaN(this.getTime());
51
+ },
52
+ set(unit,value){
53
+ let _ = this._date;
54
+ let $ = this.toObject();
55
+ switch (unit) {
56
+ case 'year':
57
+ _.setFullYear(value);
58
+ break;
59
+ case 'month':
60
+ _.setMonth(value-1);
61
+ break;
62
+ case 'day':
63
+ _.setDate(value);
64
+ break;
65
+ case 'hour':
66
+ _.setHours(value);
67
+ break;
68
+ case 'minute':
69
+ _.setMinutes(value);
70
+ break;
71
+ case 'second':
72
+ _.setSeconds(value);
73
+ break;
74
+ case 'millsecond':
75
+ _.setMilliseconds(value);
76
+ break;
77
+ case 'timestamp':
78
+ _.setTime(value);
79
+ break;
80
+ case 'week':
81
+ _.setDate($.day-$.week+value);
82
+ break;
83
+ }
84
+ return this;
85
+ },
86
+ change(unit,value){
87
+ let $ = this.toObject();
88
+ if(typeof periodTime[unit]!='undefined'){
89
+ return this.set('timestamp',$['timestamp']+value*periodTime[unit]);
90
+ }else{
91
+ return this.set(unit,$[unit]+value);
92
+ }
93
+ },
94
+ get(unit){
95
+ let $ = this.toObject();
96
+ return $[unit];
97
+ },
98
+ format(pattern = 'YYYY-MM-DD HH:mm:ss'){
99
+ let that = this.clone();
100
+ let _ = that._date;
101
+ let $ = that.toObject();
102
+ let match = _.toTimeString().match(/GMT([\+\-])(\d{2})(\d{2})/);
103
+ let map = {
104
+ 'YYYY':''+$.year,
105
+ 'YY':(''+$.year).padStart(2,'0'),
106
+ 'MM':(''+$.month).padStart(2,'0'),
107
+ 'M':''+$.month,
108
+ 'DD':(''+$.day).padStart(2,'0'),
109
+ 'D':''+$.day,
110
+ 'HH':(''+$.hour).padStart(2,'0'),
111
+ 'H':''+$.hour,
112
+ 'hh':(''+($.hour%12)).padStart(2,'0'),
113
+ 'h':''+($.hour%12),
114
+ 'mm':(''+$.minute).padStart(2,'0'),
115
+ 'm':''+$.minute,
116
+ 'ss':(''+$.second).padStart(2,'0'),
117
+ 's':''+$.second,
118
+ 'S':''+(~~(($.millsecond%1000)/100)),
119
+ 'SS':''+(~~(($.millsecond%1000)/10)),
120
+ 'SSS':''+($.millsecond%1000),
121
+ 'Z':match[1]+match[2]+':'+match[3],
122
+ 'ZZ':match[1]+match[2]+match[3],
123
+ 'A':['AM','PM'][~~($.hour/12)],
124
+ 'a':['am','pm'][~~($.hour/12)],
125
+ 'X':$.timestamp,
126
+ 'x':~~($.timestamp/1000),
127
+ 'Q':''+(~~($.month/3)),
128
+ 'W':$.week
129
+ };
130
+ return pattern.replace(/Y+|M+|D+|H+|h+|m+|s+|S+|Z+|A|a|X|x|Q|W+/g,function(key){
131
+ return map[key]||'';
132
+ });
133
+ }
134
+ });
135
+ };
@@ -0,0 +1,22 @@
1
+ export default function(datex,proto){
2
+
3
+ Object.assign(proto,{
4
+ isAfter(that,unit = 'timestamp'){
5
+ that = datex(that);
6
+ return this.get(unit)>that.get(unit);
7
+ },
8
+ isBefore(that,unit = 'timestamp'){
9
+ that = datex(that);
10
+ return this.get(unit)<that.get(unit);
11
+ },
12
+ isBetween(startDate,endDate,unit = 'timestamp'){
13
+ startDate = datex(startDate);
14
+ endDate = datex(endDate);
15
+ return this.get(unit)>startDate.get(unit)&&this.get(unit)<endDate.get(unit);
16
+ },
17
+ isSame(that,unit = 'timestamp'){
18
+ that = datex(that);
19
+ return this.get(unit)==that.get(unit);
20
+ }
21
+ });
22
+ };
@@ -0,0 +1,64 @@
1
+ import {periodKey,periodValue,periodTime} from './map/period';
2
+
3
+ export default function(datex,proto){
4
+
5
+ Object.assign(proto,{
6
+ diffWith(that,unit){
7
+ that = datex(that);
8
+ if(!that.isValid()){
9
+ return false;
10
+ }
11
+ let timestamp = this.getTime()-that.getTime();
12
+ let value = 0;
13
+ if(unit){
14
+ if(periodTime[unit]){
15
+ value = ~~(timestamp/periodTime[unit]);
16
+ }else if(unit=='month'){
17
+ let this_month = 12*(this.get('year')-1)+this.get('month');
18
+ let that_month = 12*(that.get('year')-1)+that.get('month');
19
+ value = this_month -that_month;
20
+ if(value<0&&this.get('day')>that.get('day')){
21
+ value+=1;
22
+ }else if(value>0&&this.get('day')<that.get('day')){
23
+ value-=1;
24
+ }
25
+ }else if(unit=='year'){
26
+ value = this.get('year') - that.get('year');
27
+ if(value<0&&(this.get('month')>that.get('month')||this.get('month')==that.get('month')&&this.get('day')>that.get('day'))){
28
+ value+=1;
29
+ }else if(value>0&&(this.get('month')<that.get('month')||this.get('month')==that.get('month')&&this.get('day')<that.get('day'))){
30
+ value-=1;
31
+ }
32
+ }
33
+ return value;
34
+ }else{
35
+ let clone = this.clone();
36
+ let hash = {};
37
+ periodKey.forEach(function(unit){
38
+ hash[unit] = clone.diffWith(that,unit);
39
+ clone.set(unit,that.get(unit));
40
+ });
41
+ return hash;
42
+ }
43
+ },
44
+ endOf(unit){
45
+ return this.startOf(unit).change(unit,unit=='week'?7:1).change('millsecond',-1);
46
+ },
47
+ startOf(unit){
48
+ let $ = this.toObject();
49
+ let that = null;
50
+ let index = periodKey.indexOf(unit)+1;
51
+ let dateSet = this.toArray();
52
+ let initSet = periodValue.slice(index);
53
+ dateSet.splice(index,initSet.length,...initSet);
54
+ if(unit=='timestamp'){
55
+ that = this.clone();
56
+ }else if(unit=='week'){
57
+ that = datex($.year,$.month,$.day-$.week,0,0,0,0);
58
+ }else{
59
+ that = datex(...dateSet);
60
+ }
61
+ return that;
62
+ }
63
+ });
64
+ };
@@ -0,0 +1,72 @@
1
+ import en_us from './locale/en-us';
2
+ import zh_cn from './locale/zh-cn';
3
+
4
+ export default function(datex,proto){
5
+ let _langMap = {};
6
+ _langMap['en-us'] = en_us;
7
+ _langMap['zh-cn'] = zh_cn;
8
+ let _lang = 'en-us';
9
+ if(typeof self!='undefined'&&self.navigator){
10
+ _lang = self.navigator.language.toLowerCase();
11
+ if (!_langMap[_lang]) {
12
+ _lang = 'en-us';
13
+ }
14
+ }
15
+
16
+ Object.assign(datex,{
17
+ setLanguage(lang,data={}){
18
+ lang = lang.toLowerCase();
19
+ _langMap[lang] = Object.assign(_langMap[lang]||{},data);
20
+ return this;
21
+ },
22
+ switchLanguage(lang){
23
+ _lang = lang.toLowerCase();
24
+ return this;
25
+ },
26
+ getLanguage(){
27
+ return _langMap[_lang];
28
+ }
29
+ });
30
+
31
+ Object.assign(proto,{
32
+ _langMap:{},
33
+ _lang:null,
34
+ setLanguage(lang,data={}){
35
+ lang = lang.toLowerCase();
36
+ this._langMap[lang] = Object.assign(this._langMap[lang]||{},data);
37
+ return this;
38
+ },
39
+ switchLanguage(lang){
40
+ this._lang = lang.toLowerCase();
41
+ return this;
42
+ },
43
+ getLanguage(){
44
+ return this._langMap[this._lang];
45
+ }
46
+ });
47
+
48
+ proto.onInit(function(){
49
+ this._langMap = Object.assign(this._langMap,_langMap);
50
+ this._lang = _lang;
51
+ });
52
+
53
+ // 重写
54
+ let format = proto.format;
55
+ Object.assign(proto,{
56
+ format(pattern = 'YYYY-MM-DD HH:mm:ss'){
57
+ let that = this.clone();
58
+ let $ = that.toObject();
59
+ let language = datex.getLanguage()||this.getLanguage();
60
+ let map = {};
61
+ map['MMM'] = language['MMM'][$.month-1];
62
+ map['MMMM'] = language['MMMM'][$.month-1];
63
+ map['Do'] = language['Do'][$.day-1];
64
+ map['WW'] = language['WW'][$.week];
65
+ map['WWW'] = language['WWW'][$.week];
66
+ for (let key in map) {
67
+ pattern = pattern.replace(key,map[key]||'');
68
+ }
69
+ return format.bind(this)(pattern);
70
+ }
71
+ });
72
+ };
@@ -0,0 +1,7 @@
1
+ export default {
2
+ 'MMM':['Jan.','Feb.','Mar.','Apr.','May.','Jun.','Jul.','Aug.','Sept.','Oct.','Nov.','Dec.'],
3
+ 'MMMM':['January','February','March','April','May','June','July','August','September','October','November','December'],
4
+ 'Do':['1st','2nd','3rd','4th','5th','6th','7th','8th','9th','10th','11th','12th','13th','14th','15th','16th','17th','18th','19th','20th','21st','22nd','23rd','24th','25th','26th','27th','28th','29th','30th','31st'],
5
+ 'WW':['Sun.','Mon.','Tues.','Wed.','Thur.','Fri.','Sat.'],
6
+ 'WWW':['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'],
7
+ };
@@ -0,0 +1,7 @@
1
+ export default {
2
+ 'MMM':['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月'],
3
+ 'MMMM':['一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月'],
4
+ 'Do':['1日','2日','3日','4日','5日','6日','7日','8日','9日','10日','11日','12日','13日','14日','15日','16日','17日','18日','19日','20日','21日','22日','23日','24日','25日','26日','27日','28日','29日','30日','31日'],
5
+ 'WW':['周日','周一','周二','周三','周四','周五','周六'],
6
+ 'WWW':['星期日','星期一','星期二','星期三','星期四','星期五','星期六'],
7
+ };
@@ -0,0 +1,9 @@
1
+ export const periodKey = ['year','month','day','hour','minute','second','millsecond'];
2
+ export const periodValue = [1970,1,1,0,0,0,0];
3
+ export const periodTime = {
4
+ 'day':8.64e7,
5
+ 'hour':3.6e6,
6
+ 'minute':6e4,
7
+ 'second':1000,
8
+ 'millsecond':1
9
+ };
@@ -0,0 +1,60 @@
1
+ export default function(datex,proto){
2
+ let _timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
3
+ let _offset = 0;
4
+
5
+ const convertTimeZone = (date, timeZone) => {return new Date(date.toLocaleString('en-US', { timeZone }))};
6
+
7
+ Object.assign(datex,{
8
+ supportedTimezones:(typeof Intl!='undefined'&&Intl.supportedValuesOf?Intl.supportedValuesOf('timeZone'):[]),
9
+ switchTimezone(timeZone){
10
+ _timezone = timeZone;
11
+ _offset = convertTimeZone(new Date('1970/1/1'),_timezone).getTime() - (new Date('1970/1/1')).getTime();
12
+ return this;
13
+ },
14
+ getTimezoneOffset(){
15
+ return (new Date).getTimezoneOffset() - _offset/60000;
16
+ },
17
+ getTimezone(){
18
+ return _timezone;
19
+ }
20
+ });
21
+
22
+ Object.assign(proto,{
23
+ _timezone:null,
24
+ _offset:0,
25
+ switchTimezone(timezone){
26
+ this._timezone = timezone;
27
+ this._offset = convertTimeZone(new Date('1970/1/1'),this._timezone).getTime() - (new Date('1970/1/1')).getTime();
28
+ return this;
29
+ },
30
+ getTimezoneOffset(){
31
+ return this._date.getTimezoneOffset() - this._offset/60000;
32
+ },
33
+ getTimezone(){
34
+ return this._timezone;
35
+ }
36
+ });
37
+
38
+ proto.onInit(function(...argu){
39
+ this._timezone = _timezone;
40
+ this._offset = _offset;
41
+ if(argu.length){
42
+ if(argu[0] instanceof Date){
43
+ }else if(argu[0] instanceof datex){
44
+ }else if(argu.length==1&&typeof argu[0]=='number'){
45
+ }else if(_offset){
46
+ this._date.setTime(this._date.getTime()-_offset);
47
+ }
48
+ }
49
+ });
50
+
51
+ // 重写
52
+ let format = proto.format;
53
+ Object.assign(proto,{
54
+ format(pattern = 'YYYY-MM-DD HH:mm:ss'){
55
+ let that = this.clone();
56
+ that._date.setTime(that._date.getTime()+that._offset);
57
+ return format.bind(that)(pattern);
58
+ }
59
+ });
60
+ };
@@ -0,0 +1,63 @@
1
+ import {periodKey,periodValue} from './method/map/period';
2
+
3
+ function isObject(value){
4
+ return value != null && (typeof value == 'object' || typeof value == 'function');
5
+ }
6
+
7
+ let taskQueue = [];
8
+
9
+ export default {
10
+ _date:null,
11
+ init:function(...argu){
12
+ if(argu.length){
13
+
14
+ if(Object.getPrototypeOf(argu[0])==Object.getPrototypeOf(this)){
15
+ return argu[0];
16
+ }else if(argu[0] instanceof Date){
17
+ this._date = argu[0];
18
+ }else{
19
+ if(Array.isArray(argu[0])){
20
+ argu = periodValue.map((value,index)=>(argu[0][index]||value));
21
+ }else if(isObject(argu[0])){
22
+ argu = periodValue.map((value,index)=>(argu[0][periodKey[index]]||value));
23
+ }
24
+ if(argu.length==1&&typeof argu[0]=='string'){
25
+ let matchs1 = argu[0].match(/(\d{1,4})[\-\/](\d{1,2})[\-\/](\d{1,2})([\sT](\d{1,2})?:(\d{1,2})?(:(\d{1,2}))?(\.(\d{1,3}))?)?/);
26
+ let matchs2 = argu[0].match(/(\d{1,2})[\-\/](\d{1,2})[\-\/](\d{3,4})([\sT](\d{1,2})?:(\d{1,2})?(:(\d{1,2}))?(\.(\d{1,3}))?)?/);
27
+ let matchs3 = argu[0].match(/^([12]\d{3})(\d{2})(\d{2})(\d{2})?(\d{2})?(\d{2})?(\d{1,3})?/);
28
+ if(matchs1&&!matchs2){
29
+ argu = [1,2,3,5,6,8,10].map(function(i,index){
30
+ return +(matchs1[i]||periodValue[index]);
31
+ });
32
+ }else if(matchs2){
33
+ argu = [3,1,2,5,6,8,10].map(function(i,index){
34
+ return +(matchs2[i]||periodValue[index]);
35
+ });
36
+ }else if(matchs3){
37
+ argu = [1,2,3,4,5,6,7].map(function(i,index){
38
+ return +(matchs3[i]||periodValue[index]);
39
+ });
40
+ }
41
+ }
42
+ if(argu.length>=3){
43
+ argu[1]--;
44
+ }
45
+ this._date = new Date(...argu);
46
+ if(argu.length>=2&&!isNaN(argu[0])&&argu[0]<100){
47
+ this._date.setFullYear(argu[0]);
48
+ }
49
+ }
50
+ }else{
51
+ this._date = new Date();
52
+ }
53
+
54
+ let _ = this;
55
+ taskQueue.forEach(function(task){
56
+ task.bind(_)(...argu);
57
+ });
58
+ return this;
59
+ },
60
+ onInit:function(callback){
61
+ taskQueue.push(callback);
62
+ }
63
+ };
Binary file