@cellaware/utils 3.2.22 → 3.3.23

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.
@@ -1,12 +1,15 @@
1
- export declare const COSMOS_CURRENT_YEAR_CLAUSE = "DateTimePart('yyyy', GetCurrentDateTime())";
2
- export declare const COSMOS_RECORD_YEAR_CLAUSE = "DateTimePart('yyyy', TimestampToDateTime(c._ts*1000))";
3
- export declare const COSMOS_CURRENT_MONTH_CLAUSE = "DateTimePart('mm', GetCurrentDateTime())";
4
- export declare const COSMOS_RECORD_MONTH_CLAUSE = "DateTimePart('mm', TimestampToDateTime(c._ts*1000))";
5
- export declare const COSMOS_CURRENT_DAY_CLAUSE = "DateTimePart('dd', GetCurrentDateTime())";
6
- export declare const COSMOS_RECORD_DAY_CLAUSE = "DateTimePart('dd', TimestampToDateTime(c._ts*1000))";
7
- export declare const COSMOS_CURRENT_DAY_CONDITION_CLAUSE = "DateTimeDiff('dd', TimestampToDateTime(c._ts*1000), GetCurrentDateTime()) = 0";
8
- export declare const COSMOS_CURRENT_MONTH_CONDITION_CLAUSE = "DateTimeDiff('mm', TimestampToDateTime(c._ts*1000), GetCurrentDateTime()) = 0";
9
- export declare const COSMOS_CURRENT_YEAR_CONDITION_CLAUSE = "DateTimeDiff('yyyy', TimestampToDateTime(c._ts*1000), GetCurrentDateTime()) = 0";
1
+ export declare function getCosmosCurrentYear(timeZone?: string): string;
2
+ export declare function getCosmosTimestampYear(timeZone?: string): string;
3
+ export declare function getCosmosCurrentMonth(timeZone?: string): string;
4
+ export declare function getCosmosTimestampMonth(timeZone?: string): string;
5
+ export declare function getCosmosCurrentDay(timeZone?: string): string;
6
+ export declare function getCosmosTimestampDay(timeZone?: string): string;
7
+ export declare function getCosmosTimestampHour(timeZone?: string): string;
8
+ export declare function getCosmosTimestampMinute(timeZone?: string): string;
9
+ export declare function getCosmosTimestampSecond(timeZone?: string): string;
10
+ export declare function getCosmosCurrentDayWhereClause(timeZone?: string): string;
11
+ export declare function getCosmosCurrentMonthWhereClause(timeZone?: string): string;
12
+ export declare function getCosmosCurrentYearWhereClause(timeZone?: string): string;
10
13
  export declare function cosmosSelect(databaseId: string, collectionId: string, partitionKey: string, query: string): Promise<any[]>;
11
14
  export declare function cosmosInsert(databaseId: string, collectionId: string, partitionKey: string, data: any): Promise<boolean>;
12
15
  export declare function cosmosDelete(databaseId: string, collectionId: string, partitionKey: string, query: string): Promise<boolean>;
@@ -1,13 +1,75 @@
1
1
  import { CosmosClient } from '@azure/cosmos';
