Skip to content

Commit

Permalink
Merge pull request #210 from mikepsinn/develop
Browse files Browse the repository at this point in the history
Switch to pnpm to fix jest tests and datetime functions
  • Loading branch information
mikepsinn authored Oct 1, 2024
2 parents 0e34d7f + 3dbd4ff commit 473bce6
Show file tree
Hide file tree
Showing 5 changed files with 15,984 additions and 11,202 deletions.
65 changes: 58 additions & 7 deletions apps/nextjs/lib/dateTimeWithTimezone.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,42 @@
export function getUtcDateTimeWithTimezone() {
/**
* Returns the current date and time in UTC with the timezone offset applied.
*
* @returns {string} The current date and time in UTC as an ISO string.
*
* @example
* const utcDateTime = getUtcDateTimeWithTimezone();
* console.log(utcDateTime); // Outputs: "2023-10-01T10:00:00.000Z"
*/
export function getUtcDateTimeWithTimezone(): string {
const date = new Date();
const timezoneOffset = date.getTimezoneOffset();
return new Date(date.getTime() - timezoneOffset * 60000).toISOString();
}

export function convertToUTC(localDateTime: string, timezoneOffset: number) {
/**
* Converts a local date-time string to a UTC date-time string.
*
* @param localDateTime - The local date-time string in the format "YYYY-MM-DDThh:mm:ss".
* @param timezoneOffsetInMinutes - The timezone offset in minutes (e.g., -120 for UTC+2).
* @returns The UTC date-time string in ISO format.
*/
export function convertToUTC(localDateTime: string, timezoneOffsetInMinutes: number): string {
// Convert the localDateTime string to a Date object
const localDate = new Date(localDateTime);
return new Date(localDate.getTime() + timezoneOffset * 60000).toISOString();

// Get the local time zone offset in milliseconds
const localOffset = localDate.getTimezoneOffset() * 60 * 1000;

// Calculate the UTC time in milliseconds
const utcTime = localDate.getTime() + localOffset;

// Adjust for the provided timezone offset
const adjustedTime = utcTime + (timezoneOffsetInMinutes * 60 * 1000);

// Create a new Date object using the adjusted UTC time
const utcDate = new Date(adjustedTime);

return utcDate.toUTCString();
}

export function throwErrorIfDateInFuture(utcDateTime: string) {
Expand All @@ -17,17 +47,38 @@ export function throwErrorIfDateInFuture(utcDateTime: string) {
}
}

export function getUtcDateTime(){
/**
* Retrieves the current date and time in ISO 8601 format, expressed in Coordinated Universal Time (UTC).
*
* @return {string} The current date and time in ISO 8601 format, in UTC timezone.
*
* @example
* const currentUtcDateTime = getUtcDateTime();
* console.log(currentUtcDateTime); // Outputs: "2023-10-01T10:00:00.000Z"
*/
export function getUtcDateTime(): string {
return new Date().toISOString();
}

export function getTimeZoneOffset(){
return new Date().getTimezoneOffset();
}

export function convertToLocalDateTime(utcDateTime: string | number | Date, timeZoneOffset: number){
/**
* Converts a UTC date-time string to a local date-time string.
*
* @param {string | number | Date} utcDateTime - The UTC date-time to convert.
* @param {number} timeZoneOffsetInMinutes - The timezone offset in minutes (e.g., -120 for UTC+2).
* @returns {string} The local date-time string in ISO format.
*
* @example
* const localDateTime = convertToLocalDateTime("2023-10-01T10:00:00.000Z", -120);
* console.log(localDateTime); // Outputs: "2023-10-01T08:00:00.000Z"
*/
export function convertToLocalDateTime(
utcDateTime: string | number | Date,
timeZoneOffsetInMinutes: number): string {
const utcDate = new Date(utcDateTime);
const localDate = new Date(utcDate.getTime() - timeZoneOffset * 60 * 60 * 1000);
const localDate = new Date(utcDate.getTime() + timeZoneOffsetInMinutes * 60 * 1000);
return localDate.toISOString();
}

5 changes: 3 additions & 2 deletions apps/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
"flowise-embed": "^1.2.1",
"flowise-embed-react": "^1.0.2",
"fs-extra": "^11.2.0",
"glob": "^10.3.12",
"gray-matter": "^4.0.3",
"highcharts": "^11.4.1",
"highcharts-react-official": "^3.2.1",
Expand Down Expand Up @@ -123,7 +122,9 @@
"prettier": "^3.2.5",
"prettier-plugin-tailwindcss": "^0.5.11",
"prisma": "^5.9.1",
"ts-jest": "^29.1.2"
"ts-jest": "^29.1.4",
"ts-node": "latest",
"typescript": "latest"
},
"trigger.dev": {
"endpointId": "the-decentralized-fda-1W-N"
Expand Down
Loading

0 comments on commit 473bce6

Please sign in to comment.