77 lines
2.9 KiB
JavaScript
77 lines
2.9 KiB
JavaScript
const TimezoneConfig = {
|
|
timezone: localStorage.getItem('timezone') || 'Europe/Warsaw',
|
|
|
|
availableTimezones: [
|
|
{ value: 'UTC', label: 'UTC', offset: 0 },
|
|
{ value: 'Europe/London', label: 'London (GMT/BST)', offset: 0 },
|
|
{ value: 'Europe/Paris', label: 'Central Europe (CET/CEST)', offset: 1 },
|
|
{ value: 'Europe/Warsaw', label: 'Warsaw (CET/CEST)', offset: 1 },
|
|
{ value: 'America/New_York', label: 'New York (EST/EDT)', offset: -5 },
|
|
{ value: 'America/Chicago', label: 'Chicago (CST/CDT)', offset: -6 },
|
|
{ value: 'America/Los_Angeles', label: 'Los Angeles (PST/PDT)', offset: -8 },
|
|
{ value: 'Asia/Tokyo', label: 'Tokyo (JST)', offset: 9 },
|
|
{ value: 'Asia/Shanghai', label: 'Shanghai (CST)', offset: 8 },
|
|
{ value: 'Australia/Sydney', label: 'Sydney (AEST/AEDT)', offset: 10 },
|
|
],
|
|
|
|
setTimezone(tz) {
|
|
this.timezone = tz;
|
|
localStorage.setItem('timezone', tz);
|
|
document.dispatchEvent(new CustomEvent('timezone-changed', { detail: tz }));
|
|
},
|
|
|
|
getTimezone() {
|
|
return this.timezone;
|
|
},
|
|
|
|
getOffsetHours(tz = this.timezone) {
|
|
const now = new Date();
|
|
const tzDate = new Date(now.toLocaleString('en-US', { timeZone: tz }));
|
|
const utcDate = new Date(now.toLocaleString('en-US', { timeZone: 'UTC' }));
|
|
return (tzDate - utcDate) / 3600000;
|
|
},
|
|
|
|
formatDate(timestamp) {
|
|
const date = new Date(timestamp);
|
|
const tz = this.timezone;
|
|
|
|
const options = {
|
|
timeZone: tz,
|
|
year: 'numeric', month: '2-digit', day: '2-digit',
|
|
hour: '2-digit', minute: '2-digit', second: '2-digit',
|
|
hour12: false
|
|
};
|
|
|
|
const formatter = new Intl.DateTimeFormat('en-GB', options);
|
|
const parts = formatter.formatToParts(date);
|
|
const get = (type) => parts.find(p => p.type === type).value;
|
|
|
|
return `${get('day')}/${get('month')}/${get('year')} ${get('hour')}:${get('minute')}`;
|
|
},
|
|
|
|
formatTickMark(timestamp) {
|
|
const date = new Date(timestamp * 1000);
|
|
const tz = this.timezone;
|
|
|
|
const options = {
|
|
timeZone: tz,
|
|
year: 'numeric', month: '2-digit', day: '2-digit',
|
|
hour: '2-digit', minute: '2-digit',
|
|
hour12: false
|
|
};
|
|
|
|
const formatter = new Intl.DateTimeFormat('en-GB', options);
|
|
const parts = formatter.formatToParts(date);
|
|
const get = (type) => parts.find(p => p.type === type).value;
|
|
|
|
// If it's exactly midnight, just show the date, otherwise show time too
|
|
const isMidnight = get('hour') === '00' && get('minute') === '00';
|
|
if (isMidnight) {
|
|
return `${get('day')}/${get('month')}/${get('year')}`;
|
|
}
|
|
return `${get('day')}/${get('month')} ${get('hour')}:${get('minute')}`;
|
|
}
|
|
};
|
|
|
|
export { TimezoneConfig };
|