2
- export const COSMOS_CURRENT_YEAR_CLAUSE = `DateTimePart('yyyy', GetCurrentDateTime())`;
3
- export const COSMOS_RECORD_YEAR_CLAUSE = `DateTimePart('yyyy', TimestampToDateTime(c._ts*1000))`;
4
- export const COSMOS_CURRENT_MONTH_CLAUSE = `DateTimePart('mm', GetCurrentDateTime())`;
5
- export const COSMOS_RECORD_MONTH_CLAUSE = `DateTimePart('mm', TimestampToDateTime(c._ts*1000))`;
6
- export const COSMOS_CURRENT_DAY_CLAUSE = `DateTimePart('dd', GetCurrentDateTime())`;
7
- export const COSMOS_RECORD_DAY_CLAUSE = `DateTimePart('dd', TimestampToDateTime(c._ts*1000))`;
8
- export const COSMOS_CURRENT_DAY_CONDITION_CLAUSE = `DateTimeDiff('dd', TimestampToDateTime(c._ts*1000), GetCurrentDateTime()) = 0`;
9
- export const COSMOS_CURRENT_MONTH_CONDITION_CLAUSE = `DateTimeDiff('mm', TimestampToDateTime(c._ts*1000), GetCurrentDateTime()) = 0`;
10
- export const COSMOS_CURRENT_YEAR_CONDITION_CLAUSE = `DateTimeDiff('yyyy', TimestampToDateTime(c._ts*1000), GetCurrentDateTime()) = 0`;
2
+ import { isDaylightSavingTime } from '../util.js';
3
+ const COSMOS_CURRENT_DATETIME = `GetCurrentDateTime()`;
4
+ const COSMOS_TIMESTAMP_DATETIME = `TimestampToDateTime(c._ts*1000)`;
5
+ /**
6
+ * All Cosmos timestamps are UTC.
7
+ */
8
+ function getCosmosTimeZoneHourDiff(timeZone) {
9
+ let diff = 0;
10
+ if (!!timeZone) {
11
+ const dst = isDaylightSavingTime(timeZone);
12
+ switch (timeZone) {
13
+ case 'America/New_York':
14
+ diff = dst ? -4 : -5;
15
+ break;
16
+ case 'America/Chicago':
17
+ diff = dst ? -5 : -6;
18
+ break;
19
+ case 'America/Denver':
20
+ diff = dst ? -6 : -7;
21
+ break;
22
+ case 'America/Los_Angeles':
23
+ diff = dst ? -7 : -8;
24
+ break;
25
+ default:
26
+ break;
27
+ }
28
+ }
29
+ return diff;
30
+ }
31
+ function getCosmosCurrentDateTime(timeZone) {
32
+ return `DateTimeAdd("hh", ${getCosmosTimeZoneHourDiff(timeZone)}, ${COSMOS_CURRENT_DATETIME})`;
33
+ }
34
+ function getCosmosTimestampDateTime(timeZone) {
35
+ return `DateTimeAdd("hh", ${getCosmosTimeZoneHourDiff(timeZone)}, ${COSMOS_TIMESTAMP_DATETIME})`;
36
+ }
37
+ export function getCosmosCurrentYear(timeZone) {
38
+ return `DateTimePart('yyyy', ${getCosmosCurrentDateTime(timeZone)})`;
39
+ }
40
+ export function getCosmosTimestampYear(timeZone) {
41
+ return `DateTimePart('yyyy', ${getCosmosTimestampDateTime(timeZone)})`;
42
+ }
43
+ export function getCosmosCurrentMonth(timeZone) {
44
+ return `DateTimePart('mm', ${getCosmosCurrentDateTime(timeZone)})`;
45
+ }
46
+ export function getCosmosTimestampMonth(timeZone) {
47
+ return `DateTimePart('mm', ${getCosmosTimestampDateTime(timeZone)})`;
48
+ }
49
+ export function getCosmosCurrentDay(timeZone) {
50
+ return `DateTimePart('dd', ${getCosmosCurrentDateTime(timeZone)})`;
51
+ }
52
+ export function getCosmosTimestampDay(timeZone) {
53
+ return `DateTimePart('dd', ${getCosmosTimestampDateTime(timeZone)})`;
54
+ }
55
+ export function getCosmosTimestampHour(timeZone) {
56
+ return `DateTimePart('hh', ${getCosmosTimestampDateTime(timeZone)})`;
57
+ }
58
+ export function getCosmosTimestampMinute(timeZone) {
59
+ return `DateTimePart('mi', ${getCosmosTimestampDateTime(timeZone)})`;
60
+ }
61
+ export function getCosmosTimestampSecond(timeZone) {
62
+ return `DateTimePart('ss', ${getCosmosTimestampDateTime(timeZone)})`;
63
+ }
64
+ export function getCosmosCurrentDayWhereClause(timeZone) {
65
+ return `DateTimeDiff('dd', ${getCosmosTimestampDateTime(timeZone)}, ${getCosmosCurrentDateTime(timeZone)}) = 0`;
66
+ }
67
+ export function getCosmosCurrentMonthWhereClause(timeZone) {
68
+ return `DateTimeDiff('mm', ${getCosmosTimestampDateTime(timeZone)}, ${getCosmosCurrentDateTime(timeZone)}) = 0`;
69
+ }
70
+ export function getCosmosCurrentYearWhereClause(timeZone) {
71
+ return `DateTimeDiff('yyyy', ${getCosmosTimestampDateTime(timeZone)}, ${getCosmosCurrentDateTime(timeZone)}) = 0`;
72
+ }
11
73
  export async function cosmosSelect(databaseId, collectionId, partitionKey, query) {
12
74
  try {
13
75
  const cosmosClient = new CosmosClient({
package/dist/util.d.ts CHANGED
@@ -1,9 +1,11 @@
1
1
  export declare function sleep(ms: number): Promise<any>;
2
2
  export declare function removeMarkdownIndicators(input: string): string;
3
3
  export declare function removePrefixIndicators(input: string, prefixes: string[]): string;
4
- export declare function isLeapYear(): boolean;
5
- export declare function getDaysInMonth(): number;
6
- export declare function getCurrentDayInMonth(): number;
7
- export declare function getDaysInYear(): 366 | 365;
8
- export declare function getCurrentMonth(): number;
9
- export declare function getCurrentYear(): number;
4
+ export declare function initDate(timeZone?: string): Date;
5
+ export declare function isDaylightSavingTime(timeZone?: string): boolean;
6
+ export declare function isLeapYear(timeZone?: string): boolean;
7
+ export declare function getDaysInMonth(timeZone?: string): number;
8
+ export declare function getCurrentDayInMonth(timeZone?: string): number;
9
+ export declare function getDaysInYear(timeZone?: string): 366 | 365;
10
+ export declare function getCurrentMonth(timeZone?: string): number;
11
+ export declare function getCurrentYear(timeZone?: string): number;
package/dist/util.js CHANGED
@@ -22,28 +22,43 @@ export function removePrefixIndicators(input, prefixes) {
22
22
  }
23
23
  return output;
24
24
  }
25
- export function isLeapYear() {
26
- let year = new Date().getFullYear();
25
+ export function initDate(timeZone) {
26
+ const date = new Date();
27
+ if (!!timeZone) {
28
+ return new Date(date.toLocaleString('en-US', { timeZone }));
29
+ }
30
+ else {
31
+ return date;
32
+ }
33
+ }
34
+ export function isDaylightSavingTime(timeZone) {
35
+ const date = initDate(timeZone);
36
+ const jan = new Date(date.getFullYear(), 0, 1);
37
+ const jul = new Date(date.getFullYear(), 6, 1);
38
+ return date.getTimezoneOffset() < Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset());
39
+ }
40
+ export function isLeapYear(timeZone) {
41
+ let year = initDate(timeZone).getFullYear();
27
42
  return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
28
43
  }
29
- export function getDaysInMonth() {
30
- let date = new Date();
44
+ export function getDaysInMonth(timeZone) {
45
+ let date = initDate(timeZone);
31
46
  // NOTE: month is 0-based.
32
47
  return new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate();
33
48
  }
34
- export function getCurrentDayInMonth() {
35
- let date = new Date();
49
+ export function getCurrentDayInMonth(timeZone) {
50
+ let date = initDate(timeZone);
36
51
  return date.getDate();
37
52
  }
38
- export function getDaysInYear() {
39
- return isLeapYear() ? 366 : 365;
53
+ export function getDaysInYear(timeZone) {
54
+ return isLeapYear(timeZone) ? 366 : 365;
40
55
  }
41
- export function getCurrentMonth() {
42
- let date = new Date();
56
+ export function getCurrentMonth(timeZone) {
57
+ let date = initDate(timeZone);
43
58
  // NOTE: month is 0-based.
44
59
  return date.getMonth() + 1;
45
60
  }
46
- export function getCurrentYear() {
47
- let date = new Date();
61
+ export function getCurrentYear(timeZone) {
62
+ let date = initDate(timeZone);
48
63
  return date.getFullYear();
49
64
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cellaware/utils",
3
- "version": "3.2.22",
3
+ "version": "3.3.23",
4
4
  "description": "Cellaware Utilities for Node.js",
5
5
  "author": "Cellaware Technologies",
6
6
  "type": "module",