Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to pnpm to fix jest tests and datetime functions #210

Merged
merged 3 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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