@acorex/core 7.0.19 → 7.0.20

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.
Files changed (205) hide show
  1. package/config/ax-preset.js +186 -0
  2. package/config/index.ts +1 -0
  3. package/config/ng-package.json +6 -0
  4. package/config/public-api.ts +1 -0
  5. package/config/src/configs.ts +37 -0
  6. package/dateTime/index.ts +1 -0
  7. package/dateTime/ng-package.json +6 -0
  8. package/dateTime/{public-api.d.ts → public-api.ts} +1 -1
  9. package/dateTime/src/datetime.class.ts +449 -0
  10. package/dateTime/src/datetime.module.ts +35 -0
  11. package/dateTime/src/datetime.pipe.ts +21 -0
  12. package/dateTime/src/georgian.calendar.ts +205 -0
  13. package/dateTime/src/jalali.calendar.ts +416 -0
  14. package/events/index.ts +1 -0
  15. package/events/ng-package.json +6 -0
  16. package/events/public-api.ts +1 -0
  17. package/events/src/event.service.ts +31 -0
  18. package/file/index.ts +1 -0
  19. package/file/ng-package.json +7 -0
  20. package/file/public-api.ts +4 -0
  21. package/file/src/file-download-ref.class.ts +16 -0
  22. package/file/src/file-download-result.class.ts +56 -0
  23. package/file/src/file-upload-ref.class.ts +14 -0
  24. package/file/src/file.service.ts +104 -0
  25. package/http/index.ts +1 -0
  26. package/http/ng-package.json +6 -0
  27. package/http/{public-api.d.ts → public-api.ts} +1 -1
  28. package/http/src/{http-error.class.d.ts → http-error.class.ts} +2 -2
  29. package/http/src/{http-events.interceptor.d.ts → http-events.interceptor.ts} +7 -3
  30. package/http/src/{http-request.class.d.ts → http-request.class.ts} +5 -5
  31. package/http/src/http-result.class.ts +34 -0
  32. package/http/src/http.module.ts +25 -0
  33. package/http/src/http.service.ts +161 -0
  34. package/image/index.ts +1 -0
  35. package/image/ng-package.json +7 -0
  36. package/image/public-api.ts +1 -0
  37. package/image/src/image.service.ts +42 -0
  38. package/index.ts +1 -0
  39. package/karma.conf.js +44 -0
  40. package/ng-package.json +7 -0
  41. package/package.json +16 -115
  42. package/pipes/index.ts +1 -0
  43. package/pipes/ng-package.json +6 -0
  44. package/pipes/public-api.ts +2 -0
  45. package/pipes/src/pipes.module.ts +11 -0
  46. package/pipes/src/safe.pipe.ts +24 -0
  47. package/platform/index.ts +1 -0
  48. package/platform/ng-package.json +6 -0
  49. package/platform/public-api.ts +1 -0
  50. package/platform/src/platform.service.ts +223 -0
  51. package/public-api.ts +4 -0
  52. package/test.ts +27 -0
  53. package/translation/index.ts +1 -0
  54. package/translation/ng-package.json +6 -0
  55. package/translation/{public-api.d.ts → public-api.ts} +1 -1
  56. package/translation/src/translation.module.ts +12 -0
  57. package/translation/src/translator.pipe.ts +9 -0
  58. package/translation/src/translator.ts +49 -0
  59. package/tsconfig.lib.json +15 -0
  60. package/tsconfig.lib.prod.json +10 -0
  61. package/tsconfig.spec.json +17 -0
  62. package/utils/index.ts +1 -0
  63. package/utils/ng-package.json +6 -0
  64. package/utils/{public-api.d.ts → public-api.ts} +1 -1
  65. package/utils/src/color-util.ts +83 -0
  66. package/utils/src/drawing-util.ts +43 -0
  67. package/utils/src/object-util.ts +82 -0
  68. package/utils/src/string-util.ts +25 -0
  69. package/config/index.d.ts +0 -5
  70. package/config/public-api.d.ts +0 -1
  71. package/config/src/configs.d.ts +0 -9
  72. package/dateTime/index.d.ts +0 -5
  73. package/dateTime/src/datetime.class.d.ts +0 -100
  74. package/dateTime/src/datetime.module.d.ts +0 -8
  75. package/dateTime/src/datetime.pipe.d.ts +0 -8
  76. package/dateTime/src/georgian.calendar.d.ts +0 -20
  77. package/dateTime/src/jalali.calendar.d.ts +0 -35
  78. package/esm2020/acorex-core.mjs +0 -5
  79. package/esm2020/config/acorex-core-config.mjs +0 -5
  80. package/esm2020/config/public-api.mjs +0 -2
  81. package/esm2020/config/src/configs.mjs +0 -32
  82. package/esm2020/dateTime/acorex-core-dateTime.mjs +0 -5
  83. package/esm2020/dateTime/public-api.mjs +0 -6
  84. package/esm2020/dateTime/src/datetime.class.mjs +0 -288
  85. package/esm2020/dateTime/src/datetime.module.mjs +0 -39
  86. package/esm2020/dateTime/src/datetime.pipe.mjs +0 -26
  87. package/esm2020/dateTime/src/georgian.calendar.mjs +0 -189
  88. package/esm2020/dateTime/src/jalali.calendar.mjs +0 -359
  89. package/esm2020/events/acorex-core-events.mjs +0 -5
  90. package/esm2020/events/public-api.mjs +0 -2
  91. package/esm2020/events/src/event.service.mjs +0 -36
  92. package/esm2020/file/acorex-core-file.mjs +0 -5
  93. package/esm2020/file/public-api.mjs +0 -5
  94. package/esm2020/file/src/file-download-ref.class.mjs +0 -12
  95. package/esm2020/file/src/file-download-result.class.mjs +0 -47
  96. package/esm2020/file/src/file-upload-ref.class.mjs +0 -12
  97. package/esm2020/file/src/file.service.mjs +0 -101
  98. package/esm2020/http/acorex-core-http.mjs +0 -5
  99. package/esm2020/http/public-api.mjs +0 -6
  100. package/esm2020/http/src/http-error.class.mjs +0 -2
  101. package/esm2020/http/src/http-events.interceptor.mjs +0 -3
  102. package/esm2020/http/src/http-request.class.mjs +0 -2
  103. package/esm2020/http/src/http-result.class.mjs +0 -21
  104. package/esm2020/http/src/http.module.mjs +0 -29
  105. package/esm2020/http/src/http.service.mjs +0 -148
  106. package/esm2020/image/acorex-core-image.mjs +0 -5
  107. package/esm2020/image/public-api.mjs +0 -2
  108. package/esm2020/image/src/image.service.mjs +0 -43
  109. package/esm2020/pipes/acorex-core-pipes.mjs +0 -5
  110. package/esm2020/pipes/public-api.mjs +0 -3
  111. package/esm2020/pipes/src/pipes.module.mjs +0 -19
  112. package/esm2020/pipes/src/safe.pipe.mjs +0 -30
  113. package/esm2020/platform/acorex-core-platform.mjs +0 -5
  114. package/esm2020/platform/public-api.mjs +0 -2
  115. package/esm2020/platform/src/platform.service.mjs +0 -153
  116. package/esm2020/public-api.mjs +0 -5
  117. package/esm2020/translation/acorex-core-translation.mjs +0 -5
  118. package/esm2020/translation/public-api.mjs +0 -4
  119. package/esm2020/translation/src/translation.module.mjs +0 -18
  120. package/esm2020/translation/src/translator.mjs +0 -43
  121. package/esm2020/translation/src/translator.pipe.mjs +0 -15
  122. package/esm2020/utils/acorex-core-utils.mjs +0 -5
  123. package/esm2020/utils/public-api.mjs +0 -5
  124. package/esm2020/utils/src/color-util.mjs +0 -71
  125. package/esm2020/utils/src/drawing-util.mjs +0 -27
  126. package/esm2020/utils/src/object-util.mjs +0 -39
  127. package/esm2020/utils/src/string-util.mjs +0 -19
  128. package/events/index.d.ts +0 -5
  129. package/events/public-api.d.ts +0 -1
  130. package/events/src/event.service.d.ts +0 -9
  131. package/fesm2015/acorex-core-config.mjs +0 -40
  132. package/fesm2015/acorex-core-config.mjs.map +0 -1
  133. package/fesm2015/acorex-core-dateTime.mjs +0 -899
  134. package/fesm2015/acorex-core-dateTime.mjs.map +0 -1
  135. package/fesm2015/acorex-core-events.mjs +0 -44
  136. package/fesm2015/acorex-core-events.mjs.map +0 -1
  137. package/fesm2015/acorex-core-file.mjs +0 -177
  138. package/fesm2015/acorex-core-file.mjs.map +0 -1
  139. package/fesm2015/acorex-core-http.mjs +0 -203
  140. package/fesm2015/acorex-core-http.mjs.map +0 -1
  141. package/fesm2015/acorex-core-image.mjs +0 -54
  142. package/fesm2015/acorex-core-image.mjs.map +0 -1
  143. package/fesm2015/acorex-core-pipes.mjs +0 -54
  144. package/fesm2015/acorex-core-pipes.mjs.map +0 -1
  145. package/fesm2015/acorex-core-platform.mjs +0 -161
  146. package/fesm2015/acorex-core-platform.mjs.map +0 -1
  147. package/fesm2015/acorex-core-translation.mjs +0 -81
  148. package/fesm2015/acorex-core-translation.mjs.map +0 -1
  149. package/fesm2015/acorex-core-utils.mjs +0 -164
  150. package/fesm2015/acorex-core-utils.mjs.map +0 -1
  151. package/fesm2015/acorex-core.mjs +0 -12
  152. package/fesm2015/acorex-core.mjs.map +0 -1
  153. package/fesm2020/acorex-core-config.mjs +0 -40
  154. package/fesm2020/acorex-core-config.mjs.map +0 -1
  155. package/fesm2020/acorex-core-dateTime.mjs +0 -899
  156. package/fesm2020/acorex-core-dateTime.mjs.map +0 -1
  157. package/fesm2020/acorex-core-events.mjs +0 -44
  158. package/fesm2020/acorex-core-events.mjs.map +0 -1
  159. package/fesm2020/acorex-core-file.mjs +0 -177
  160. package/fesm2020/acorex-core-file.mjs.map +0 -1
  161. package/fesm2020/acorex-core-http.mjs +0 -202
  162. package/fesm2020/acorex-core-http.mjs.map +0 -1
  163. package/fesm2020/acorex-core-image.mjs +0 -51
  164. package/fesm2020/acorex-core-image.mjs.map +0 -1
  165. package/fesm2020/acorex-core-pipes.mjs +0 -54
  166. package/fesm2020/acorex-core-pipes.mjs.map +0 -1
  167. package/fesm2020/acorex-core-platform.mjs +0 -161
  168. package/fesm2020/acorex-core-platform.mjs.map +0 -1
  169. package/fesm2020/acorex-core-translation.mjs +0 -80
  170. package/fesm2020/acorex-core-translation.mjs.map +0 -1
  171. package/fesm2020/acorex-core-utils.mjs +0 -164
  172. package/fesm2020/acorex-core-utils.mjs.map +0 -1
  173. package/fesm2020/acorex-core.mjs +0 -12
  174. package/fesm2020/acorex-core.mjs.map +0 -1
  175. package/file/index.d.ts +0 -5
  176. package/file/public-api.d.ts +0 -4
  177. package/file/src/file-download-ref.class.d.ts +0 -6
  178. package/file/src/file-download-result.class.d.ts +0 -8
  179. package/file/src/file-upload-ref.class.d.ts +0 -5
  180. package/file/src/file.service.d.ts +0 -18
  181. package/http/index.d.ts +0 -5
  182. package/http/src/http-result.class.d.ts +0 -11
  183. package/http/src/http.module.d.ts +0 -8
  184. package/http/src/http.service.d.ts +0 -23
  185. package/image/index.d.ts +0 -5
  186. package/image/public-api.d.ts +0 -1
  187. package/image/src/image.service.d.ts +0 -12
  188. package/index.d.ts +0 -5
  189. package/pipes/index.d.ts +0 -5
  190. package/pipes/public-api.d.ts +0 -2
  191. package/pipes/src/pipes.module.d.ts +0 -8
  192. package/pipes/src/safe.pipe.d.ts +0 -10
  193. package/platform/index.d.ts +0 -5
  194. package/platform/public-api.d.ts +0 -1
  195. package/platform/src/platform.service.d.ts +0 -26
  196. package/public-api.d.ts +0 -1
  197. package/translation/index.d.ts +0 -5
  198. package/translation/src/translation.module.d.ts +0 -7
  199. package/translation/src/translator.d.ts +0 -11
  200. package/translation/src/translator.pipe.d.ts +0 -7
  201. package/utils/index.d.ts +0 -5
  202. package/utils/src/color-util.d.ts +0 -20
  203. package/utils/src/drawing-util.d.ts +0 -17
  204. package/utils/src/object-util.d.ts +0 -4
  205. package/utils/src/string-util.d.ts +0 -6
