mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-27 02:14:54 +00:00
finished schedule module
This commit is contained in:
23
libs/common/src/helper/convertTimestampToDubaiTime.ts
Normal file
23
libs/common/src/helper/convertTimestampToDubaiTime.ts
Normal file
@ -0,0 +1,23 @@
|
||||
export function convertTimestampToDubaiTime(timestamp) {
|
||||
// Convert timestamp to milliseconds
|
||||
const date = new Date(timestamp * 1000);
|
||||
|
||||
// Convert to Dubai time (UTC+4)
|
||||
const dubaiTimeOffset = 4 * 60; // 4 hours in minutes
|
||||
const dubaiTime = new Date(date.getTime() + dubaiTimeOffset * 60 * 1000);
|
||||
|
||||
// Format the date as YYYYMMDD
|
||||
const year = dubaiTime.getUTCFullYear();
|
||||
const month = String(dubaiTime.getUTCMonth() + 1).padStart(2, '0'); // Months are zero-based
|
||||
const day = String(dubaiTime.getUTCDate()).padStart(2, '0');
|
||||
|
||||
// Format the time as HH:MM (24-hour format)
|
||||
const hours = String(dubaiTime.getUTCHours()).padStart(2, '0');
|
||||
const minutes = String(dubaiTime.getUTCMinutes()).padStart(2, '0');
|
||||
|
||||
// Return formatted date and time
|
||||
return {
|
||||
date: `${year}${month}${day}`,
|
||||
time: `${hours}:${minutes}`,
|
||||
};
|
||||
}
|
||||
27
libs/common/src/helper/getScheduleStatus.ts
Normal file
27
libs/common/src/helper/getScheduleStatus.ts
Normal file
@ -0,0 +1,27 @@
|
||||
export function getScheduleStatus(daysEnabled: string[]): string {
|
||||
const daysMap: string[] = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
|
||||
|
||||
const schedule: string[] = Array(7).fill('0');
|
||||
|
||||
daysEnabled.forEach((day) => {
|
||||
const index: number = daysMap.indexOf(day);
|
||||
if (index !== -1) {
|
||||
schedule[index] = '1';
|
||||
}
|
||||
});
|
||||
|
||||
return schedule.join('');
|
||||
}
|
||||
export function getEnabledDays(schedule: string): string[] {
|
||||
const daysMap: string[] = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
|
||||
const enabledDays: string[] = [];
|
||||
|
||||
// Iterate through the schedule string
|
||||
for (let i = 0; i < schedule.length; i++) {
|
||||
if (schedule[i] === '1') {
|
||||
enabledDays.push(daysMap[i]);
|
||||
}
|
||||
}
|
||||
|
||||
return enabledDays;
|
||||
}
|
||||
Reference in New Issue
Block a user