Skip to content

Commit

Permalink
feat(new tool): Timezone Converter
Browse files Browse the repository at this point in the history
Fix #429
  • Loading branch information
sharevb committed Sep 7, 2024
1 parent 80e46c9 commit fcb8ab2
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 8 deletions.
4 changes: 4 additions & 0 deletions components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ declare module '@vue/runtime-core' {
NH3: typeof import('naive-ui')['NH3']
NIcon: typeof import('naive-ui')['NIcon']
NImage: typeof import('naive-ui')['NImage']
NInput: typeof import('naive-ui')['NInput']
NInputGroup: typeof import('naive-ui')['NInputGroup']
NInputGroupLabel: typeof import('naive-ui')['NInputGroupLabel']
NInputNumber: typeof import('naive-ui')['NInputNumber']
Expand All @@ -165,7 +166,9 @@ declare module '@vue/runtime-core' {
NMenu: typeof import('naive-ui')['NMenu']
NProgress: typeof import('naive-ui')['NProgress']
NScrollbar: typeof import('naive-ui')['NScrollbar']
NSelect: typeof import('naive-ui')['NSelect']
NSlider: typeof import('naive-ui')['NSlider']
NSpace: typeof import('naive-ui')['NSpace']
NStatistic: typeof import('naive-ui')['NStatistic']
NSwitch: typeof import('naive-ui')['NSwitch']
NTable: typeof import('naive-ui')['NTable']
Expand Down Expand Up @@ -197,6 +200,7 @@ declare module '@vue/runtime-core' {
TextStatistics: typeof import('./src/tools/text-statistics/text-statistics.vue')['default']
TextToBinary: typeof import('./src/tools/text-to-binary/text-to-binary.vue')['default']
TextToNatoAlphabet: typeof import('./src/tools/text-to-nato-alphabet/text-to-nato-alphabet.vue')['default']
TimezoneConverter: typeof import('./src/tools/timezone-converter/timezone-converter.vue')['default']
TokenDisplay: typeof import('./src/tools/otp-code-generator-and-validator/token-display.vue')['default']
'TokenGenerator.tool': typeof import('./src/tools/token-generator/token-generator.tool.vue')['default']
TomlToJson: typeof import('./src/tools/toml-to-json/toml-to-json.vue')['default']
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"change-case": "^4.1.2",
"colord": "^2.9.3",
"composerize-ts": "^0.6.2",
"countries-and-timezones": "^3.6.0",
"country-code-lookup": "^0.1.0",
"cron-validator": "^1.3.1",
"cronstrue": "^2.26.0",
Expand All @@ -59,6 +60,7 @@
"emojilib": "^3.0.10",
"figue": "^1.2.0",
"fuse.js": "^6.6.2",
"get-timezone-offset": "^1.0.5",
"highlight.js": "^11.7.0",
"iarna-toml-esm": "^3.0.5",
"ibantools": "^4.3.3",
Expand Down
33 changes: 25 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/tools/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { tool as base64FileConverter } from './base64-file-converter';
import { tool as base64StringConverter } from './base64-string-converter';
import { tool as basicAuthGenerator } from './basic-auth-generator';
import { tool as timezoneConverter } from './timezone-converter';
import { tool as pdfSignatureChecker } from './pdf-signature-checker';
import { tool as numeronymGenerator } from './numeronym-generator';
import { tool as macAddressGenerator } from './mac-address-generator';
Expand Down Expand Up @@ -85,6 +86,7 @@ export const toolsByCategory: ToolCategory[] = [
name: 'Converter',
components: [
dateTimeConverter,
timezoneConverter,
baseConverter,
romanNumeralConverter,
base64StringConverter,
Expand Down
3 changes: 3 additions & 0 deletions src/tools/timezone-converter/get-timezone-offset.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare module "get-timezone-offset" {
export default function(timeZoneName: string, date: Date);
}
12 changes: 12 additions & 0 deletions src/tools/timezone-converter/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { CalendarTime } from '@vicons/tabler';
import { defineTool } from '../tool';

export const tool = defineTool({
name: 'Timezone Converter',
path: '/timezone-converter',
description: 'Convert Date-Time from a timezone to others and get timezone vs countries infos',
keywords: ['timezone', 'tz', 'date', 'time', 'country', 'converter'],
component: () => import('./timezone-converter.vue'),
icon: CalendarTime,
createdAt: new Date('2024-08-15'),
});
129 changes: 129 additions & 0 deletions src/tools/timezone-converter/timezone-converter.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<script setup lang="ts">
import ctz from 'countries-and-timezones';
import getTimezoneOffset from 'get-timezone-offset';
const browserTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
const allTimezones = Object.values(ctz.getAllTimezones()).map(tz => ({
value: tz.name,
label: `${tz.name === browserTimezone ? 'Browser TZ - ' : ''}${tz.name} (${tz.utcOffset === tz.dstOffset ? tz.utcOffsetStr : `${tz.utcOffsetStr}/${tz.dstOffsetStr}`})`,
}));
function convertMinsToHrsMins(minutes: number) {
const h = String(Math.floor(minutes / 60)).padStart(2, '0');
const m = String(minutes % 60).padStart(2, '0');
return `${h}:${m}`;
}
const otherTimezones = useStorage<{ name: string }[]>('timezone-conv:zones', [{ name: 'Etc/GMT' }]);
const currentTimezone = useStorage<string>('timezone-conv:current', browserTimezone);
const now = Date.now();
const currentDatetimeRange = ref<[number, number]>([now, now]);
const currentTimezoneOffset = computed(() => {
return convertMinsToHrsMins(-getTimezoneOffset(currentTimezone.value, new Date(currentDatetimeRange.value[0])));
});
function convertToTimezone(tz: string, timestamp: number) {
return new Date(
timestamp
+ getTimezoneOffset(currentTimezone.value, new Date()) * 60 * 1000
- getTimezoneOffset(browserTimezone, new Date()) * 60 * 1000,
).toLocaleString(undefined,
{ timeZone: tz, timeZoneName: undefined, hour12: false });
}
const tzToCountriesInput = ref(browserTimezone);
const tzToCountriesOutput = computed(() => ctz.getCountriesForTimezone(tzToCountriesInput.value));
const allCountries = Object.values(ctz.getAllCountries()).map(c => ({
value: c.id,
label: `${c.name} (${c.id})`,
}));
const countryToTimezonesInput = ref('FR');
const countryToTimezonesOutput = computed(() => ctz.getTimezonesForCountry(countryToTimezonesInput.value));
</script>

<template>
<div>
<c-card title="Timezones Date-Time Converter" mb-2>
<c-select
v-model:value="currentTimezone"
label="Timezone"
label-position="left"
searchable
:options="allTimezones"
mb-2
/>
<n-date-picker
v-model:value="currentDatetimeRange"
type="datetimerange"
mb-2
/>

<input-copyable
label="Current Timezone Offset (min)"
label-position="left"
:value="currentTimezoneOffset"
mb-2
/>

<c-card title="Date-Time in other timezones">
<n-dynamic-input
v-model:value="otherTimezones"
show-sort-button
:on-create="() => ({ name: browserTimezone })"
>
<template #default="{ value }">
<div flex flex-wrap items-center gap-1>
<n-select
v-model:value="value.name"
filterable
placeholder="Please select a timezone"
:options="allTimezones"
w-full
/>
<div w-full flex items-baseline gap-1>
<n-input style="min-width: 49%" readonly :value="convertToTimezone(value.name, currentDatetimeRange[0])" />
<n-input style="min-width: 49%" readonly :value="convertToTimezone(value.name, currentDatetimeRange[1])" />
</div>
</div>
</template>
</n-dynamic-input>
</c-card>
</c-card>

<c-card title="Country to Timezones" mb-2>
<c-select
label="Country"
label-position="left"
:value="countryToTimezonesInput"
searchable
:options="allCountries"
/>

<n-divider />

<ul>
<li v-for="(tz, ix) in countryToTimezonesOutput" :key="ix">
{{ tz.name }} ({{ tz.countries.join(', ') }}): UTC= {{ tz.utcOffsetStr }}, DST= {{ tz.dstOffsetStr }}
</li>
</ul>
</c-card>

<c-card title="Timezones to Countries" mb-2>
<c-select
label="Timezone"
label-position="left"
:value="tzToCountriesInput"
searchable
:options="allTimezones"
/>

<n-divider />

<ul>
<li v-for="(country, ix) in tzToCountriesOutput" :key="ix">
{{ country.name }} ({{ country.id }}): {{ country.timezones.join(', ') }}
</li>
</ul>
</c-card>
</div>
</template>

0 comments on commit fcb8ab2

Please sign in to comment.