@@ -0,0 +1,449 @@
1
+ import * as _ from 'lodash';
2
+ import { AXConfig } from '@acorex/core/config';
3
+
4
+ export type TimeUnit =
5
+ | 'ms'
6
+ | 'second'
7
+ | 'minute'
8
+ | 'hour'
9
+ | 'day'
10
+ | 'month'
11
+ | 'year'
12
+ | 'week';
13
+
14
+
15
+
16
+
17
+
18
+ export interface AXDateValue {
19
+ year: number,
20
+ month: number,
21
+ date: number,
22
+ hours?: number,
23
+ minutes?: number,
24
+ seconds?: number,
25
+ ms?: number
26
+ calendar: string;
27
+ }
28
+
29
+
30
+ export interface AXCalendar {
31
+ monthNames: string[];
32
+
33
+ monthShortNames: string[];
34
+
35
+ dayNames: string[];
36
+
37
+ dayShortNames: string[];
38
+
39
+ name(): string;
40
+
41
+ dayOfMonth(date: Date): number;
42
+
43
+ dayOfYear(date: Date): number;
44
+
45
+ dayOfWeek(date: Date): number;
46
+
47
+ monthOfYear(date: Date): number;
48
+
49
+ weekOfYear(date: Date): number;
50
+
51
+ year(date: Date): number;
52
+
53
+ add(date: Date, unit: TimeUnit, amount: number): AXDateTime;
54
+
55
+ set(date: Date, unit: TimeUnit, value: number): AXDateTime;
56
+
57
+ startOf(date: Date, unit: TimeUnit): AXDateTime;
58
+
59
+ endOf(date: Date, unit: TimeUnit): AXDateTime;
60
+
61
+ create(value: AXDateValue): AXDateTime;
62
+
63
+ }
64
+
65
+
66
+
67
+
68
+ // @dynamic
69
+ export class AXDateTime {
70
+ static convert(
71
+ value: Date | AXDateTime | string,
72
+ calendar: string = AXConfig.get('dateTime.calendar')
73
+ ): AXDateTime {
74
+ let date: AXDateTime;
75
+ if (typeof value === 'string' || value instanceof String) {
76
+ date = new AXDateTime(value as string, calendar);
77
+ } else if (value instanceof Date) {
78
+ date = new AXDateTime(value as Date, calendar);
79
+ } else if (value instanceof AXDateTime) {
80
+ date = new AXDateTime(value.date, calendar);
81
+ }
82
+ return date!;
83
+ }
84
+
85
+ static from(value: AXDateValue = {
86
+ calendar: AXConfig.get('dateTime.calendar'),
87
+ year: 1,
88
+ month: 1,
89
+ date: 1,
90
+ hours: 0,
91
+ minutes: 0,
92
+ seconds: 0,
93
+ ms: 0
94
+ }): AXDateTime {
95
+ return AXDateTime.resolveCalendar(value.calendar).create(value);
96
+ }
97
+
98
+
99
+ static resolveCalendar(calendar: string | AXCalendar): AXCalendar {
100
+ return typeof calendar == 'string'
101
+ ? AXConfig.get(`dateTime.calendars.${calendar}.class`)
102
+ : calendar;
103
+ }
104
+
105
+
106
+ private _date: Date;
107
+
108
+ get date(): Date {
109
+ return this._date;
110
+ }
111
+
112
+ private _calendar: AXCalendar;
113
+ get calendar(): AXCalendar {
114
+ return this._calendar;
115
+ }
116
+
117
+
118
+
119
+ constructor(
120
+ value: Date | string = new Date(),
121
+ calendar: string | AXCalendar = AXConfig.get(`dateTime.calendar`)
122
+ ) {
123
+ this._calendar = AXDateTime.resolveCalendar(calendar);
124
+ if (value instanceof Date) {
125
+ this._date = value as Date;
126
+ } else {
127
+ //TODO: parse jalali
128
+ this._date = new Date(value);
129
+ }
130
+ }
131
+
132
+ clone(): AXDateTime {
133
+ return new AXDateTime(this.date, this.calendar);
134
+ }
135
+
136
+ get dayOfMonth(): number {
137
+ return this._calendar.dayOfMonth(this.date);
138
+ }
139
+
140
+ get dayOfYear(): number {
141
+ return this._calendar.dayOfYear(this.date);
142
+ }
143
+
144
+ get dayOfWeek(): number {
145
+ return this._calendar.dayOfWeek(this.date);
146
+ }
147
+
148
+ get hour(): number {
149
+ return this._date.getHours();
150
+ }
151
+
152
+ get minute(): number {
153
+ return this._date.getMinutes();
154
+ }
155
+
156
+ get second(): number {
157
+ return this._date.getSeconds();
158
+ }
159
+
160
+ get year(): number {
161
+ return this._calendar.year(this.date);
162
+ }
163
+
164
+ get monthOfYear(): number {
165
+ return this._calendar.monthOfYear(this.date);
166
+ }
167
+
168
+ get weekOfYear(): number {
169
+ return this._calendar.weekOfYear(this.date);
170
+ }
171
+
172
+ get month(): AXCalendarMonth {
173
+ return new AXCalendarMonth(this);
174
+ }
175
+
176
+ add(unit: TimeUnit, amount: number): AXDateTime {
177
+ return this._calendar.add(this.date, unit, amount);
178
+ }
179
+
180
+ set(unit: TimeUnit = 'day', value: number): AXDateTime {
181
+ return this._calendar.set(this.date, unit, value);
182
+ }
183
+
184
+ duration(
185
+ end: Date | AXDateTime,
186
+ unit: TimeUnit = 'day'
187
+ ): AXTimeSpan {
188
+ const range = new AXDateTimeRange(
189
+ this,
190
+ AXDateTime.convert(end, this.calendar.name())
191
+ );
192
+ return range.duration();
193
+ }
194
+
195
+ startOf(unit: TimeUnit = 'day'): AXDateTime {
196
+ return this._calendar.startOf(this.date, unit);
197
+ }
198
+
199
+ endOf(unit: TimeUnit = 'day'): AXDateTime {
200
+ return this._calendar.endOf(this.date, unit);
201
+ }
202
+
203
+ private _formatKeys = {
204
+ ss: () => this.pad(this.date.getSeconds(), 2),
205
+ s: () => this.date.getSeconds().toString(),
206
+ //
207
+ dd: () => this.pad(this.calendar.dayOfMonth(this.date), 2),
208
+ d: () => this.calendar.dayOfMonth(this.date).toString(),
209
+ //
210
+ mm: () => this.pad(this.date.getMinutes(), 2),
211
+ m: () => this.date.getMinutes().toString(),
212
+ //
213
+ MMMM: () => this.calendar.monthNames[this.calendar.monthOfYear(this.date) - 1],
214
+ MMM: () => this.calendar.monthShortNames[this.calendar.monthOfYear(this.date) - 1],
215
+ MM: () => this.pad(this.calendar.monthOfYear(this.date), 2),
216
+ //
217
+ DDDD: () => this.calendar.dayNames[this.calendar.dayOfWeek(this.date) - 1],
218
+ DDD: () => this.calendar.dayShortNames[this.calendar.dayOfWeek(this.date) - 1],
219
+ //
220
+ yyyy: () => this.calendar.year(this.date).toString(),
221
+ YYYY: () => this.calendar.year(this.date).toString(),
222
+ yy: () => this.calendar.year(this.date).toString().substring(2),
223
+ YY: () => this.calendar.year(this.date).toString().substring(2),
224
+ //
225
+ HH: () => this.pad(this.date.getHours(), 2),
226
+ H: () => this.date.getHours().toString(),
227
+ //
228
+ hh: () => this.pad((this.date.getHours() % 12 || 12), 2),
229
+ h: () => (this.date.getHours() % 12 || 12).toString(),
230
+ //
231
+ A: () => (this.date.getHours() < 12) ? 'am' : 'pm',
232
+ a: () => (this.date.getHours() < 12) ? 'am' : 'pm',
233
+ };
234
+
235
+ format(
236
+ format: string = AXConfig.get('dateTime.shortDateFormat')
237
+ ): string {
238
+ const re = new RegExp(_.orderBy(Object.keys(this._formatKeys), c => c.length, ['desc']).join("|"), "gi");
239
+ return format.replace(re, (matched) => {
240
+ return this._formatKeys[matched]();
241
+ });
242
+ }
243
+
244
+ private pad(n, width, z = '0') {
245
+ n = n + '';
246
+ return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
247
+ }
248
+
249
+ toString(format: string = AXConfig.get('dateTime.shortDateFormat')): string {
250
+ return this.format(format);
251
+ }
252
+
253
+ equal(value: Date | AXDateTime, unit: TimeUnit = 'day') {
254
+ return this.compare(value, unit) == 0;
255
+ }
256
+
257
+ compare(value: Date | AXDateTime, unit: TimeUnit = 'day') {
258
+ const val = AXDateTime.convert(value, this.calendar.name());
259
+ const func = (v1: number, v2: number) => {
260
+ if (v1 == v2) {
261
+ return 0;
262
+ } else if (v1 > v2) {
263
+ return 1;
264
+ } else {
265
+ return -1;
266
+ }
267
+ }
268
+ let p = 0;
269
+ switch (unit) {
270
+ case 'year':
271
+ return func(this.year, val.year);
272
+ case 'week':
273
+ p = this.compare(val, 'year');
274
+ return p == 0 ? func(this.weekOfYear, val.weekOfYear) : p;
275
+ case 'month':
276
+ p = this.compare(val, 'year');
277
+ return p == 0 ? func(this.monthOfYear, val.monthOfYear) : p;
278
+ case 'day':
279
+ p = this.compare(val, 'year');
280
+ return p == 0 ? func(this.dayOfYear, val.dayOfYear) : p;
281
+ case 'hour':
282
+ p = this.compare(val, 'day');
283
+ return p == 0 ? func(this.hour, val.hour) : p;
284
+ case 'minute':
285
+ p = this.compare(val, 'hour');
286
+ return p == 0 ? func(this.minute, val.minute) : p;
287
+ case 'second':
288
+ p = this.compare(val, 'minute');
289
+ return p == 0 ? func(this.second, val.second) : p;
290
+ default:
291
+ return func(this.date.getTime(), val.date.getTime());
292
+ }
293
+
294
+ }
295
+
296
+ convert(calendar: string) {
297
+ return AXDateTime.convert(this, calendar);
298
+ }
299
+ }
300
+
301
+ export class AXCalendarMonth {
302
+ private _range: AXDateTimeRange;
303
+ public get range(): AXDateTimeRange {
304
+ return this._range;
305
+ }
306
+ public set range(v: AXDateTimeRange) {
307
+ this._range = v;
308
+ }
309
+
310
+ constructor(date: AXDateTime) {
311
+ this.index = date.date.getMonth();
312
+ this.name = date.format('MMMM');
313
+ this.range = new AXDateTimeRange(
314
+ new AXDateTime(date.startOf('month').date, date.calendar),
315
+ new AXDateTime(date.endOf('month').date, date.calendar)
316
+ );
317
+ }
318
+
319
+ private readonly index: number;
320
+ private readonly name: string;
321
+ }
322
+
323
+ export interface AXTimeSpanTotal {
324
+ miliseconds: number;
325
+ seconds: number;
326
+ minutes: number;
327
+ hours: number;
328
+ days: number;
329
+ weeks: number;
330
+ months: number;
331
+ years: number;
332
+ }
333
+
334
+ export interface AXTimeSpan {
335
+ miliseconds: number;
336
+ seconds: number;
337
+ minutes: number;
338
+ hours: number;
339
+ days: number;
340
+ months: number;
341
+ years: number;
342
+ total: AXTimeSpanTotal;
343
+ }
344
+
345
+ export class AXDateTimeRange {
346
+ private _startTime: AXDateTime;
347
+ private _endTime: AXDateTime;
348
+
349
+ public get startTime(): AXDateTime {
350
+ return this._startTime;
351
+ }
352
+
353
+ public get endTime(): AXDateTime {
354
+ return this._endTime;
355
+ }
356
+
357
+ constructor(startTime: AXDateTime, endTime: AXDateTime) {
358
+ this._startTime = startTime;
359
+ this._endTime = endTime;
360
+ }
361
+
362
+ duration(): AXTimeSpan {
363
+ const result: AXTimeSpan = {
364
+ miliseconds: 0,
365
+ seconds: 0,
366
+ minutes: 0,
367
+ hours: 0,
368
+ days: 0,
369
+ months: 0,
370
+ years: 0,
371
+ total: {
372
+ miliseconds: 0,
373
+ seconds: 0,
374
+ minutes: 0,
375
+ hours: 0,
376
+ days: 0,
377
+ weeks: 0,
378
+ months: 0,
379
+ years: 0,
380
+ },
381
+ };
382
+
383
+ const one_second = 1000;
384
+ const one_min = one_second * 60;
385
+ const one_hour = one_min * 60;
386
+ const one_day = one_hour * 24;
387
+ const one_week = one_day * 7;
388
+ const one_year = 365.25 * one_day;
389
+ const one_month = one_year / 12;
390
+ const startTime = this._startTime.date.getTime();
391
+ const endTime = this._endTime.date.getTime();
392
+ const diff = Math.abs(endTime - startTime);
393
+ //
394
+ result.total.miliseconds = diff;
395
+ result.total.seconds = Number((diff / one_second).toFixed(2));
396
+ result.total.minutes = Number((diff / one_min).toFixed(2));
397
+ result.total.hours = Number((diff / one_hour).toFixed(2));
398
+ result.total.days = Number((diff / one_day).toFixed(2));
399
+ result.total.weeks = Number((diff / one_week).toFixed(2));
400
+ //
401
+ // let months = (this.endTime.year - this.startTime.year) * 12;
402
+ // months += this.endTime.monthOfYear - this.startTime.monthOfYear + 1;
403
+ // if (this.endTime.dayOfYear < this.startTime.dayOfYear) {
404
+ // months--;
405
+ // }
406
+ // result.total.months = Math.abs(months);
407
+ // TODO: review
408
+ result.total.months = Number((diff / one_month).toFixed(2));
409
+ result.total.years = Number((diff / one_year).toFixed(2));
410
+ //
411
+ result.miliseconds = result.total.miliseconds % 1000;
412
+ result.seconds = Number((result.total.seconds % 60).toFixed(0));
413
+ result.minutes = Number((result.total.minutes % 60).toFixed(0));
414
+ result.hours = Number((result.total.hours % 24).toFixed(0));
415
+ // TODO: review
416
+ result.days = Number((result.total.days % 30.4).toFixed(0));
417
+ result.months = Number((result.total.months % 12).toFixed(0));
418
+ result.years = Number(result.total.years.toFixed(0));
419
+ return result;
420
+ }
421
+
422
+ enumurate(unit: TimeUnit = 'day', amount: number = 1): AXDateTime[] {
423
+ const result: AXDateTime[] = [];
424
+ let item = this._startTime.clone();
425
+ while (item.compare(this._endTime, unit) < 1) {
426
+ if (!result.some(c => c.equal(item, unit))) {
427
+ result.push(item);
428
+ }
429
+ item = item.add(unit, amount)
430
+ }
431
+ return result;
432
+ }
433
+
434
+ includes(value: AXDateTime, unit: TimeUnit = 'day'): boolean {
435
+ // TODO: ??
436
+ return true;
437
+ }
438
+
439
+ // getViewCompaire(view) {
440
+ // switch (view) {
441
+ // case 'day':
442
+ // return 'YMD';
443
+ // case 'month':
444
+ // return 'YM';
445
+ // case 'year':
446
+ // return 'Y';
447
+ // }
448
+ // }
449
+ }
@@ -0,0 +1,35 @@
1
+ import { NgModule } from '@angular/core';
2
+ import { AXConfig } from '@acorex/core/config';
3
+ import { AXDateTimePipe } from './datetime.pipe';
4
+ import { GeorgianCalendar } from './georgian.calendar';
5
+ import { JalaliCalendar } from './jalali.calendar';
6
+
7
+
8
+ @NgModule({
9
+ imports: [],
10
+ exports: [AXDateTimePipe],
11
+ declarations: [AXDateTimePipe],
12
+ providers: [],
13
+ })
14
+ export class AXDateTimeModule {
15
+
16
+ constructor() {
17
+ AXConfig.set({
18
+ dateTime: {
19
+ calendars: {
20
+ jalali: {
21
+ "class": new JalaliCalendar(),
22
+ weekend: [6, 7]
23
+ },
24
+ gregorian: {
25
+ "class": new GeorgianCalendar(),
26
+ weekend: [7, 1]
27
+ }
28
+ },
29
+ calendar: 'gregorian',
30
+ shortDateFormat: 'DDDD, d MMM yyyy'
31
+ }
32
+ });
33
+
34
+ }
35
+ }
@@ -0,0 +1,21 @@
1
+ import { PipeTransform, Pipe } from '@angular/core';
2
+ import { AXConfig } from '@acorex/core/config';
3
+ import { AXDateTime } from './datetime.class';
4
+
5
+ @Pipe({ name: 'axDate' })
6
+ export class AXDateTimePipe implements PipeTransform {
7
+ constructor() { }
8
+
9
+ transform(value: any, format?: string, calendar?: string): string {
10
+ if (value == null) {
11
+ return '';
12
+ }
13
+ const date: AXDateTime = value instanceof AXDateTime ? value.clone() : AXDateTime.convert(value, calendar);
14
+ if (!format) {
15
+ return date.format(AXConfig.get('dateTime.shortDateFormat'));
16
+ }
17
+ else {
18
+ return date.format(format);
19
+ }
20
+ }
21
+ }
@@ -0,0 +1,205 @@
1
+ import { AXCalendar, AXDateTime, AXDateValue, TimeUnit } from "./datetime.class";
2
+
3
+ export class GeorgianCalendar implements AXCalendar {
4
+
5
+ monthNames = [
6
+ "January", "February", "March",
7
+ "April", "May", "June", "July",
8
+ "August", "September", "October",
9
+ "November", "December"
10
+ ];
11
+
12
+ monthShortNames = [
13
+ "Jan", "Feb", "Mar",
14
+ "Apr", "May", "Jun", "Jul",
15
+ "Aug", "Sep", "Oct",
16
+ "Nov", "Dec"
17
+ ];
18
+
19
+ dayNames = [
20
+ "Sunday", "Monday", "Tuesday", "Wednesday",
21
+ "Thursday", "Friday", "Saturday"
22
+ ];
23
+
24
+ dayShortNames = [
25
+ "Sun", "Mon", "Tue", "Wed",
26
+ "Thu", "Fri", "Sat"
27
+ ];
28
+
29
+
30
+ create(value: AXDateValue): AXDateTime {
31
+ return new AXDateTime(new Date(value.year, value.month - 1, value.date, value.hours || 0, value.minutes || 0, value.seconds || 0, value.ms || 0), this.name());
32
+ }
33
+
34
+ name(): string {
35
+ return 'gregorian'
36
+ }
37
+
38
+ dayOfMonth(date: Date): number {
39
+ return date.getDate();
40
+ }
41
+
42
+ dayOfYear(date: Date): number {
43
+ let result = 0;
44
+ let m = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
45
+ if (this.isLeap(date)) {
46
+ m[1] = 29;
47
+ }
48
+ for (let i = 0; i < date.getMonth(); i++) {
49
+ result = result + m[i];
50
+ }
51
+ result += date.getDate();
52
+ return result;
53
+ }
54
+
55
+ dayOfWeek(date: Date): number {
56
+ return date.getDay() + 1;
57
+ }
58
+
59
+ weekOfYear(date: Date): number {
60
+ const firstDay = new AXDateTime(date).startOf('year');
61
+ return Math.ceil((((date.getTime() - firstDay.date.getTime()) / 86400000) + firstDay.date.getDay() + 1) / 7);
62
+ }
63
+
64
+ year(date: Date): number {
65
+ return date.getFullYear();
66
+ }
67
+
68
+ monthOfYear(date: Date): number {
69
+ return date.getMonth() + 1;
70
+ }
71
+
72
+ add(date: Date, unit: TimeUnit, amount: number): AXDateTime {
73
+ let value = date.valueOf();
74
+ switch (unit) {
75
+ case 'second':
76
+ value += 1000 * amount;
77
+ break
78
+ case 'minute':
79
+ value += 60000 * amount;
80
+ break
81
+ case 'hour':
82
+ value += 3600000 * amount;
83
+ break
84
+ case 'month':
85
+ const v = new Date(value);
86
+ var mo = date.getMonth();
87
+ var yr = date.getFullYear();
88
+ mo = (mo + amount) % 12;
89
+ if (0 > mo) {
90
+ yr += (date.getMonth() + amount - mo - 12) / 12;
91
+ mo += 12;
92
+ }
93
+ else
94
+ yr += ((date.getMonth() + amount - mo) / 12);
95
+ v.setMonth(mo);
96
+ v.setFullYear(yr);
97
+ value = v.valueOf();
98
+ break
99
+ case 'week':
100
+ value += 7 * 86400000 * amount;
101
+ break
102
+ case 'year':
103
+ const yv = new Date(value);
104
+ yv.setFullYear(yv.getFullYear() + amount);
105
+ value = yv.valueOf();
106
+ break
107
+ case 'day':
108
+ default:
109
+ value += 86400000 * amount;
110
+ }
111
+ return new AXDateTime(new Date(value), this.name());
112
+ }
113
+
114
+ set(date: Date, unit: TimeUnit, value: number): AXDateTime {
115
+ const clone = new Date(date.valueOf());
116
+ switch (unit) {
117
+ case 'second':
118
+ clone.setHours(clone.getHours(), clone.getMinutes(), value, clone.getMilliseconds());
119
+ break;
120
+ case 'minute':
121
+ clone.setHours(clone.getHours(), value, clone.getSeconds(), clone.getMilliseconds());
122
+ break;
123
+ case 'hour':
124
+ clone.setHours(value, clone.getMinutes(), clone.getSeconds(), clone.getMilliseconds());
125
+ break;
126
+ default:
127
+ case 'day':
128
+ clone.setDate(value);
129
+ break;
130
+ case "week":
131
+ break;
132
+ case "month":
133
+ clone.setMonth(Math.max(0, value - 1));
134
+ break;
135
+ case "year":
136
+ clone.setFullYear(value);
137
+ break;
138
+ }
139
+ return new AXDateTime(clone, this.name());
140
+ }
141
+
142
+ startOf(date: Date, unit: TimeUnit): AXDateTime {
143
+ const clone = new Date(date.valueOf());
144
+ switch (unit) {
145
+ case 'second':
146
+ clone.setHours(clone.getHours(), clone.getMinutes(), clone.getSeconds(), 0);
147
+ return new AXDateTime(clone, this.name());
148
+ case 'minute':
149
+ clone.setHours(clone.getHours(), clone.getMinutes(), 0, 0);
150
+ return new AXDateTime(clone, this.name());
151
+ case 'hour':
152
+ clone.setHours(clone.getHours(), 0, 0, 0);
153
+ return new AXDateTime(clone, this.name());
154
+ default:
155
+ case 'day':
156
+ clone.setHours(0, 0, 0, 0);
157
+ return new AXDateTime(clone, this.name());
158
+ case "week":
159
+ const index = 0;
160
+ const start = index >= 0 ? index : 0;
161
+ const day = clone.getDay();
162
+ const diff = clone.getDate() - day + (start > day ? start - 7 : start);
163
+ clone.setDate(diff);
164
+ return new AXDateTime(clone, this.name()).startOf('day');
165
+ case "month":
166
+ clone.setDate(1)
167
+ return new AXDateTime(clone, this.name()).startOf('day');
168
+ case "year":
169
+ clone.setMonth(0);
170
+ clone.setDate(1);
171
+ return new AXDateTime(clone, this.name()).startOf('day');
172
+ }
173
+ }
174
+
175
+ endOf(date: Date, unit: TimeUnit): AXDateTime {
176
+ const clone = new Date(date.valueOf());
177
+ switch (unit) {
178
+ case 'second':
179
+ clone.setHours(clone.getHours(), clone.getMinutes(), clone.getSeconds(), 999);
180
+ return new AXDateTime(clone, this.name());
181
+ case 'minute':
182
+ clone.setHours(clone.getHours(), clone.getMinutes(), 59, 999);
183
+ return new AXDateTime(clone, this.name());
184
+ case 'hour':
185
+ clone.setHours(clone.getHours(), 59, 59, 999);
186
+ return new AXDateTime(clone, this.name());
187
+ default:
188
+ case 'day':
189
+ clone.setHours(23, 59, 59, 999);
190
+ return new AXDateTime(clone, this.name());
191
+ case 'week':
192
+ return this.startOf(date, 'week').add('day', 6).endOf('day');
193
+ case 'month':
194
+ return new AXDateTime(new Date(date.getFullYear(), date.getMonth() + 1, 0), this.name()).endOf('day');
195
+ case "year":
196
+ clone.setMonth(11);
197
+ return new AXDateTime(clone, this.name()).endOf('month');
198
+ }
199
+ }
200
+
201
+ isLeap(date: Date) {
202
+ let leapYear = new Date(date.getFullYear(), 1, 29);
203
+ return leapYear.getDate() == 29;
204
+ }
205
+ }