disacomp-dateservice 1.0.0

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,23 @@
1
+ export declare function bothDatesFormated(dateToFormat?: string): {
2
+ signDate: string;
3
+ normalDate: string;
4
+ };
5
+ export declare function dateClarionToSQL(clarionDate: number): string;
6
+ interface IDateManipulator {
7
+ addDays(date: string, daysToAdd: number): string;
8
+ getCurrentDate(): string;
9
+ getDateFromDateSrting(date: string): Date;
10
+ calculateDaysBetweenDates(date1: string, date2: string): number;
11
+ getPeriodFromDate(date: string): string;
12
+ }
13
+ declare class DateService {
14
+ private dateManipulator;
15
+ constructor(dateManipulator: IDateManipulator);
16
+ addDaysToDate(date: string, days: number): string;
17
+ calculateDaysBetweenDates(date1: string, date2: string): number;
18
+ getCurrentDate(): string;
19
+ getDateFromDateString(dateString: string): Date;
20
+ getPeriodFromDate(date: string): string;
21
+ }
22
+ declare const DisacompDateService: DateService;
23
+ export default DisacompDateService;
package/dist/index.js ADDED
@@ -0,0 +1,130 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.dateClarionToSQL = exports.bothDatesFormated = void 0;
4
+ function AddCeroAtBegin(numero) {
5
+ return numero < 10 ? `0${numero}` : numero;
6
+ }
7
+ function bothDatesFormated(dateToFormat) {
8
+ let date;
9
+ if (dateToFormat) {
10
+ date = new Date(dateToFormat);
11
+ }
12
+ else {
13
+ date = new Date();
14
+ }
15
+ const day = AddCeroAtBegin(date.getUTCDate());
16
+ const month = AddCeroAtBegin(date.getUTCMonth() + 1);
17
+ const year = date.getFullYear();
18
+ const hours = AddCeroAtBegin(date.getHours());
19
+ const min = AddCeroAtBegin(date.getMinutes());
20
+ const sec = AddCeroAtBegin(date.getSeconds());
21
+ return {
22
+ signDate: `${day}-${month}-${year} ${hours}:${min}:${sec}`,
23
+ normalDate: `${day}-${month}-${year}`,
24
+ };
25
+ }
26
+ exports.bothDatesFormated = bothDatesFormated;
27
+ function dateClarionToSQL(clarionDate) {
28
+ let response;
29
+ if (clarionDate <= 0) {
30
+ response = new Date("1900-01-01");
31
+ }
32
+ else {
33
+ response = new Date("1900-01-01");
34
+ response.setUTCDate(response.getUTCDate() + (clarionDate - 36163));
35
+ }
36
+ return response.toISOString();
37
+ }
38
+ exports.dateClarionToSQL = dateClarionToSQL;
39
+ class DateHelper {
40
+ static isLeapYear(year) {
41
+ return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
42
+ }
43
+ static getDaysInMonth(month, year) {
44
+ switch (month) {
45
+ case 2:
46
+ return this.isLeapYear(year) ? 29 : 28;
47
+ case 4:
48
+ case 6:
49
+ case 9:
50
+ case 11:
51
+ return 30;
52
+ default:
53
+ return 31;
54
+ }
55
+ }
56
+ }
57
+ class DateManipulator {
58
+ addDays(date, daysToAdd) {
59
+ let [day, month, year] = date.split("-").map(Number);
60
+ while (daysToAdd > 0) {
61
+ const daysLeftInMonth = DateHelper.getDaysInMonth(month, year) - day;
62
+ if (daysToAdd <= daysLeftInMonth) {
63
+ day += daysToAdd;
64
+ daysToAdd = 0;
65
+ }
66
+ else {
67
+ daysToAdd -= daysLeftInMonth + 1;
68
+ day = 1;
69
+ month++;
70
+ if (month > 12) {
71
+ month = 1;
72
+ year++;
73
+ }
74
+ }
75
+ }
76
+ return this.formatDate(year, month, day);
77
+ }
78
+ getCurrentDate() {
79
+ const today = new Date();
80
+ const day = String(today.getUTCDate()).padStart(2, "0");
81
+ const month = String(today.getMonth() + 1).padStart(2, "0");
82
+ const year = String(today.getFullYear());
83
+ return `${day}-${month}-${year}`;
84
+ }
85
+ calculateDaysBetweenDates(date1, date2) {
86
+ const [day1, month1, year1] = date1.split("-").map(Number);
87
+ const [day2, month2, year2] = date2.split("-").map(Number);
88
+ const fomatedDate1 = new Date(year1, month1 - 1, day1);
89
+ const fomatedDate2 = new Date(year2, month2 - 1, day2);
90
+ const differenceTime = Math.abs(fomatedDate2.getTime() - fomatedDate1.getTime());
91
+ const differenceInDays = Math.ceil(differenceTime / (1000 * 60 * 60 * 24));
92
+ return differenceInDays;
93
+ }
94
+ getDateFromDateSrting(date) {
95
+ const [day, month, year] = date.split("-").map(Number);
96
+ const formatedDate = new Date(year, month - 1, day);
97
+ return formatedDate;
98
+ }
99
+ formatDate(year, month, day) {
100
+ const monthStr = month.toString().padStart(2, "0");
101
+ const dayStr = day.toString().padStart(2, "0");
102
+ return `${dayStr}-${monthStr}-${year}`;
103
+ }
104
+ getPeriodFromDate(date) {
105
+ const [day, month, year] = date.split("-").map(Number);
106
+ return `${month}-${year}`;
107
+ }
108
+ }
109
+ class DateService {
110
+ constructor(dateManipulator) {
111
+ this.dateManipulator = dateManipulator;
112
+ }
113
+ addDaysToDate(date, days) {
114
+ return this.dateManipulator.addDays(date, days);
115
+ }
116
+ calculateDaysBetweenDates(date1, date2) {
117
+ return this.dateManipulator.calculateDaysBetweenDates(date1, date2);
118
+ }
119
+ getCurrentDate() {
120
+ return this.dateManipulator.getCurrentDate();
121
+ }
122
+ getDateFromDateString(dateString) {
123
+ return this.dateManipulator.getDateFromDateSrting(dateString);
124
+ }
125
+ getPeriodFromDate(date) {
126
+ return this.dateManipulator.getPeriodFromDate(date);
127
+ }
128
+ }
129
+ const DisacompDateService = new DateService(new DateManipulator());
130
+ exports.default = DisacompDateService;
package/package.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "name": "disacomp-dateservice",
3
+ "version": "1.0.0",
4
+ "description": "Disacomp Date Service",
5
+ "main": "dist/index.js",
6
+ "scripts": {
7
+ "build": "tsc"
8
+ },
9
+ "keywords": [
10
+ "Disacomp"
11
+ ],
12
+ "author": "DISACOMP SRL",
13
+ "license": "ISC"
14
+ }
package/src/index.ts ADDED
@@ -0,0 +1,167 @@
1
+ function AddCeroAtBegin(numero: number) {
2
+ return numero < 10 ? `0${numero}` : numero;
3
+ }
4
+ export function bothDatesFormated(dateToFormat?: string) {
5
+ let date: Date;
6
+ if (dateToFormat) {
7
+ date = new Date(dateToFormat);
8
+ } else {
9
+ date = new Date();
10
+ }
11
+
12
+ const day = AddCeroAtBegin(date.getUTCDate());
13
+ const month = AddCeroAtBegin(date.getUTCMonth() + 1); // Meses son indexados desde 0
14
+ const year = date.getFullYear();
15
+
16
+ const hours = AddCeroAtBegin(date.getHours());
17
+ const min = AddCeroAtBegin(date.getMinutes());
18
+ const sec = AddCeroAtBegin(date.getSeconds());
19
+
20
+ return {
21
+ signDate: `${day}-${month}-${year} ${hours}:${min}:${sec}`,
22
+ normalDate: `${day}-${month}-${year}`,
23
+ };
24
+ }
25
+
26
+ export function dateClarionToSQL(clarionDate: number): string {
27
+ let response: Date;
28
+ if (clarionDate <= 0) {
29
+ response = new Date("1900-01-01");
30
+ } else {
31
+ response = new Date("1900-01-01");
32
+ response.setUTCDate(response.getUTCDate() + (clarionDate - 36163));
33
+ }
34
+ return response.toISOString();
35
+ }
36
+
37
+ // Interface for date manipulation
38
+ interface IDateManipulator {
39
+ addDays(date: string, daysToAdd: number): string;
40
+ getCurrentDate(): string;
41
+ getDateFromDateSrting(date: string): Date;
42
+ calculateDaysBetweenDates(date1: string, date2: string): number;
43
+ getPeriodFromDate(date: string): string;
44
+ }
45
+
46
+ // Helper class to handle leap years and days in months
47
+ class DateHelper {
48
+ public static isLeapYear(year: number): boolean {
49
+ return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
50
+ }
51
+
52
+ public static getDaysInMonth(month: number, year: number): number {
53
+ switch (month) {
54
+ case 2: // February
55
+ return this.isLeapYear(year) ? 29 : 28;
56
+ case 4:
57
+ case 6:
58
+ case 9:
59
+ case 11: // Months with 30 days
60
+ return 30;
61
+ default: // Months with 31 days
62
+ return 31;
63
+ }
64
+ }
65
+ }
66
+
67
+ // Class responsible for adding days to a date
68
+ class DateManipulator implements IDateManipulator {
69
+ public addDays(date: string, daysToAdd: number): string {
70
+ let [day, month, year] = date.split("-").map(Number);
71
+
72
+ while (daysToAdd > 0) {
73
+ const daysLeftInMonth = DateHelper.getDaysInMonth(month, year) - day;
74
+
75
+ if (daysToAdd <= daysLeftInMonth) {
76
+ day += daysToAdd;
77
+ daysToAdd = 0;
78
+ } else {
79
+ daysToAdd -= daysLeftInMonth + 1;
80
+ day = 1;
81
+ month++;
82
+
83
+ if (month > 12) {
84
+ month = 1;
85
+ year++;
86
+ }
87
+ }
88
+ }
89
+
90
+ return this.formatDate(year, month, day);
91
+ }
92
+
93
+ public getCurrentDate(): string {
94
+ const today = new Date();
95
+ const day = String(today.getUTCDate()).padStart(2, "0");
96
+ const month = String(today.getMonth() + 1).padStart(2, "0");
97
+ const year = String(today.getFullYear());
98
+
99
+ return `${day}-${month}-${year}`;
100
+ }
101
+
102
+ public calculateDaysBetweenDates(date1: string, date2: string): number {
103
+ const [day1, month1, year1] = date1.split("-").map(Number);
104
+ const [day2, month2, year2] = date2.split("-").map(Number);
105
+
106
+ const fomatedDate1 = new Date(year1, month1 - 1, day1);
107
+ const fomatedDate2 = new Date(year2, month2 - 1, day2);
108
+
109
+ const differenceTime = Math.abs(
110
+ fomatedDate2.getTime() - fomatedDate1.getTime()
111
+ );
112
+
113
+ const differenceInDays = Math.ceil(differenceTime / (1000 * 60 * 60 * 24));
114
+
115
+ return differenceInDays;
116
+ }
117
+
118
+ public getDateFromDateSrting(date: string): Date {
119
+ const [day, month, year] = date.split("-").map(Number);
120
+ const formatedDate = new Date(year, month - 1, day);
121
+ return formatedDate;
122
+ }
123
+
124
+ private formatDate(year: number, month: number, day: number): string {
125
+ const monthStr = month.toString().padStart(2, "0");
126
+ const dayStr = day.toString().padStart(2, "0");
127
+ return `${dayStr}-${monthStr}-${year}`;
128
+ }
129
+
130
+ public getPeriodFromDate(date: string): string {
131
+ const [day, month, year] = date.split("-").map(Number);
132
+
133
+ return `${month}-${year}`; // Concatenate year and month
134
+ }
135
+ }
136
+
137
+ // Client code using dependency inversion
138
+ class DateService {
139
+ private dateManipulator: IDateManipulator;
140
+
141
+ constructor(dateManipulator: IDateManipulator) {
142
+ this.dateManipulator = dateManipulator;
143
+ }
144
+
145
+ public addDaysToDate(date: string, days: number): string {
146
+ return this.dateManipulator.addDays(date, days);
147
+ }
148
+
149
+ public calculateDaysBetweenDates(date1: string, date2: string): number {
150
+ return this.dateManipulator.calculateDaysBetweenDates(date1, date2);
151
+ }
152
+
153
+ public getCurrentDate(): string {
154
+ return this.dateManipulator.getCurrentDate();
155
+ }
156
+
157
+ public getDateFromDateString(dateString: string): Date {
158
+ return this.dateManipulator.getDateFromDateSrting(dateString);
159
+ }
160
+
161
+ public getPeriodFromDate(date: string): string {
162
+ return this.dateManipulator.getPeriodFromDate(date);
163
+ }
164
+ }
165
+ const DisacompDateService = new DateService(new DateManipulator());
166
+
167
+ export default DisacompDateService;
package/tsconfig.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "compilerOptions": {
3
+ "composite": true,
4
+ "target": "es2017",
5
+ "module": "commonjs",
6
+ "rootDir": "./src",
7
+ "outDir": "dist",
8
+ "removeComments": true,
9
+ "strict": true,
10
+ "noImplicitAny": true,
11
+ "strictNullChecks": true,
12
+ "noImplicitThis": true
13
+ },
14
+ "include": ["src"]
15
+ }
@@ -0,0 +1 @@
1
+ {"program":{"fileNames":["../../../../../usr/local/lib/node_modules/typescript/lib/lib.es5.d.ts","../../../../../usr/local/lib/node_modules/typescript/lib/lib.es2015.d.ts","../../../../../usr/local/lib/node_modules/typescript/lib/lib.es2016.d.ts","../../../../../usr/local/lib/node_modules/typescript/lib/lib.es2017.d.ts","../../../../../usr/local/lib/node_modules/typescript/lib/lib.dom.d.ts","../../../../../usr/local/lib/node_modules/typescript/lib/lib.dom.iterable.d.ts","../../../../../usr/local/lib/node_modules/typescript/lib/lib.webworker.importscripts.d.ts","../../../../../usr/local/lib/node_modules/typescript/lib/lib.scripthost.d.ts","../../../../../usr/local/lib/node_modules/typescript/lib/lib.es2015.core.d.ts","../../../../../usr/local/lib/node_modules/typescript/lib/lib.es2015.collection.d.ts","../../../../../usr/local/lib/node_modules/typescript/lib/lib.es2015.generator.d.ts","../../../../../usr/local/lib/node_modules/typescript/lib/lib.es2015.iterable.d.ts","../../../../../usr/local/lib/node_modules/typescript/lib/lib.es2015.promise.d.ts","../../../../../usr/local/lib/node_modules/typescript/lib/lib.es2015.proxy.d.ts","../../../../../usr/local/lib/node_modules/typescript/lib/lib.es2015.reflect.d.ts","../../../../../usr/local/lib/node_modules/typescript/lib/lib.es2015.symbol.d.ts","../../../../../usr/local/lib/node_modules/typescript/lib/lib.es2015.symbol.wellknown.d.ts","../../../../../usr/local/lib/node_modules/typescript/lib/lib.es2016.array.include.d.ts","../../../../../usr/local/lib/node_modules/typescript/lib/lib.es2017.object.d.ts","../../../../../usr/local/lib/node_modules/typescript/lib/lib.es2017.sharedmemory.d.ts","../../../../../usr/local/lib/node_modules/typescript/lib/lib.es2017.string.d.ts","../../../../../usr/local/lib/node_modules/typescript/lib/lib.es2017.intl.d.ts","../../../../../usr/local/lib/node_modules/typescript/lib/lib.es2017.typedarrays.d.ts","../../../../../usr/local/lib/node_modules/typescript/lib/lib.decorators.d.ts","../../../../../usr/local/lib/node_modules/typescript/lib/lib.decorators.legacy.d.ts","../../../../../usr/local/lib/node_modules/typescript/lib/lib.es2017.full.d.ts","./src/index.ts"],"fileInfos":[{"version":"f59215c5f1d886b05395ee7aca73e0ac69ddfad2843aa88530e797879d511bad","affectsGlobalScope":true},"45b7ab580deca34ae9729e97c13cfd999df04416a79116c3bfb483804f85ded4","dc48272d7c333ccf58034c0026162576b7d50ea0e69c3b9292f803fc20720fd5","27147504487dc1159369da4f4da8a26406364624fa9bc3db632f7d94a5bae2c3",{"version":"3dda5344576193a4ae48b8d03f105c86f20b2f2aff0a1d1fd7935f5d68649654","affectsGlobalScope":true},{"version":"35299ae4a62086698444a5aaee27fc7aa377c68cbb90b441c9ace246ffd05c97","affectsGlobalScope":true},{"version":"c5c5565225fce2ede835725a92a28ece149f83542aa4866cfb10290bff7b8996","affectsGlobalScope":true},{"version":"7d2dbc2a0250400af0809b0ad5f84686e84c73526de931f84560e483eb16b03c","affectsGlobalScope":true},{"version":"9d9885c728913c1d16e0d2831b40341d6ad9a0ceecaabc55209b306ad9c736a5","affectsGlobalScope":true},{"version":"17bea081b9c0541f39dd1ae9bc8c78bdd561879a682e60e2f25f688c0ecab248","affectsGlobalScope":true},{"version":"4443e68b35f3332f753eacc66a04ac1d2053b8b035a0e0ac1d455392b5e243b3","affectsGlobalScope":true},{"version":"ab22100fdd0d24cfc2cc59d0a00fc8cf449830d9c4030dc54390a46bd562e929","affectsGlobalScope":true},{"version":"f7bd636ae3a4623c503359ada74510c4005df5b36de7f23e1db8a5c543fd176b","affectsGlobalScope":true},{"version":"ce691fb9e5c64efb9547083e4a34091bcbe5bdb41027e310ebba8f7d96a98671","affectsGlobalScope":true},{"version":"8d697a2a929a5fcb38b7a65594020fcef05ec1630804a33748829c5ff53640d0","affectsGlobalScope":true},{"version":"0c20f4d2358eb679e4ae8a4432bdd96c857a2960fd6800b21ec4008ec59d60ea","affectsGlobalScope":true},{"version":"36ae84ccc0633f7c0787bc6108386c8b773e95d3b052d9464a99cd9b8795fbec","affectsGlobalScope":true},{"version":"82d0d8e269b9eeac02c3bd1c9e884e85d483fcb2cd168bccd6bc54df663da031","affectsGlobalScope":true},{"version":"b8deab98702588840be73d67f02412a2d45a417a3c097b2e96f7f3a42ac483d1","affectsGlobalScope":true},{"version":"4738f2420687fd85629c9efb470793bb753709c2379e5f85bc1815d875ceadcd","affectsGlobalScope":true},{"version":"2f11ff796926e0832f9ae148008138ad583bd181899ab7dd768a2666700b1893","affectsGlobalScope":true},{"version":"376d554d042fb409cb55b5cbaf0b2b4b7e669619493c5d18d5fa8bd67273f82a","affectsGlobalScope":true},{"version":"9fc46429fbe091ac5ad2608c657201eb68b6f1b8341bd6d670047d32ed0a88fa","affectsGlobalScope":true},{"version":"189c0703923150aa30673fa3de411346d727cc44a11c75d05d7cf9ef095daa22","affectsGlobalScope":true},{"version":"782dec38049b92d4e85c1585fbea5474a219c6984a35b004963b00beb1aab538","affectsGlobalScope":true},"994c234848afc14a2586b6565777f4c0b05dc479ede0a041bfd5becf6dceb586",{"version":"07831f807514afbc10818f997b7825969712b0bf110593845249abe17105d7bd","signature":"5163fa2cf70f01f0c3e09202f543d138f56675ec88cfdddd5566d45f89e8fadf"}],"root":[27],"options":{"composite":true,"module":1,"noImplicitAny":true,"noImplicitThis":true,"outDir":"./dist","removeComments":true,"rootDir":"./src","strict":true,"strictNullChecks":true,"target":4},"referencedMap":[],"exportedModulesMap":[],"semanticDiagnosticsPerFile":[27,24,25,5,6,10,9,2,11,12,13,14,15,16,17,18,3,4,26,22,19,20,21,23,1,8,7],"latestChangedDtsFile":"./dist/index.d.ts"},"version":"5.1.6"}