mybase 1.1.39 → 1.1.40

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mybase",
3
- "version": "1.1.39",
3
+ "version": "1.1.40",
4
4
  "description": "",
5
5
  "main": "mybase.js",
6
6
  "scripts": {
@@ -24,7 +24,16 @@ export declare class Unixtime {
24
24
  addYears(years?: number): Unixtime;
25
25
  yyyymmdd(): string;
26
26
  yyymmddhh(): string;
27
+ /**
28
+ @deprecated use 'day' instead
29
+ */
27
30
  getDay(): number;
31
+ get day(): number;
32
+ get month(): number;
33
+ get year(): number;
34
+ get hours(): number;
35
+ get minutes(): number;
36
+ get seconds(): number;
28
37
  static from(value: Date | string | number): Unixtime;
29
38
  toShortUnixtime(): number;
30
39
  toLongUnixtime(): number;
@@ -33,5 +42,6 @@ export declare class Unixtime {
33
42
  elapsedSeconds(): number;
34
43
  elapsedMinutes(): number;
35
44
  clone(): Unixtime;
45
+ static fromYYYYMMDD(value: string): Unixtime;
36
46
  static try(value: Date | string | number): Unixtime | null;
37
47
  }
@@ -80,34 +80,41 @@ class Unixtime {
80
80
  return this;
81
81
  }
82
82
  addMonths(months = 1) {
83
- this.value.setMonth(this.value.getMonth() + months);
83
+ this.value.setUTCMonth(this.value.getMonth() + months);
84
84
  return this;
85
85
  }
86
86
  addYears(years = 1) {
87
- this.value.setFullYear(this.value.getFullYear() + years);
87
+ this.value.setUTCFullYear(this.year + years);
88
88
  return this;
89
89
  }
90
90
  yyyymmdd() {
91
- let mm = this.value.getMonth() + 1;
92
- let dd = this.value.getDate();
91
+ let mm = this.month;
92
+ let dd = this.day;
93
93
  return [this.value.getFullYear(),
94
94
  (mm > 9 ? '' : '0') + mm,
95
95
  (dd > 9 ? '' : '0') + dd
96
96
  ].join('');
97
97
  }
98
98
  yyymmddhh() {
99
- let mm = this.value.getMonth() + 1;
100
- let dd = this.value.getDate();
101
- let hh = this.value.getHours();
102
- return [this.value.getFullYear(),
99
+ let mm = this.month;
100
+ let dd = this.day;
101
+ let hh = this.hours;
102
+ return [this.year,
103
103
  (mm > 9 ? '' : '0') + mm,
104
104
  (dd > 9 ? '' : '0') + dd,
105
105
  (hh > 9 ? '' : '0') + hh
106
106
  ].join('');
107
107
  }
108
- getDay() {
109
- return this.value.getDay();
110
- }
108
+ /**
109
+ @deprecated use 'day' instead
110
+ */
111
+ getDay() { return this.value.getUTCDay(); }
112
+ get day() { return this.value.getUTCDate(); }
113
+ get month() { return this.value.getUTCMonth() + 1; }
114
+ get year() { return this.value.getUTCFullYear(); }
115
+ get hours() { return this.value.getUTCHours(); }
116
+ get minutes() { return this.value.getUTCMinutes(); }
117
+ get seconds() { return this.value.getUTCSeconds(); }
111
118
  static from(value) {
112
119
  return new Unixtime(value);
113
120
  }
@@ -132,6 +139,20 @@ class Unixtime {
132
139
  clone() {
133
140
  return new Unixtime(this.toLongUnixtime());
134
141
  }
142
+ static fromYYYYMMDD(value) {
143
+ if (value.length != 8)
144
+ throw new Error('Invalid yyyymmdd value');
145
+ let year = parseInt(value.substring(0, 4));
146
+ let month = parseInt(value.substring(4, 6)) - 1;
147
+ let day = parseInt(value.substring(6, 8));
148
+ if (isNaN(year) || isNaN(month) || isNaN(day))
149
+ throw new Error('Invalid yyyymmdd value');
150
+ if (month < 0 || month > 11)
151
+ throw new Error('Invalid month');
152
+ if (day < 1 || day > 31)
153
+ throw new Error('Invalid day');
154
+ return new Unixtime(new Date(Date.UTC(year, month, day)));
155
+ }
135
156
  static try(value) {
136
157
  try {
137
158
  return new Unixtime(value);
@@ -106,4 +106,28 @@ describe('Unixtime', () => {
106
106
  expect(unixtime1).not.toBe(unixtime0);
107
107
  expect(unixtime0.toDate()).toEqual(unixtime1.toDate());
108
108
  });
109
+
110
+
111
+ it('day,month,year should return correct values', () => {
112
+ const unixtime = new Unixtime("2024-10-11T12:13:14Z");
113
+ expect(unixtime.day).toEqual(11);
114
+ expect(unixtime.month).toEqual(10);
115
+ expect(unixtime.year).toEqual(2024);
116
+ expect(unixtime.minutes).toEqual(13);
117
+ expect(unixtime.seconds).toEqual(14);
118
+ expect(unixtime.hours).toEqual(12);
119
+ })
120
+
121
+ it('fromYYYYMMDD should create Unixtime object from string', () => {
122
+ const unixtime = Unixtime.fromYYYYMMDD('20241011');
123
+ expect(unixtime.toISOString()).toEqual('2024-10-11T00:00:00.000Z');
124
+ })
125
+
126
+ it('fromYYYYMMDD should throw error when invalid date', () => {
127
+ expect(() => Unixtime.fromYYYYMMDD('20243131')).toThrow('Invalid month');
128
+ expect(() => Unixtime.fromYYYYMMDD('20241233')).toThrow('Invalid day');
129
+ expect(() => Unixtime.fromYYYYMMDD('xx')).toThrow('Invalid yyyymmdd value');
130
+ //@ts-ignore
131
+ expect(() => Unixtime.fromYYYYMMDD([])).toThrow('Invalid yyyymmdd value');
132
+ })
109
133
  });
@@ -98,18 +98,18 @@ export class Unixtime {
98
98
  }
99
99
 
100
100
  public addMonths(months: number = 1): Unixtime {
101
- this.value.setMonth(this.value.getMonth() + months)
101
+ this.value.setUTCMonth(this.value.getMonth() + months)
102
102
  return this
103
103
  }
104
104
 
105
105
  public addYears(years: number = 1): Unixtime {
106
- this.value.setFullYear(this.value.getFullYear() + years)
106
+ this.value.setUTCFullYear(this.year + years)
107
107
  return this
108
108
  }
109
109
 
110
110
  public yyyymmdd(): string {
111
- let mm = this.value.getMonth() + 1
112
- let dd = this.value.getDate()
111
+ let mm = this.month
112
+ let dd = this.day
113
113
  return [this.value.getFullYear(),
114
114
  (mm > 9 ? '' : '0') + mm,
115
115
  (dd > 9 ? '' : '0') + dd
@@ -117,20 +117,27 @@ export class Unixtime {
117
117
  }
118
118
 
119
119
  public yyymmddhh(): string {
120
- let mm = this.value.getMonth() + 1
121
- let dd = this.value.getDate()
122
- let hh = this.value.getHours()
123
- return [this.value.getFullYear(),
120
+ let mm = this.month
121
+ let dd = this.day
122
+ let hh = this.hours
123
+ return [this.year,
124
124
  (mm > 9 ? '' : '0') + mm,
125
125
  (dd > 9 ? '' : '0') + dd,
126
126
  (hh > 9 ? '' : '0') + hh
127
127
  ].join('')
128
128
  }
129
129
 
130
+ /**
131
+ @deprecated use 'day' instead
132
+ */
133
+ public getDay() : number {return this.value.getUTCDay()}
130
134
 
131
- public getDay() : number {
132
- return this.value.getDay()
133
- }
135
+ get day(): number { return this.value.getUTCDate() }
136
+ get month(): number { return this.value.getUTCMonth() + 1 }
137
+ get year(): number { return this.value.getUTCFullYear() }
138
+ get hours(): number { return this.value.getUTCHours() }
139
+ get minutes(): number { return this.value.getUTCMinutes() }
140
+ get seconds(): number { return this.value.getUTCSeconds() }
134
141
 
135
142
  static from(value: Date | string | number) {
136
143
  return new Unixtime(value)
@@ -165,6 +172,17 @@ export class Unixtime {
165
172
  }
166
173
 
167
174
 
175
+ static fromYYYYMMDD(value: string): Unixtime {
176
+ if (value.length != 8) throw new Error('Invalid yyyymmdd value')
177
+ let year = parseInt(value.substring(0, 4))
178
+ let month = parseInt(value.substring(4, 6)) - 1
179
+ let day = parseInt(value.substring(6, 8))
180
+ if (isNaN(year) || isNaN(month) || isNaN(day)) throw new Error('Invalid yyyymmdd value')
181
+ if (month < 0 || month > 11) throw new Error('Invalid month')
182
+ if (day < 1 || day > 31) throw new Error('Invalid day')
183
+ return new Unixtime(new Date(Date.UTC(year, month, day)))
184
+ }
185
+
168
186
  static try(value: Date | string | number): Unixtime | null {
169
187
  try {
170
188
  return new Unixtime(value)