install and use luxon for date and tz functions

This commit is contained in:
Dobromir Popov
2024-05-05 01:42:11 +03:00
parent 0348a2603b
commit 2cea934efd
6 changed files with 65 additions and 24 deletions

View File

@ -10,7 +10,7 @@ const { PrismaClient, UserRole } = require('@prisma/client');
const DayOfWeek = require("@prisma/client").DayOfWeek;
const winston = require('winston');
const { getSession } = require("next-auth/react");
const { DateTime, FixedOffsetZone } = require('luxon');
const logger = winston.createLogger({
level: 'info', // Set the default log level
@ -36,6 +36,27 @@ exports.logger = logger;
// dotenv.config();
// // dotenv.config({ path: ".env.local" });
exports.adjustUtcTimeToSofia = function (time) {
// Convert the Date object to a Luxon DateTime object in UTC
let result = DateTime.fromJSDate(time, { zone: 'utc' });
// Convert to Sofia time, retaining the local time as provided
result = result.setZone('Europe/Sofia', { keepLocalTime: true });
// Set hours, minutes, and seconds to match the input time
result = result.set({
hour: time.getHours(),
minute: time.getMinutes(),
second: time.getSeconds()
});
return result.toJSDate();
};
exports.adjustTimeToUTC = function (time) {
let result = DateTime.fromJSDate(time, { zone: 'Europe/Sofia' });
result = result.setZone('utc', { keepLocalTime: true });
return result.toJSDate();
};
exports.isValidPhoneNumber = function (phone) {
if (typeof phone !== 'string') {
return false; // or handle as you see fit
@ -366,11 +387,6 @@ exports.getDateFormatedShort = function (date) {
}
exports.getTimeFomatted = function (date) {
return date.toLocaleTimeString('en-US', { hour12: false, hour: '2-digit', minute: '2-digit', timeZone: 'Europe/Sofia' });//timeZone: 'local'
}
/*Todo: remove:
toISOString
slice(0, 10)
@ -544,9 +560,11 @@ exports.getCurrentYearMonth = () => {
const month = String(currentDate.getMonth() + 1).padStart(2, '0'); // Month is 0-indexed
return `${year}-${month}`;
}
exports.getTimeFormated = function (date) {
return this.formatTimeHHmm(date);
}
exports.getTimeFormatted = function (date) {
const dateTime = DateTime.fromJSDate(date, { zone: 'Europe/Sofia' });
return dateTime.toFormat('HH:mm');
};
// format date to 'HH:mm' time string required by the time picker
exports.formatTimeHHmm = function (input) {
// Check if the input is a string or a Date object