fix dates ant TZ, cleanup

This commit is contained in:
Dobromir Popov
2024-05-05 20:43:47 +02:00
parent 227c1f0ab3
commit 12503409b6
7 changed files with 190 additions and 154 deletions

View File

@ -339,14 +339,8 @@ exports.compareTimes = function (time1, time2) {
const time2String = `${getHours(time2)}:${getMinutes(time2)}`;
return time1String.localeCompare(time2String);
};
exports.normalizeTime = function (date, baseDate) {
// return set(baseDate, {
// hours: getHours(date),
// minutes: getMinutes(date),
// seconds: getSeconds(date),
// milliseconds: 0
// });
//don't use date-fns
let newDate = new Date(baseDate);
newDate.setHours(date.getHours(), date.getMinutes(), date.getSeconds(), 0);
return newDate;
@ -549,54 +543,96 @@ exports.getCurrentYearMonth = () => {
const month = String(currentDate.getMonth() + 1).padStart(2, '0'); // Month is 0-indexed
return `${year}-${month}`;
}
exports.getTimeFormatted = function (date) {
const dateTime = DateTime.fromJSDate(date, { zone: 'Europe/Sofia' });
return dateTime.toFormat('HH:mm');
// new date FNs
// Utility to handle date parsing consistently
const parseDate = (input) => {
return (typeof input === 'string' || input instanceof Date)
? DateTime.fromJSDate(new Date(input), { zone: 'Europe/Sofia' })
: DateTime.now({ zone: 'Europe/Sofia' });
};
exports.parseDate = parseDate;
// Set timezone to 'Europe/Sofia' without translating time
exports.setTimezone = (input) => {
let dateTime = parseDate(input);
dateTime = dateTime.setZone('Europe/Sofia', { keepLocalTime: true });
return dateTime.toJSDate();
};
// 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
const date = (typeof input === 'string') ? new Date(input) : input;
// Format date to a specified format, defaulting to 'HH:mm'
exports.getTimeFormatted = (input, format = 'HH:mm') => {
const dateTime = parseDate(input);
return dateTime.toFormat(format);
};
return date.toLocaleTimeString('en-US', {
hour12: false,
hour: '2-digit',
minute: '2-digit',
timeZone: 'Europe/Sofia'
}).substring(0, 5);
}
// Set time in 'HH:mm' format to a date and return as JS Date in Sofia timezone
exports.setTimeHHmm = (input, timeString) => {
let dateTime = parseDate(input);
const [hour, minute] = timeString.split(':').map(Number);
dateTime = dateTime.set({ hour, minute, second: 0, millisecond: 0 });
return dateTime.toJSDate();
};
//parse 'HH:mm' time string to date object
// Parse 'HH:mm' time string to a JS Date object in Sofia timezone for today
exports.parseTimeHHmm = (timeString) => {
// If timeString is already a date, return it as is
if (timeString instanceof Date) {
return timeString;
}
const [hours, minutes] = timeString.split(':');
const date = new Date();
date.setHours(hours);
date.setMinutes(minutes);
return date;
}
exports.setTimeHHmm = (date, timeStringOrHours) => {
const newDate = new Date(date);
if (typeof timeStringOrHours === 'string' && timeStringOrHours.includes(':')) {
// If hours is a string in "HH:mm" format
const [h, m] = timeStringOrHours.split(':');
newDate.setHours(parseInt(h, 10), parseInt(m, 10), 0, 0);
} else {
// If hours and minutes are provided separately
newDate.setHours(parseInt(timeStringOrHours, 10), 0, 0, 0);
}
return newDate;
const dateTime = DateTime.now({ zone: 'Europe/Sofia' });
const [hour, minute] = timeString.split(':').map(Number);
return dateTime.set({ hour, minute, second: 0, millisecond: 0 }).toJSDate();
};
// ToDo: update all uses of this function to use the new one
// exports.getTimeFormatted = function (date) {
// const dateTime = DateTime.fromJSDate(date, { zone: 'Europe/Sofia' });
// return dateTime.toFormat('HH:mm');
// };
// exports.setTimeHHmm = (date, timeStringOrHours) => {
// const newDate = new Date(date);
// if (typeof timeStringOrHours === 'string' && timeStringOrHours.includes(':')) {
// // If hours is a string in "HH:mm" format
// const [h, m] = timeStringOrHours.split(':');
// newDate.setHours(parseInt(h, 10), parseInt(m, 10), 0, 0);
// } else {
// // If hours and minutes are provided separately
// newDate.setHours(parseInt(timeStringOrHours, 10), 0, 0, 0);
// }
// return newDate;
// };
// // 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
// const date = (typeof input === 'string') ? new Date(input) : input;
// return date.toLocaleTimeString('en-US', {
// hour12: false,
// hour: '2-digit',
// minute: '2-digit',
// timeZone: 'Europe/Sofia'
// }).substring(0, 5);
// }
// //parse 'HH:mm' time string to date object
// exports.parseTimeHHmm = (timeString) => {
// // If timeString is already a date, return it as is
// if (timeString instanceof Date) {
// return timeString;
// }
// const [hours, minutes] = timeString.split(':');
// const date = new Date();
// date.setHours(hours);
// date.setMinutes(minutes);
// return date;
// }
exports.getTimeInMinutes = (dateOrTimestamp) => {
const date = new Date(dateOrTimestamp);
logger.debug("getTimeInMinutes: date = ", date);
@ -782,8 +818,13 @@ exports.convertDatesToISOStrings = function (obj) {
return obj;
}
// if (obj instanceof Date) {
// return obj.toISOString();
// }
if (obj instanceof Date) {
return obj.toISOString();
// Convert the Date object to a Luxon DateTime in UTC
const utcDate = DateTime.fromJSDate(obj, { zone: 'utc' });
return utcDate.toISO(); // Output in UTC as ISO string
}
if (Array.isArray(obj)) {
@ -800,7 +841,29 @@ exports.convertDatesToISOStrings = function (obj) {
return obj;
}
function adjustDateForDST(date, timezone) {
// Convert the date to the specified timezone
let dateTime = DateTime.fromJSDate(date, { zone: timezone });
// Check if the original date is in DST
const isOriginalDST = dateTime.isInDST;
// Check if the current date in the same timezone is in DST
const isNowDST = DateTime.now().setZone(timezone).isInDST;
// Compare and adjust if necessary
if (isOriginalDST && !isNowDST) {
// If original date was in DST but now is not, subtract one hour
dateTime = dateTime.minus({ hours: 1 });
} else if (!isOriginalDST && isNowDST) {
// If original date was not in DST but now is, add one hour
dateTime = dateTime.plus({ hours: 1 });
}
// Return the adjusted date as a JavaScript Date
return dateTime.toJSDate();
}
exports.adjustDateForDST = adjustDateForDST;
// exports.getInitials = function (names) {
// const parts = names.split(' '); // Split the full name into parts