mirror of
https://github.com/MetaFam/TheGame.git
synced 2026-01-13 08:37:53 -05:00
This is being merged for further testing so as to not block the progress of `develop` & not require more rebases.
44 lines
1.1 KiB
TypeScript
44 lines
1.1 KiB
TypeScript
import { TimeZoneOptions } from '@metafam/ds';
|
|
import { Maybe } from '@metafam/utils';
|
|
|
|
export interface TimeZoneDisplay {
|
|
timeZone?: string;
|
|
offset?: string;
|
|
}
|
|
|
|
const getOffsetLabel = (offset: number): string => {
|
|
const offsets = Math.abs(offset).toFixed(1).split('.');
|
|
const hrs = offsets[0].toString();
|
|
const mins = `0${(Number(offsets[1]) * 6).toString()}`.slice(-2);
|
|
const offsetString = `${hrs}:${mins}`;
|
|
|
|
if (offset < 0) {
|
|
return `(GMT -${offsetString})`;
|
|
}
|
|
return `(GMT +${offsetString})`;
|
|
};
|
|
|
|
export const getPlayerTimeZoneDisplay = (
|
|
zone?: Maybe<string>,
|
|
): TimeZoneDisplay => {
|
|
let tzLabel;
|
|
let offsetLabel;
|
|
const timeZone = TimeZoneOptions.find((t) => t.name === zone);
|
|
if (timeZone) {
|
|
const { abbreviation, offset, label } = timeZone;
|
|
tzLabel = label;
|
|
if (abbreviation) {
|
|
tzLabel = abbreviation;
|
|
offsetLabel = getOffsetLabel(offset);
|
|
} else if (offset === 0) {
|
|
tzLabel = 'GMT';
|
|
offsetLabel = getOffsetLabel(offset);
|
|
}
|
|
}
|
|
|
|
return {
|
|
timeZone: tzLabel,
|
|
offset: offsetLabel,
|
|
};
|
|
};
|