2019-04-03 17:29:03 +02:00
|
|
|
<template>
|
2021-10-10 16:25:50 +02:00
|
|
|
<p v-if="!endsOn">
|
|
|
|
<span>{{
|
|
|
|
formatDateTimeString(beginsOn, timezoneToShow, showStartTime)
|
|
|
|
}}</span>
|
|
|
|
<br />
|
2022-07-12 10:55:28 +02:00
|
|
|
<o-switch
|
|
|
|
size="small"
|
2021-10-10 16:25:50 +02:00
|
|
|
v-model="showLocalTimezone"
|
|
|
|
v-if="differentFromUserTimezone"
|
|
|
|
>
|
|
|
|
{{ singleTimeZone }}
|
2022-07-12 10:55:28 +02:00
|
|
|
</o-switch>
|
2021-10-10 16:25:50 +02:00
|
|
|
</p>
|
|
|
|
<p v-else-if="isSameDay() && showStartTime && showEndTime">
|
|
|
|
<span>{{
|
2020-02-18 08:57:00 +01:00
|
|
|
$t("On {date} from {startTime} to {endTime}", {
|
|
|
|
date: formatDate(beginsOn),
|
2021-10-10 16:25:50 +02:00
|
|
|
startTime: formatTime(beginsOn, timezoneToShow),
|
|
|
|
endTime: formatTime(endsOn, timezoneToShow),
|
2020-02-18 08:57:00 +01:00
|
|
|
})
|
2021-10-10 16:25:50 +02:00
|
|
|
}}</span>
|
|
|
|
<br />
|
2022-07-12 10:55:28 +02:00
|
|
|
<o-switch
|
|
|
|
size="small"
|
2021-10-10 16:25:50 +02:00
|
|
|
v-model="showLocalTimezone"
|
|
|
|
v-if="differentFromUserTimezone"
|
|
|
|
>
|
|
|
|
{{ singleTimeZone }}
|
2022-07-12 10:55:28 +02:00
|
|
|
</o-switch>
|
2021-10-10 16:25:50 +02:00
|
|
|
</p>
|
|
|
|
<p v-else-if="isSameDay() && showStartTime && !showEndTime">
|
2020-02-18 08:57:00 +01:00
|
|
|
{{
|
|
|
|
$t("On {date} starting at {startTime}", {
|
|
|
|
date: formatDate(beginsOn),
|
|
|
|
startTime: formatTime(beginsOn),
|
|
|
|
})
|
|
|
|
}}
|
2021-10-10 16:25:50 +02:00
|
|
|
</p>
|
|
|
|
<p v-else-if="isSameDay()">
|
|
|
|
{{ $t("On {date}", { date: formatDate(beginsOn) }) }}
|
|
|
|
</p>
|
|
|
|
<p v-else-if="endsOn && showStartTime && showEndTime">
|
|
|
|
<span>
|
|
|
|
{{
|
|
|
|
$t(
|
|
|
|
"From the {startDate} at {startTime} to the {endDate} at {endTime}",
|
|
|
|
{
|
|
|
|
startDate: formatDate(beginsOn),
|
|
|
|
startTime: formatTime(beginsOn, timezoneToShow),
|
|
|
|
endDate: formatDate(endsOn),
|
|
|
|
endTime: formatTime(endsOn, timezoneToShow),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}}
|
|
|
|
</span>
|
|
|
|
<br />
|
2022-07-12 10:55:28 +02:00
|
|
|
<o-switch
|
|
|
|
size="small"
|
2021-10-10 16:25:50 +02:00
|
|
|
v-model="showLocalTimezone"
|
|
|
|
v-if="differentFromUserTimezone"
|
|
|
|
>
|
|
|
|
{{ multipleTimeZones }}
|
2022-07-12 10:55:28 +02:00
|
|
|
</o-switch>
|
2021-10-10 16:25:50 +02:00
|
|
|
</p>
|
|
|
|
<p v-else-if="endsOn && showStartTime">
|
|
|
|
<span>
|
|
|
|
{{
|
|
|
|
$t("From the {startDate} at {startTime} to the {endDate}", {
|
|
|
|
startDate: formatDate(beginsOn),
|
|
|
|
startTime: formatTime(beginsOn, timezoneToShow),
|
|
|
|
endDate: formatDate(endsOn),
|
|
|
|
})
|
|
|
|
}}
|
|
|
|
</span>
|
|
|
|
<br />
|
2022-07-12 10:55:28 +02:00
|
|
|
<o-switch
|
|
|
|
size="small"
|
2021-10-10 16:25:50 +02:00
|
|
|
v-model="showLocalTimezone"
|
|
|
|
v-if="differentFromUserTimezone"
|
|
|
|
>
|
|
|
|
{{ singleTimeZone }}
|
2022-07-12 10:55:28 +02:00
|
|
|
</o-switch>
|
2021-10-10 16:25:50 +02:00
|
|
|
</p>
|
|
|
|
<p v-else-if="endsOn">
|
2020-02-18 08:57:00 +01:00
|
|
|
{{
|
2022-07-12 10:55:28 +02:00
|
|
|
t("From the {startDate} to the {endDate}", {
|
2020-02-18 08:57:00 +01:00
|
|
|
startDate: formatDate(beginsOn),
|
|
|
|
endDate: formatDate(endsOn),
|
|
|
|
})
|
|
|
|
}}
|
2021-10-10 16:25:50 +02:00
|
|
|
</p>
|
2019-04-03 17:29:03 +02:00
|
|
|
</template>
|
2022-07-12 10:55:28 +02:00
|
|
|
<script lang="ts" setup>
|
|
|
|
import {
|
|
|
|
formatDateString,
|
|
|
|
formatDateTimeString,
|
|
|
|
formatTimeString,
|
|
|
|
} from "@/filters/datetime";
|
2021-11-10 20:39:44 +01:00
|
|
|
import { getTimezoneOffset } from "date-fns-tz";
|
2022-07-12 10:55:28 +02:00
|
|
|
import { computed, ref } from "vue";
|
|
|
|
import { useI18n } from "vue-i18n";
|
|
|
|
|
|
|
|
const props = withDefaults(
|
|
|
|
defineProps<{
|
|
|
|
beginsOn: string;
|
|
|
|
endsOn?: string;
|
|
|
|
showStartTime?: boolean;
|
|
|
|
showEndTime?: boolean;
|
|
|
|
timezone?: string;
|
|
|
|
userTimezone?: string;
|
|
|
|
}>(),
|
|
|
|
{
|
|
|
|
showStartTime: true,
|
|
|
|
showEndTime: true,
|
2021-10-10 16:25:50 +02:00
|
|
|
}
|
2022-07-12 10:55:28 +02:00
|
|
|
);
|
2021-10-10 16:25:50 +02:00
|
|
|
|
2022-07-12 10:55:28 +02:00
|
|
|
const { t } = useI18n({ useScope: "global" });
|
2019-04-03 17:29:03 +02:00
|
|
|
|
2022-07-12 10:55:28 +02:00
|
|
|
const showLocalTimezone = ref(true);
|
2021-10-10 16:25:50 +02:00
|
|
|
|
2022-07-12 10:55:28 +02:00
|
|
|
const timezoneToShow = computed((): string | undefined => {
|
|
|
|
if (showLocalTimezone.value) {
|
|
|
|
return props.timezone;
|
2019-04-03 17:29:03 +02:00
|
|
|
}
|
2022-07-12 10:55:28 +02:00
|
|
|
return userActualTimezone.value;
|
|
|
|
});
|
2019-04-03 17:29:03 +02:00
|
|
|
|
2022-07-12 10:55:28 +02:00
|
|
|
const userActualTimezone = computed((): string => {
|
|
|
|
if (props.userTimezone) {
|
|
|
|
return props.userTimezone;
|
2019-04-03 17:29:03 +02:00
|
|
|
}
|
2022-07-12 10:55:28 +02:00
|
|
|
return Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
|
|
});
|
|
|
|
|
|
|
|
const formatDate = (value: string): string | undefined => {
|
|
|
|
return formatDateString(value);
|
|
|
|
};
|
|
|
|
|
|
|
|
const formatTime = (
|
|
|
|
value: string,
|
|
|
|
timezone: string | undefined = undefined
|
|
|
|
): string | undefined => {
|
|
|
|
return formatTimeString(value, timezone ?? "Etc/UTC");
|
|
|
|
};
|
|
|
|
|
|
|
|
const isSameDay = (): boolean => {
|
|
|
|
if (!props.endsOn) return false;
|
|
|
|
return (
|
|
|
|
beginsOnDate.value.toDateString() === new Date(props.endsOn).toDateString()
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const beginsOnDate = computed((): Date => {
|
|
|
|
return new Date(props.beginsOn);
|
|
|
|
});
|
|
|
|
|
|
|
|
const differentFromUserTimezone = computed((): boolean => {
|
|
|
|
return (
|
|
|
|
!!props.timezone &&
|
|
|
|
!!userActualTimezone.value &&
|
|
|
|
getTimezoneOffset(props.timezone, beginsOnDate.value) !==
|
|
|
|
getTimezoneOffset(userActualTimezone.value, beginsOnDate.value) &&
|
|
|
|
props.timezone !== userActualTimezone.value
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
const singleTimeZone = computed((): string => {
|
|
|
|
if (showLocalTimezone.value) {
|
|
|
|
return t("Local time ({timezone})", {
|
|
|
|
timezone: timezoneToShow,
|
2021-10-10 16:25:50 +02:00
|
|
|
}) as string;
|
|
|
|
}
|
2022-07-12 10:55:28 +02:00
|
|
|
return t("Time in your timezone ({timezone})", {
|
|
|
|
timezone: timezoneToShow,
|
|
|
|
}) as string;
|
|
|
|
});
|
|
|
|
|
|
|
|
const multipleTimeZones = computed((): string => {
|
|
|
|
if (showLocalTimezone.value) {
|
|
|
|
return t("Local time ({timezone})", {
|
|
|
|
timezone: timezoneToShow,
|
2021-10-10 16:25:50 +02:00
|
|
|
}) as string;
|
|
|
|
}
|
2022-07-12 10:55:28 +02:00
|
|
|
return t("Times in your timezone ({timezone})", {
|
|
|
|
timezone: timezoneToShow,
|
|
|
|
}) as string;
|
|
|
|
});
|
2019-04-03 17:29:03 +02:00
|
|
|
</script>
|