refactor: time between dates

This commit is contained in:
Ibrahima G. Coulibaly
2025-03-27 20:19:43 +00:00
parent dc44f44f53
commit 0576223aa5
4 changed files with 232 additions and 361 deletions

196
.idea/workspace.xml generated
View File

@@ -4,9 +4,11 @@
<option name="autoReloadType" value="SELECTIVE" /> <option name="autoReloadType" value="SELECTIVE" />
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="b30e2810-c4c1-4aad-b134-794e52cc1c7d" name="Changes" comment="chore: show new tools in landing"> <list default="true" id="b30e2810-c4c1-4aad-b134-794e52cc1c7d" name="Changes" comment="chore: zoom on hover">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/components/allTools/ToolCard.tsx" beforeDir="false" afterPath="$PROJECT_DIR$/src/components/allTools/ToolCard.tsx" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/pages/time/time-between-dates.tsx" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/pages/tools/time/time-between-dates/index.tsx" beforeDir="false" afterPath="$PROJECT_DIR$/src/pages/tools/time/time-between-dates/index.tsx" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/pages/tools/time/time-between-dates/service.ts" beforeDir="false" afterPath="$PROJECT_DIR$/src/pages/tools/time/time-between-dates/service.ts" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@@ -23,7 +25,7 @@
<option name="PUSH_AUTO_UPDATE" value="true" /> <option name="PUSH_AUTO_UPDATE" value="true" />
<option name="RECENT_BRANCH_BY_REPOSITORY"> <option name="RECENT_BRANCH_BY_REPOSITORY">
<map> <map>
<entry key="$PROJECT_DIR$" value="chesterkxng" /> <entry key="$PROJECT_DIR$" value="main" />
</map> </map>
</option> </option>
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" /> <option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
@@ -42,59 +44,66 @@
&quot;state&quot;: &quot;OPEN&quot; &quot;state&quot;: &quot;OPEN&quot;
} }
}</component> }</component>
<component name="GitHubPullRequestState">{ <component name="GitHubPullRequestState"><![CDATA[{
&quot;prStates&quot;: [ "prStates": [
{ {
&quot;id&quot;: { "id": {
&quot;id&quot;: &quot;PR_kwDOMJIfts51PkS9&quot;, "id": "PR_kwDOMJIfts51PkS9",
&quot;number&quot;: 22 "number": 22
}, },
&quot;lastSeen&quot;: 1741207144695 "lastSeen": 1741207144695
}, },
{ {
&quot;id&quot;: { "id": {
&quot;id&quot;: &quot;PR_kwDOMJIfts6NiNYl&quot;, "id": "PR_kwDOMJIfts6NiNYl",
&quot;number&quot;: 32 "number": 32
}, },
&quot;lastSeen&quot;: 1741209723869 "lastSeen": 1741209723869
}, },
{ {
&quot;id&quot;: { "id": {
&quot;id&quot;: &quot;PR_kwDOMJIfts6Nheyd&quot;, "id": "PR_kwDOMJIfts6Nheyd",
&quot;number&quot;: 31 "number": 31
}, },
&quot;lastSeen&quot;: 1741213371410 "lastSeen": 1741213371410
}, },
{ {
&quot;id&quot;: { "id": {
&quot;id&quot;: &quot;PR_kwDOMJIfts6NmRBs&quot;, "id": "PR_kwDOMJIfts6NmRBs",
&quot;number&quot;: 33 "number": 33
}, },
&quot;lastSeen&quot;: 1741282429036 "lastSeen": 1741282429036
}, },
{ {
&quot;id&quot;: { "id": {
&quot;id&quot;: &quot;PR_kwDOMJIfts5zyFTs&quot;, "id": "PR_kwDOMJIfts5zyFTs",
&quot;number&quot;: 15 "number": 15
}, },
&quot;lastSeen&quot;: 1741535540953 "lastSeen": 1741535540953
}, },
{ {
&quot;id&quot;: { "id": {
&quot;id&quot;: &quot;PR_kwDOMJIfts6QQB3c&quot;, "id": "PR_kwDOMJIfts6QQB3c",
&quot;number&quot;: 59 "number": 59
}, },
&quot;lastSeen&quot;: 1743018960900 "lastSeen": 1743018960900
}, },
{ {
&quot;id&quot;: { "id": {
&quot;id&quot;: &quot;PR_kwDOMJIfts6QMPEg&quot;, "id": "PR_kwDOMJIfts6QMPEg",
&quot;number&quot;: 58 "number": 58
}, },
&quot;lastSeen&quot;: 1743019452983 "lastSeen": 1743019452983
},
{
"id": {
"id": "PR_kwDOMJIfts6QZvRI",
"number": 61
},
"lastSeen": 1743103196866
} }
] ]
}</component> }]]></component>
<component name="GithubPullRequestsUISettings">{ <component name="GithubPullRequestsUISettings">{
&quot;selectedUrlAndAccountId&quot;: { &quot;selectedUrlAndAccountId&quot;: {
&quot;url&quot;: &quot;https://github.com/iib0011/omni-tools.git&quot;, &quot;url&quot;: &quot;https://github.com/iib0011/omni-tools.git&quot;,
@@ -123,55 +132,55 @@
<option name="hideEmptyMiddlePackages" value="true" /> <option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" /> <option name="showLibraryContents" value="true" />
</component> </component>
<component name="PropertiesComponent">{ <component name="PropertiesComponent"><![CDATA[{
&quot;keyToString&quot;: { "keyToString": {
&quot;ASKED_ADD_EXTERNAL_FILES&quot;: &quot;true&quot;, "ASKED_ADD_EXTERNAL_FILES": "true",
&quot;ASKED_SHARE_PROJECT_CONFIGURATION_FILES&quot;: &quot;true&quot;, "ASKED_SHARE_PROJECT_CONFIGURATION_FILES": "true",
&quot;Docker.Dockerfile build.executor&quot;: &quot;Run&quot;, "Docker.Dockerfile build.executor": "Run",
&quot;Docker.Dockerfile.executor&quot;: &quot;Run&quot;, "Docker.Dockerfile.executor": "Run",
&quot;Playwright.Create transparent PNG.should make png color transparent.executor&quot;: &quot;Run&quot;, "Playwright.Create transparent PNG.should make png color transparent.executor": "Run",
&quot;Playwright.JoinText Component.executor&quot;: &quot;Run&quot;, "Playwright.JoinText Component.executor": "Run",
&quot;Playwright.JoinText Component.should merge text pieces with specified join character.executor&quot;: &quot;Run&quot;, "Playwright.JoinText Component.should merge text pieces with specified join character.executor": "Run",
&quot;RunOnceActivity.OpenProjectViewOnStart&quot;: &quot;true&quot;, "RunOnceActivity.OpenProjectViewOnStart": "true",
&quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;, "RunOnceActivity.ShowReadmeOnStart": "true",
&quot;RunOnceActivity.git.unshallow&quot;: &quot;true&quot;, "RunOnceActivity.git.unshallow": "true",
&quot;Vitest.compute function (1).executor&quot;: &quot;Run&quot;, "Vitest.compute function (1).executor": "Run",
&quot;Vitest.compute function.executor&quot;: &quot;Run&quot;, "Vitest.compute function.executor": "Run",
&quot;Vitest.mergeText.executor&quot;: &quot;Run&quot;, "Vitest.mergeText.executor": "Run",
&quot;Vitest.mergeText.should merge lines and preserve blank lines when deleteBlankLines is false.executor&quot;: &quot;Run&quot;, "Vitest.mergeText.should merge lines and preserve blank lines when deleteBlankLines is false.executor": "Run",
&quot;Vitest.mergeText.should merge lines, preserve blank lines and trailing spaces when both deleteBlankLines and deleteTrailingSpaces are false.executor&quot;: &quot;Run&quot;, "Vitest.mergeText.should merge lines, preserve blank lines and trailing spaces when both deleteBlankLines and deleteTrailingSpaces are false.executor": "Run",
&quot;Vitest.parsePageRanges.executor&quot;: &quot;Run&quot;, "Vitest.parsePageRanges.executor": "Run",
&quot;Vitest.removeDuplicateLines function.executor&quot;: &quot;Run&quot;, "Vitest.removeDuplicateLines function.executor": "Run",
&quot;Vitest.removeDuplicateLines function.newlines option.executor&quot;: &quot;Run&quot;, "Vitest.removeDuplicateLines function.newlines option.executor": "Run",
&quot;Vitest.removeDuplicateLines function.newlines option.should filter newlines when newlines is set to filter.executor&quot;: &quot;Run&quot;, "Vitest.removeDuplicateLines function.newlines option.should filter newlines when newlines is set to filter.executor": "Run",
&quot;Vitest.replaceText function (regexp mode).should return the original text when passed an invalid regexp.executor&quot;: &quot;Run&quot;, "Vitest.replaceText function (regexp mode).should return the original text when passed an invalid regexp.executor": "Run",
&quot;Vitest.replaceText function.executor&quot;: &quot;Run&quot;, "Vitest.replaceText function.executor": "Run",
&quot;git-widget-placeholder&quot;: &quot;main&quot;, "git-widget-placeholder": "fork/TheLukasHenry/days-calculator-#51",
&quot;ignore.virus.scanning.warn.message&quot;: &quot;true&quot;, "ignore.virus.scanning.warn.message": "true",
&quot;kotlin-language-version-configured&quot;: &quot;true&quot;, "kotlin-language-version-configured": "true",
&quot;last_opened_file_path&quot;: &quot;C:/Users/Ibrahima/IdeaProjects/omni-tools/src/components/input&quot;, "last_opened_file_path": "C:/Users/Ibrahima/IdeaProjects/omni-tools/src/components/input",
&quot;node.js.detected.package.eslint&quot;: &quot;true&quot;, "node.js.detected.package.eslint": "true",
&quot;node.js.detected.package.tslint&quot;: &quot;true&quot;, "node.js.detected.package.tslint": "true",
&quot;node.js.selected.package.eslint&quot;: &quot;(autodetect)&quot;, "node.js.selected.package.eslint": "(autodetect)",
&quot;node.js.selected.package.tslint&quot;: &quot;(autodetect)&quot;, "node.js.selected.package.tslint": "(autodetect)",
&quot;nodejs_package_manager_path&quot;: &quot;npm&quot;, "nodejs_package_manager_path": "npm",
&quot;npm.build.executor&quot;: &quot;Run&quot;, "npm.build.executor": "Run",
&quot;npm.dev.executor&quot;: &quot;Run&quot;, "npm.dev.executor": "Run",
&quot;npm.lint.executor&quot;: &quot;Run&quot;, "npm.lint.executor": "Run",
&quot;npm.prebuild.executor&quot;: &quot;Run&quot;, "npm.prebuild.executor": "Run",
&quot;npm.script:create:tool.executor&quot;: &quot;Run&quot;, "npm.script:create:tool.executor": "Run",
&quot;npm.test.executor&quot;: &quot;Run&quot;, "npm.test.executor": "Run",
&quot;npm.test:e2e.executor&quot;: &quot;Run&quot;, "npm.test:e2e.executor": "Run",
&quot;npm.test:e2e:run.executor&quot;: &quot;Run&quot;, "npm.test:e2e:run.executor": "Run",
&quot;prettierjs.PrettierConfiguration.Package&quot;: &quot;C:\\Users\\Ibrahima\\IdeaProjects\\omni-tools\\node_modules\\prettier&quot;, "prettierjs.PrettierConfiguration.Package": "C:\\Users\\Ibrahima\\IdeaProjects\\omni-tools\\node_modules\\prettier",
&quot;project.structure.last.edited&quot;: &quot;Problems&quot;, "project.structure.last.edited": "Problems",
&quot;project.structure.proportion&quot;: &quot;0.0&quot;, "project.structure.proportion": "0.0",
&quot;project.structure.side.proportion&quot;: &quot;0.2&quot;, "project.structure.side.proportion": "0.2",
&quot;settings.editor.selected.configurable&quot;: &quot;refactai_advanced_settings&quot;, "settings.editor.selected.configurable": "refactai_advanced_settings",
&quot;ts.external.directory.path&quot;: &quot;C:\\Users\\Ibrahima\\IdeaProjects\\omni-tools\\node_modules\\typescript\\lib&quot;, "ts.external.directory.path": "C:\\Users\\Ibrahima\\IdeaProjects\\omni-tools\\node_modules\\typescript\\lib",
&quot;vue.rearranger.settings.migration&quot;: &quot;true&quot; "vue.rearranger.settings.migration": "true"
} }
}</component> }]]></component>
<component name="ReactDesignerToolWindowState"> <component name="ReactDesignerToolWindowState">
<option name="myId2Visible"> <option name="myId2Visible">
<map> <map>
@@ -363,14 +372,7 @@
<workItem from="1741971589699" duration="371000" /> <workItem from="1741971589699" duration="371000" />
<workItem from="1743018497879" duration="3895000" /> <workItem from="1743018497879" duration="3895000" />
<workItem from="1743047367993" duration="986000" /> <workItem from="1743047367993" duration="986000" />
</task> <workItem from="1743103182313" duration="3430000" />
<task id="LOCAL-00122" summary="fix: bg">
<option name="closed" value="true" />
<created>1740504100676</created>
<option name="number" value="00122" />
<option name="presentableId" value="LOCAL-00122" />
<option name="project" value="LOCAL" />
<updated>1740504100676</updated>
</task> </task>
<task id="LOCAL-00123" summary="fix: bg"> <task id="LOCAL-00123" summary="fix: bg">
<option name="closed" value="true" /> <option name="closed" value="true" />
@@ -756,7 +758,15 @@
<option name="project" value="LOCAL" /> <option name="project" value="LOCAL" />
<updated>1743051792459</updated> <updated>1743051792459</updated>
</task> </task>
<option name="localTasksCounter" value="171" /> <task id="LOCAL-00171" summary="chore: zoom on hover">
<option name="closed" value="true" />
<created>1743052111988</created>
<option name="number" value="00171" />
<option name="presentableId" value="LOCAL-00171" />
<option name="project" value="LOCAL" />
<updated>1743052111988</updated>
</task>
<option name="localTasksCounter" value="172" />
<servers /> <servers />
</component> </component>
<component name="TypeScriptGeneratedFilesManager"> <component name="TypeScriptGeneratedFilesManager">
@@ -803,7 +813,6 @@
<option name="CHECK_CODE_SMELLS_BEFORE_PROJECT_COMMIT" value="false" /> <option name="CHECK_CODE_SMELLS_BEFORE_PROJECT_COMMIT" value="false" />
<option name="CHECK_NEW_TODO" value="false" /> <option name="CHECK_NEW_TODO" value="false" />
<option name="ADD_EXTERNAL_FILES_SILENTLY" value="true" /> <option name="ADD_EXTERNAL_FILES_SILENTLY" value="true" />
<MESSAGE value="fix: replace text service" />
<MESSAGE value="chore: smooth scroll for use this tool and examles" /> <MESSAGE value="chore: smooth scroll for use this tool and examles" />
<MESSAGE value="feat: minify json" /> <MESSAGE value="feat: minify json" />
<MESSAGE value="feat: stringify json" /> <MESSAGE value="feat: stringify json" />
@@ -828,7 +837,8 @@
<MESSAGE value="chore: result file name" /> <MESSAGE value="chore: result file name" />
<MESSAGE value="chore: text result extensions" /> <MESSAGE value="chore: text result extensions" />
<MESSAGE value="chore: show new tools in landing" /> <MESSAGE value="chore: show new tools in landing" />
<option name="LAST_COMMIT_MESSAGE" value="chore: show new tools in landing" /> <MESSAGE value="chore: zoom on hover" />
<option name="LAST_COMMIT_MESSAGE" value="chore: zoom on hover" />
</component> </component>
<component name="XSLT-Support.FileAssociations.UIState"> <component name="XSLT-Support.FileAssociations.UIState">
<expand /> <expand />

View File

@@ -1,3 +0,0 @@
import TimeBetweenDates from '../tools/time/time-between-dates';
export default TimeBetweenDates;

View File

@@ -1,12 +1,13 @@
import { Box, Typography, Paper } from '@mui/material'; import { Box, Paper, Typography } from '@mui/material';
import React, { useState } from 'react'; import React, { useState } from 'react';
import ToolContent from '@components/ToolContent'; import ToolContent from '@components/ToolContent';
import TextFieldWithDesc from '@components/options/TextFieldWithDesc'; import TextFieldWithDesc from '@components/options/TextFieldWithDesc';
import SelectWithDesc from '@components/options/SelectWithDesc'; import SelectWithDesc from '@components/options/SelectWithDesc';
import { import {
calculateTimeBetweenDates, calculateTimeBetweenDates,
formatTimeDifference, formatTimeWithLargestUnit,
getTimeWithTimezone getTimeWithTimezone,
unitHierarchy
} from './service'; } from './service';
import * as Yup from 'yup'; import * as Yup from 'yup';
import { CardExampleType } from '@components/examples/ToolExamples'; import { CardExampleType } from '@components/examples/ToolExamples';
@@ -29,135 +30,6 @@ type InitialValuesType = {
endTimezone: string; endTimezone: string;
}; };
// Helper function to format time based on largest unit
const formatTimeWithLargestUnit = (
difference: any,
largestUnit: TimeUnit
): string => {
const unitHierarchy: TimeUnit[] = [
'years',
'months',
'days',
'hours',
'minutes',
'seconds',
'milliseconds'
];
const largestUnitIndex = unitHierarchy.indexOf(largestUnit);
const unitsToInclude = unitHierarchy.slice(largestUnitIndex);
// Make a deep copy of the difference object to avoid modifying the original
const convertedDifference = { ...difference };
// Constants for time conversions - more precise values
const HOURS_PER_DAY = 24;
const DAYS_PER_MONTH = 30; // Approximation
const MONTHS_PER_YEAR = 12;
const DAYS_PER_YEAR = 365; // Approximation
const MINUTES_PER_HOUR = 60;
const SECONDS_PER_MINUTE = 60;
const MS_PER_SECOND = 1000;
// Apply conversions based on the selected largest unit
if (largestUnit === 'months') {
// Convert years to months
convertedDifference.months += convertedDifference.years * MONTHS_PER_YEAR;
convertedDifference.years = 0;
} else if (largestUnit === 'days') {
// Convert years and months to days
convertedDifference.days +=
convertedDifference.years * DAYS_PER_YEAR +
convertedDifference.months * DAYS_PER_MONTH;
convertedDifference.months = 0;
convertedDifference.years = 0;
} else if (largestUnit === 'hours') {
// Convert years, months, and days to hours
convertedDifference.hours +=
convertedDifference.years * DAYS_PER_YEAR * HOURS_PER_DAY +
convertedDifference.months * DAYS_PER_MONTH * HOURS_PER_DAY +
convertedDifference.days * HOURS_PER_DAY;
convertedDifference.days = 0;
convertedDifference.months = 0;
convertedDifference.years = 0;
} else if (largestUnit === 'minutes') {
// Convert years, months, days, and hours to minutes
convertedDifference.minutes +=
convertedDifference.years *
DAYS_PER_YEAR *
HOURS_PER_DAY *
MINUTES_PER_HOUR +
convertedDifference.months *
DAYS_PER_MONTH *
HOURS_PER_DAY *
MINUTES_PER_HOUR +
convertedDifference.days * HOURS_PER_DAY * MINUTES_PER_HOUR +
convertedDifference.hours * MINUTES_PER_HOUR;
convertedDifference.hours = 0;
convertedDifference.days = 0;
convertedDifference.months = 0;
convertedDifference.years = 0;
} else if (largestUnit === 'seconds') {
// Convert years, months, days, hours, and minutes to seconds
convertedDifference.seconds +=
convertedDifference.years *
DAYS_PER_YEAR *
HOURS_PER_DAY *
MINUTES_PER_HOUR *
SECONDS_PER_MINUTE +
convertedDifference.months *
DAYS_PER_MONTH *
HOURS_PER_DAY *
MINUTES_PER_HOUR *
SECONDS_PER_MINUTE +
convertedDifference.days *
HOURS_PER_DAY *
MINUTES_PER_HOUR *
SECONDS_PER_MINUTE +
convertedDifference.hours * MINUTES_PER_HOUR * SECONDS_PER_MINUTE +
convertedDifference.minutes * SECONDS_PER_MINUTE;
convertedDifference.minutes = 0;
convertedDifference.hours = 0;
convertedDifference.days = 0;
convertedDifference.months = 0;
convertedDifference.years = 0;
} else if (largestUnit === 'milliseconds') {
// Convert everything to milliseconds
convertedDifference.milliseconds +=
convertedDifference.years *
DAYS_PER_YEAR *
HOURS_PER_DAY *
MINUTES_PER_HOUR *
SECONDS_PER_MINUTE *
MS_PER_SECOND +
convertedDifference.months *
DAYS_PER_MONTH *
HOURS_PER_DAY *
MINUTES_PER_HOUR *
SECONDS_PER_MINUTE *
MS_PER_SECOND +
convertedDifference.days *
HOURS_PER_DAY *
MINUTES_PER_HOUR *
SECONDS_PER_MINUTE *
MS_PER_SECOND +
convertedDifference.hours *
MINUTES_PER_HOUR *
SECONDS_PER_MINUTE *
MS_PER_SECOND +
convertedDifference.minutes * SECONDS_PER_MINUTE * MS_PER_SECOND +
convertedDifference.seconds * MS_PER_SECOND;
convertedDifference.seconds = 0;
convertedDifference.minutes = 0;
convertedDifference.hours = 0;
convertedDifference.days = 0;
convertedDifference.months = 0;
convertedDifference.years = 0;
}
return formatTimeDifference(convertedDifference, unitsToInclude);
};
const initialValues: InitialValuesType = { const initialValues: InitialValuesType = {
startDate: new Date().toISOString().split('T')[0], startDate: new Date().toISOString().split('T')[0],
startTime: '00:00', startTime: '00:00',
@@ -178,17 +50,26 @@ const validationSchema = Yup.object({
const timezoneOptions = [ const timezoneOptions = [
{ value: 'local', label: 'Local Time' }, { value: 'local', label: 'Local Time' },
{ value: 'GMT+0000', label: 'GMT+0000 (UTC)' }, ...Intl.supportedValuesOf('timeZone')
{ value: 'GMT-0500', label: 'GMT-0500 (Eastern Standard Time)' }, .map((tz) => {
{ value: 'GMT-0600', label: 'GMT-0600 (Central Standard Time)' }, const formatter = new Intl.DateTimeFormat('en', {
{ value: 'GMT-0700', label: 'GMT-0700 (Mountain Standard Time)' }, timeZone: tz,
{ value: 'GMT-0800', label: 'GMT-0800 (Pacific Standard Time)' }, timeZoneName: 'shortOffset'
{ value: 'GMT+0100', label: 'GMT+0100 (Central European Time)' }, });
{ value: 'GMT+0200', label: 'GMT+0200 (Eastern European Time)' },
{ value: 'GMT+0530', label: 'GMT+0530 (Indian Standard Time)' }, const offset =
{ value: 'GMT+0800', label: 'GMT+0800 (China Standard Time)' }, formatter
{ value: 'GMT+0900', label: 'GMT+0900 (Japan Standard Time)' }, .formatToParts(new Date())
{ value: 'GMT+1000', label: 'GMT+1000 (Australian Eastern Standard Time)' } .find((part) => part.type === 'timeZoneName')?.value || '';
return {
value: offset.replace('UTC', 'GMT'),
label: `${offset.replace('UTC', 'GMT')} (${tz})`
};
})
.sort((a, b) =>
a.value.localeCompare(b.value, undefined, { numeric: true })
)
]; ];
const exampleCards: CardExampleType<InitialValuesType>[] = [ const exampleCards: CardExampleType<InitialValuesType>[] = [
@@ -213,8 +94,8 @@ const exampleCards: CardExampleType<InitialValuesType>[] = [
startTime: '12:00', startTime: '12:00',
endDate: '2023-01-01', endDate: '2023-01-01',
endTime: '12:00', endTime: '12:00',
startTimezone: 'GMT-0500', startTimezone: 'GMT-5',
endTimezone: 'GMT+0000' endTimezone: 'GMT'
}, },
sampleResult: '5 hours' sampleResult: '5 hours'
}, },
@@ -325,7 +206,6 @@ export default function TimeBetweenDates() {
]} ]}
compute={(values) => { compute={(values) => {
try { try {
// Create Date objects with timezone consideration
const startDateTime = getTimeWithTimezone( const startDateTime = getTimeWithTimezone(
values.startDate, values.startDate,
values.startTime, values.startTime,
@@ -344,36 +224,17 @@ export default function TimeBetweenDates() {
endDateTime endDateTime
); );
// Format the result - use 'years' as the default largest unit // Auto-determine the best unit to display based on the time difference
if (typeof difference === 'number') { const bestUnit: TimeUnit =
setResult(`${difference} milliseconds`); unitHierarchy.find((unit) => difference[unit] > 0) ||
} else { 'milliseconds';
// Auto-determine the best unit to display based on the time difference
let bestUnit: TimeUnit = 'years';
if (difference.years > 0) { const formattedDifference = formatTimeWithLargestUnit(
bestUnit = 'years'; difference,
} else if (difference.months > 0) { bestUnit
bestUnit = 'months'; );
} else if (difference.days > 0) {
bestUnit = 'days';
} else if (difference.hours > 0) {
bestUnit = 'hours';
} else if (difference.minutes > 0) {
bestUnit = 'minutes';
} else if (difference.seconds > 0) {
bestUnit = 'seconds';
} else {
bestUnit = 'milliseconds';
}
const formattedDifference = formatTimeWithLargestUnit( setResult(formattedDifference);
difference,
bestUnit
);
setResult(formattedDifference);
}
} catch (error) { } catch (error) {
setResult( setResult(
`Error: ${ `Error: ${

View File

@@ -1,28 +1,20 @@
type TimeUnit = export const unitHierarchy = [
| 'milliseconds' 'years',
| 'seconds' 'months',
| 'minutes' 'days',
| 'hours' 'hours',
| 'days' 'minutes',
| 'months' 'seconds',
| 'years'; 'milliseconds'
] as const;
interface TimeDifference { export type TimeUnit = (typeof unitHierarchy)[number];
milliseconds: number; export type TimeDifference = Record<TimeUnit, number>;
seconds: number;
minutes: number;
hours: number;
days: number;
months: number;
years: number;
}
export const calculateTimeBetweenDates = ( export const calculateTimeBetweenDates = (
startDate: Date, startDate: Date,
endDate: Date, endDate: Date
unit: TimeUnit = 'milliseconds' ): TimeDifference => {
): TimeDifference | number => {
// Ensure endDate is after startDate
if (endDate < startDate) { if (endDate < startDate) {
const temp = startDate; const temp = startDate;
startDate = endDate; startDate = endDate;
@@ -44,12 +36,6 @@ export const calculateTimeBetweenDates = (
const months = (endYear - startYear) * 12 + (endMonth - startMonth); const months = (endYear - startYear) * 12 + (endMonth - startMonth);
const years = Math.floor(months / 12); const years = Math.floor(months / 12);
// If specific unit requested, return just that value
if (unit !== 'milliseconds') {
return { milliseconds, seconds, minutes, hours, days, months, years }[unit];
}
// Otherwise return the complete breakdown
return { return {
milliseconds, milliseconds,
seconds, seconds,
@@ -63,64 +49,32 @@ export const calculateTimeBetweenDates = (
export const formatTimeDifference = ( export const formatTimeDifference = (
difference: TimeDifference, difference: TimeDifference,
includeUnits: TimeUnit[] = [ includeUnits: TimeUnit[] = unitHierarchy.slice(0, -1)
'years',
'months',
'days',
'hours',
'minutes',
'seconds'
]
): string => { ): string => {
const parts: string[] = []; const timeUnits: { key: TimeUnit; value: number; divisor?: number }[] = [
{ key: 'years', value: difference.years },
{ key: 'months', value: difference.months, divisor: 12 },
{ key: 'days', value: difference.days, divisor: 30 },
{ key: 'hours', value: difference.hours, divisor: 24 },
{ key: 'minutes', value: difference.minutes, divisor: 60 },
{ key: 'seconds', value: difference.seconds, divisor: 60 }
];
if (includeUnits.includes('years') && difference.years > 0) { const parts = timeUnits
parts.push( .filter(({ key }) => includeUnits.includes(key))
`${difference.years} ${difference.years === 1 ? 'year' : 'years'}` .map(({ key, value, divisor }) => {
); const remaining = divisor ? value % divisor : value;
} return remaining > 0 ? `${remaining} ${key}` : '';
})
if (includeUnits.includes('months') && difference.months % 12 > 0) { .filter(Boolean);
const remainingMonths = difference.months % 12;
parts.push(
`${remainingMonths} ${remainingMonths === 1 ? 'month' : 'months'}`
);
}
if (includeUnits.includes('days') && difference.days % 30 > 0) {
const remainingDays = difference.days % 30;
parts.push(`${remainingDays} ${remainingDays === 1 ? 'day' : 'days'}`);
}
if (includeUnits.includes('hours') && difference.hours % 24 > 0) {
const remainingHours = difference.hours % 24;
parts.push(`${remainingHours} ${remainingHours === 1 ? 'hour' : 'hours'}`);
}
if (includeUnits.includes('minutes') && difference.minutes % 60 > 0) {
const remainingMinutes = difference.minutes % 60;
parts.push(
`${remainingMinutes} ${remainingMinutes === 1 ? 'minute' : 'minutes'}`
);
}
if (includeUnits.includes('seconds') && difference.seconds % 60 > 0) {
const remainingSeconds = difference.seconds % 60;
parts.push(
`${remainingSeconds} ${remainingSeconds === 1 ? 'second' : 'seconds'}`
);
}
if (parts.length === 0) { if (parts.length === 0) {
if (includeUnits.includes('milliseconds')) { if (includeUnits.includes('milliseconds')) {
parts.push( return `${difference.milliseconds} millisecond${
`${difference.milliseconds} ${ difference.milliseconds === 1 ? '' : 's'
difference.milliseconds === 1 ? 'millisecond' : 'milliseconds' }`;
}`
);
} else {
parts.push('0 seconds');
} }
return '0 seconds';
} }
return parts.join(', '); return parts.join(', ');
@@ -132,19 +86,68 @@ export const getTimeWithTimezone = (
timezone: string timezone: string
): Date => { ): Date => {
// Combine date and time // Combine date and time
const dateTimeString = `${dateString}T${timeString}`; const dateTimeString = `${dateString}T${timeString}Z`; // Append 'Z' to enforce UTC parsing
const utcDate = new Date(dateTimeString);
// Create a date object in the local timezone if (isNaN(utcDate.getTime())) {
const dateObject = new Date(dateTimeString); throw new Error('Invalid date or time format');
// If timezone is provided, adjust the date
if (timezone && timezone !== 'local') {
// Create date string with the timezone identifier
const dateWithTimezone = new Date(
dateTimeString + timezone.replace('GMT', '')
);
return dateWithTimezone;
} }
return dateObject; // If timezone is "local", return the local date
if (timezone === 'local') {
return utcDate;
}
// Extract offset from timezone (e.g., "GMT+5:30" or "GMT-4")
const match = timezone.match(/^GMT(?:([+-]\d{1,2})(?::(\d{2}))?)?$/);
if (!match) {
throw new Error('Invalid timezone format');
}
const offsetHours = match[1] ? parseInt(match[1], 10) : 0;
const offsetMinutes = match[2] ? parseInt(match[2], 10) : 0;
const totalOffsetMinutes =
offsetHours * 60 + (offsetHours < 0 ? -offsetMinutes : offsetMinutes);
// Adjust the UTC date by the timezone offset
return new Date(utcDate.getTime() - totalOffsetMinutes * 60 * 1000);
};
// Helper function to format time based on largest unit
export const formatTimeWithLargestUnit = (
difference: TimeDifference,
largestUnit: TimeUnit
): string => {
const conversionFactors: Record<TimeUnit, number> = {
years: 365.2425, // Leap years considered
months: 30.436875, // Average month length
days: 24, // Hours per day
hours: 60, // Minutes per hour
minutes: 60, // Seconds per minute
seconds: 1000, // Milliseconds per second
milliseconds: 1
};
const largestUnitIndex = unitHierarchy.indexOf(largestUnit);
const unitsToInclude = unitHierarchy.slice(largestUnitIndex);
// Deep copy to avoid mutating original object
const convertedDifference = { ...difference };
let carryOver = 0;
for (let i = 0; i < largestUnitIndex; i++) {
const unit = unitHierarchy[i];
const nextUnit = unitHierarchy[i + 1];
if (nextUnit) {
carryOver =
(convertedDifference[unit] || 0) * (conversionFactors[unit] || 1);
convertedDifference[nextUnit] =
(convertedDifference[nextUnit] || 0) + carryOver;
convertedDifference[unit] = 0;
}
}
return formatTimeDifference(convertedDifference, unitsToInclude);
}; };