@dative-gpi/foundation-shared-services 1.0.129 → 1.0.130

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.
@@ -53,8 +53,8 @@ export const useAppTimeZone = () => {
53
53
  return (parseInt(hours) * 60 + parseInt(minutes)) * 60 * 1000;
54
54
  }
55
55
 
56
- const getOffsetDifference = (): number => {
57
- return getUserOffset() - getMachineOffset();
56
+ const getOffsetDifference = (epoch: number | null = null): number => {
57
+ return getUserOffset(epoch) - getMachineOffset(epoch);
58
58
  };
59
59
 
60
60
  const ready = computed(() => timeZone.value !== null);
@@ -4,4 +4,5 @@ export * from "./app";
4
4
  export * from "./useDateFormat";
5
5
  export * from "./useFiles";
6
6
  export * from "./useFoundationShared";
7
- export * from "./useRouting";
7
+ export * from "./useRouting";
8
+ export * from "./useTermFieldDate";
@@ -9,7 +9,7 @@ export const useDateFormat = () => {
9
9
  const { $tr } = useTranslationsProvider();
10
10
 
11
11
  const { languageCode } = useAppLanguageCode();
12
- const { timeZone, getOffsetDifference, getMachineOffset, getUserOffset } = useAppTimeZone();
12
+ const { timeZone, getOffsetDifference, getMachineOffset } = useAppTimeZone();
13
13
 
14
14
  const isEpochToday = (value: number | null | undefined): boolean => {
15
15
  if (value == null || !isFinite(value)) {
@@ -43,6 +43,14 @@ export const useDateFormat = () => {
43
43
  return date.toLocaleString(languageCode.value, { ...OPTIONS.dayMonthLongOnly, timeZone: timeZone.value });
44
44
  }
45
45
 
46
+ const epochToDayMonthShortOnly = (value: number | null | undefined): string => {
47
+ if (value == null || !isFinite(value)) {
48
+ return "";
49
+ }
50
+ const date = new Date(value);
51
+ return date.toLocaleString(languageCode.value, { ...OPTIONS.dayMonthShortOnly, timeZone: timeZone.value });
52
+ }
53
+
46
54
  const epochToShortDateFormat = (value: number | null | undefined): string => {
47
55
  if (value == null || !isFinite(value)) {
48
56
  return "";
@@ -74,6 +82,14 @@ export const useDateFormat = () => {
74
82
  return date.toLocaleString(languageCode.value, { ...OPTIONS.shortTime, timeZone: timeZone.value });
75
83
  };
76
84
 
85
+ const epochToMonthShortTimeFormat = (value: number | null | undefined): string => {
86
+ if (value == null || !isFinite(value)) {
87
+ return "";
88
+ }
89
+ const date = new Date(value);
90
+ return date.toLocaleString(languageCode.value, { ...OPTIONS.monthShortTime, timeZone: timeZone.value });
91
+ };
92
+
77
93
  const epochToLocalDayStart = (value: number | null | undefined): number => {
78
94
  if (value == null || !isFinite(value)) {
79
95
  return 0;
@@ -146,14 +162,14 @@ export const useDateFormat = () => {
146
162
 
147
163
  const pickerToEpoch = (value: Date | null | undefined): number => {
148
164
  if (value != null) {
149
- return value.getTime() - getOffsetDifference();
165
+ return value.getTime() - getOffsetDifference(value.getTime());
150
166
  }
151
167
  return 0;
152
168
  };
153
169
 
154
170
  const epochToPicker = (value: number | null | undefined): Date => {
155
171
  if (value != null) {
156
- return new Date(value + getOffsetDifference());
172
+ return new Date(value + getOffsetDifference(value));
157
173
  }
158
174
  return new Date(0);
159
175
  };
@@ -181,10 +197,14 @@ export const useDateFormat = () => {
181
197
  };
182
198
 
183
199
  const parseForPicker = (value: string, dateFormat: string = ISO_FORMAT): number | null => {
184
- const date = addMilliseconds(parse(value, dateFormat, new Date()), getUserOffset());
185
- if (!isFinite(date.getTime())) {
200
+ // parse the date with the machine's current timezone. The last parameter is used to fill the gaps in the date string
201
+ const dateWithoutCorrection = parse(value, dateFormat, new Date());
202
+ if (!isFinite(dateWithoutCorrection.getTime())) {
186
203
  return null;
187
204
  }
205
+ // check if the timezone at the time of the date is different from the current machine timezone
206
+ // adjust if needed (this is the case when the date is at summer time and the machine is at winter time, for example)
207
+ const date = addMilliseconds(dateWithoutCorrection, getMachineOffset(dateWithoutCorrection.getTime()));
188
208
  return date.getTime();
189
209
  };
190
210
 
@@ -209,6 +229,8 @@ export const useDateFormat = () => {
209
229
  epochToMonthYearOnlyFormat,
210
230
  epochToShortDateFormat,
211
231
  epochToShortTimeFormat,
232
+ epochToDayMonthShortOnly,
233
+ epochToMonthShortTimeFormat,
212
234
  epochToShortTimeOnlyFormat,
213
235
  epochToTimeOnlyFormat,
214
236
  epochToWeekNumber,
@@ -0,0 +1,138 @@
1
+ import { useAppTimeZone } from './app/useAppTimeZone';
2
+ import { useDateFormat } from '@dative-gpi/foundation-shared-services/composables/useDateFormat';
3
+
4
+ export const useTermFieldDate = () => {
5
+ const { getOffsetDifference } = useAppTimeZone();
6
+ const { parseForPicker } = useDateFormat();
7
+
8
+ const EXPRESSION = /(?:(?:([-\+])(\d*))?(\w+))?(?:\/(\w))?/g;
9
+ const NOW = 'now';
10
+
11
+ const convert = (value: string, variables: { [key: string]: number | string } = {}): number => {
12
+ //Try to convert the value to a date
13
+ const date = parseForPicker(value);
14
+ if (date) {
15
+ return date;
16
+ }
17
+
18
+ const currentVariables = {
19
+ ...variables,
20
+ [NOW]: new Date().getTime()
21
+ };
22
+
23
+ return applyFormula(value, currentVariables);
24
+ }
25
+
26
+ const applyFormula = (expression: string, variables: { [key: string]: number | string }): number => {
27
+ expression = expression.replace(/\s/g, '');
28
+ const matches = expression.matchAll(EXPRESSION);
29
+
30
+ if (!matches) {
31
+ console.error('Invalid expression');
32
+ return NaN;
33
+ }
34
+
35
+ let timestamp = 0;
36
+
37
+ // Retrieving group in order are: operator (+ or -), number, unit, and special (variable)
38
+ matches.forEach(match => {
39
+ const operator = match[1];
40
+ const number = match[2];
41
+ const unit = match[3];
42
+ const special = match[4];
43
+
44
+ if (unit) {
45
+ timestamp = applyUnit(timestamp, operator, number, unit, variables);
46
+ }
47
+
48
+ if (special) {
49
+ timestamp = applySpecial(timestamp, special);
50
+ }
51
+ });
52
+
53
+ return timestamp;
54
+ }
55
+
56
+ const applyUnit = (timestamp: number, operator: string, number: string, unit: string, variables: { [key: string]: number | string }): number => {
57
+ let factor = operator === '-' ? -1 : 1;
58
+ factor *= Number.isNaN(parseInt(number)) ? 1 : parseInt(number);
59
+
60
+ switch (unit) {
61
+ case 's':
62
+ return timestamp + factor * 1000;
63
+ case 'm':
64
+ return timestamp + factor * 60 * 1000;
65
+ case 'h':
66
+ return timestamp + factor * 60 * 60 * 1000;
67
+ case 'd':
68
+ return new Date(timestamp).setDate(new Date(timestamp).getDate() + factor)
69
+ case 'w':
70
+ return new Date(timestamp).setDate(new Date(timestamp).getDate() + factor * 7);
71
+ case 'M':
72
+ return new Date(timestamp).setMonth(new Date(timestamp).getMonth() + factor);
73
+ case 'y':
74
+ return new Date(timestamp).setFullYear(new Date(timestamp).getFullYear() + factor);
75
+ default:
76
+ if(variables[unit]) {
77
+ const variableValue = getVariableValue(unit, variables);
78
+ return timestamp + factor * variableValue;
79
+ }
80
+ else {
81
+ console.error(`Invalid unit expression, unit ${unit} is not defined in the variables`);
82
+ return timestamp;
83
+ }
84
+ }
85
+ }
86
+
87
+ const getVariableValue = (unit: string, variables: { [key: string]: number | string }): number => {
88
+ if(variables[unit]) {
89
+ const variableValue = parseInt(variables[unit].toString());
90
+ if (typeof variables[unit] === 'number') {
91
+ return variableValue;
92
+ } else if (!isNaN(variableValue)) {
93
+ return variableValue;
94
+ } else {
95
+ const variableExpression = variables[unit].toString();
96
+ const variableValue = applyFormula(variableExpression, variables);
97
+ if (!isNaN(variableValue)) {
98
+ return variableValue;
99
+ } else {
100
+ console.error(`Invalid unit expression, unit ${unit} is not defined in the variables`);
101
+ return 0;
102
+ }
103
+ }
104
+ }
105
+ else {
106
+ console.error(`Invalid unit expression, unit ${unit} is not defined in the variables`);
107
+ return 0;
108
+ }
109
+ }
110
+
111
+ const applySpecial = (timestamp: number, special: string): number => {
112
+ timestamp += getOffsetDifference(timestamp);
113
+ switch (special) {
114
+ case "h":
115
+ return new Date(timestamp).setMinutes(0, 0, 0) - getOffsetDifference(timestamp);
116
+ case "d":
117
+ return new Date(timestamp).setHours(0, 0, 0, 0) - getOffsetDifference(timestamp);
118
+ case "w":
119
+ const date = new Date(timestamp);
120
+ const day = date.getDay();
121
+ const diff = date.getDate() - day + (day === 0 ? -6 : 1);
122
+ return new Date(date.setDate(diff)).setHours(0, 0, 0, 0) - getOffsetDifference(timestamp);
123
+ case "M":
124
+ const dateM = new Date(timestamp);
125
+ return new Date(dateM.setMonth(dateM.getMonth(), 1)).setHours(0, 0, 0, 0) - getOffsetDifference(timestamp);
126
+ case "y":
127
+ const dateY = new Date(timestamp);
128
+ return new Date(dateY.setMonth(0, 1)).setHours(0, 0, 0, 0) - getOffsetDifference(timestamp);
129
+ default:
130
+ break;
131
+ }
132
+ return timestamp;
133
+ }
134
+
135
+ return {
136
+ convert
137
+ }
138
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@dative-gpi/foundation-shared-services",
3
3
  "sideEffects": false,
4
- "version": "1.0.129",
4
+ "version": "1.0.130",
5
5
  "description": "",
6
6
  "publishConfig": {
7
7
  "access": "public"
@@ -10,7 +10,7 @@
10
10
  "author": "",
11
11
  "license": "ISC",
12
12
  "dependencies": {
13
- "@dative-gpi/foundation-shared-domain": "1.0.129"
13
+ "@dative-gpi/foundation-shared-domain": "1.0.130"
14
14
  },
15
15
  "peerDependencies": {
16
16
  "@dative-gpi/bones-ui": "^1.0.0",
@@ -18,5 +18,5 @@
18
18
  "vue": "^3.4.38",
19
19
  "vue-router": "^4.3.0"
20
20
  },
21
- "gitHead": "73ebbdb8cfd158e2748933ec57315bc1b5fc4835"
21
+ "gitHead": "fc680c25f1b54df6a7c9195343556c0c93f1454d"
22
22
  }