diff --git a/public/locales/en/time.json b/public/locales/en/time.json index 487296a..650692c 100644 --- a/public/locales/en/time.json +++ b/public/locales/en/time.json @@ -67,17 +67,20 @@ "convertUnixToDate": { "addUtcLabel": "Add 'UTC' suffix", "addUtcLabelDescription": "Display 'UTC' after the converted date (only for UTC mode)", - "description": "Convert a Unix timestamp to a human-readable date.", + "description": "Convert a Unix timestamp to a human-readable date or vice versa.", "outputOptions": "Output Options", - "shortDescription": "Convert Unix timestamp to date", - "title": "Convert Unix to Date", + "shortDescription": "Convert Unix timestamp to date and vice versa.", + "title": "Convert Unix to Date / Date to Unix", "toolInfo": { - "description": "This tool converts a Unix timestamp (in seconds) into a human-readable date format (e.g., YYYY-MM-DD HH:MM:SS). It supports both local and UTC output, making it useful for quickly interpreting timestamps from logs, APIs, or systems that use Unix time.", - "title": "Convert Unix to Date" + "description": "This tool converts a Unix timestamp (in seconds) into a human-readable date format (e.g., YYYY-MM-DD HH:MM:SS). It supports both local and UTC output, making it useful for quickly interpreting timestamps from logs, APIs, or systems that use Unix time. The tool also converts a human-readable date format (e.g., YYYY-MM-DD HH:MM:SS) into a Unix Timestamp. The user can choose to specify a given UTC offset (e.g., YYYY-MM-DD HH:MM:SS +XX:XX) or use the local timezone. If the user doesn't provide either an offset or choose to use the local timezone, the result is given with an offset of +00:00. ", + "title": "Convert Unix to Date / Date to Unix" }, "useLocalTime": "Use Local Time", "useLocalTimeDescription": "Show converted date in your local timezone instead of UTC", - "withLabel": "Options" + "withLabel": "Options", + "optionsTitle": "Mode", + "date-to-unix": "Convert Date To Unix", + "unix-to-date": "Convert Unix To Date" }, "crontabGuru": { "description": "Generate and understand cron expressions. Create cron schedules for automated tasks and system jobs.", @@ -120,12 +123,6 @@ "zeroPrintDescription": "Display the dropped parts as zero values \"00\".", "zeroPrintTruncatedParts": "Zero-print Truncated Parts" }, - "convertTimeToDecimal": { - "title": "Convert time to decimal", - "description": "Convert a formatted time duration (HH:MM:SS) into a decimal hour value.", - "shortDescription": "Convert human time to decimal time", - "longDescription": "Convert a formatted time string (HH:MM:SS or HH:MM) into its decimal-hour equivalent. Hours can be any positive number, while minutes and seconds accept values from 0-59 and can be single or double digits (e.g., '12:5' or '12:05'). This function interprets hours, minutes, and seconds, then calculates the total duration as a single decimal value. It is useful for productivity tracking, payroll calculations, time-based billing, data analysis, or any workflow that requires converting human-readable time into a numerical format that can be easily summed, compared, or processed. For example, '03:26:00' becomes 3.43 hours, and '12:5' becomes 12.08 hours." - }, "discordTimestamp": { "name": "Discord Timestamp Generator", "description": "Convert dates and times into Discord's special timestamp format that automatically adapts to each user's local timezone and language", diff --git a/src/pages/tools/time/convert-date-to-unix/example.tsx b/src/pages/tools/time/convert-date-to-unix/example.tsx deleted file mode 100644 index b3d631b..0000000 --- a/src/pages/tools/time/convert-date-to-unix/example.tsx +++ /dev/null @@ -1,33 +0,0 @@ -import { CardExampleType } from '@components/examples/ToolExamples'; -import InitialValuesType from './types'; - -export const exampleCards: CardExampleType[] = [ - { - title: 'Basic Date to Unix', - description: - 'In this example, a plain human-readable timeframe is converted into a Unix timestamp. The UTC offset is treated as +00:00. ', - sampleText: `1990-03-17 14:23:00 -2012-12-21 00:00:00`, - sampleResult: `637654980 -1356019200`, - sampleOptions: { useLocalTime: false } - }, - { - title: 'With UTC Offset', - description: - 'In this example, the UTC offset is provided after stating the timeframe with a space.', - sampleText: `1985-06-15 12:00:00 +00:00 -2025-04-04 10:00:00 +08:00`, - sampleResult: `487684800 -1743732000`, - sampleOptions: { useLocalTime: false } - }, - { - title: 'Use Local Time', - description: - "This example uses your browser's timezone. Any suffix provided as the UTC offset is ignored. In this example we assume the timezone of the user to be UTC+6.", - sampleText: `2025-04-04 11:30:00`, - sampleResult: `1743744600`, - sampleOptions: { useLocalTime: true } - } -]; diff --git a/src/pages/tools/time/convert-date-to-unix/index.tsx b/src/pages/tools/time/convert-date-to-unix/index.tsx deleted file mode 100644 index d8ff703..0000000 --- a/src/pages/tools/time/convert-date-to-unix/index.tsx +++ /dev/null @@ -1,62 +0,0 @@ -import { Box } from '@mui/material'; -import { useState } from 'react'; -import ToolContent from '@components/ToolContent'; -import { ToolComponentProps } from '@tools/defineTool'; -import ToolTextInput from '@components/input/ToolTextInput'; -import ToolTextResult from '@components/result/ToolTextResult'; -import { GetGroupsType } from '@components/options/ToolOptions'; -import CheckboxWithDesc from '@components/options/CheckboxWithDesc'; -import { convertToUnix } from './service'; -import { useTranslation } from 'react-i18next'; -import InitialValuesType from './types'; -import { exampleCards } from './example'; - -const initialValues: InitialValuesType = { - useLocalTime: false -}; - -export default function convertDateToUnix({ title }: ToolComponentProps) { - const { t } = useTranslation('time'); - const [input, setInput] = useState(''); - const [result, setResult] = useState(''); - - const compute = (values: InitialValuesType, input: string) => { - setResult(convertToUnix(input, values.useLocalTime)); - }; - - const getGroups: GetGroupsType | null = ({ - values, - updateField - }) => [ - { - title: t('convertDateToUnix.options'), - component: ( - - updateField('useLocalTime', val)} - checked={values.useLocalTime} - title={t('convertDateToUnix.useLocalTime')} - description={t('convertDateToUnix.useLocalTimeDescription')} - /> - - ) - } - ]; - return ( - } - resultComponent={} - initialValues={initialValues} - exampleCards={exampleCards} - getGroups={getGroups} - setInput={setInput} - compute={compute} - toolInfo={{ - title: t('convertDateToUnix.toolInfo.title'), - description: t('convertDateToUnix.toolInfo.description') - }} - /> - ); -} diff --git a/src/pages/tools/time/convert-date-to-unix/meta.ts b/src/pages/tools/time/convert-date-to-unix/meta.ts deleted file mode 100644 index e28f98f..0000000 --- a/src/pages/tools/time/convert-date-to-unix/meta.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { defineTool } from '@tools/defineTool'; -import { lazy } from 'react'; - -export const tool = defineTool('time', { - i18n: { - name: 'time:convertDateToUnix.title', - description: 'time:convertDateToUnix.description', - shortDescription: 'time:convertDateToUnix.shortDescription' - }, - path: 'convert-date-to-unix', - icon: 'qlementine-icons:update-16', - keywords: ['convert', 'date', 'to', 'unix'], - component: lazy(() => import('./index')) -}); diff --git a/src/pages/tools/time/convert-date-to-unix/service.ts b/src/pages/tools/time/convert-date-to-unix/service.ts deleted file mode 100644 index 48c4622..0000000 --- a/src/pages/tools/time/convert-date-to-unix/service.ts +++ /dev/null @@ -1,80 +0,0 @@ -// Changing a Date Object Into Unix -function computeDateToUnix(input: string, useLocalTime: boolean): string { - try { - // Extract Data - const splitInput = input.split(' '); - const timeFrame = splitInput[0] + ' ' + splitInput[1]; - const utcOffset = splitInput.length > 2 ? splitInput[2] : null; - - // Convert into Unix (This Has The Local Offset) - const localUnixValue: number = Date.parse(timeFrame) / 1000; - - // Unsuccesful Conversion - if (isNaN(localUnixValue)) { - return 'Invalid Date Time'; - } - - // Case 1: Base Scenario (Assume Time with GMT +00) - if (!useLocalTime && !utcOffset) { - const processedDateTime = convertToGMTZero(localUnixValue); - return `${processedDateTime}`; - } - - // Case 2: Time Given with Custom Offset - if (!useLocalTime && utcOffset) { - // Verify Offset (-12:00 to +14.00 in 15 Minute Intervals) - const regexMatch = utcOffset.match(/^[+-](0\d|1[0-4]):(00|15|30|45)$/); - - // Invalid Regex - if (!regexMatch) { - return 'Invalid Date Time'; - } - - // Variables to Be Processed - const hours = Number(regexMatch[1]); - const minutes = Number(regexMatch[2]) + hours * 60; - const offset = minutes * 60; - - // Remove The Offset to The Time (Opposite of First Symbol - If Input Time is +08:00 Then We Need to Deduct by 8 Hours) - const processedDateTime = convertToGMTZero(localUnixValue) - offset; - return `${processedDateTime}`; - } - - // Case 3: Local (Time Given is Local Time) - if (useLocalTime) { - return `${localUnixValue}`; - } - - return `Valid Date`; - } catch (err) { - // Not Using Local Time (Either UTC is Given or Assume +00) - console.log('Error', err); - return 'Invalid Date Time'; - } -} - -function convertToGMTZero(localUnixValue: number): number { - // Get A Reference Object - const refTimeDate = new Date(); - const offsetMinutes = refTimeDate.getTimezoneOffset(); - - // Remove the Offset - const processedDateTime = localUnixValue - offsetMinutes * 60; - return processedDateTime; -} - -export function convertToUnix(input: string, useLocalTime: boolean): string { - // Empty Input - if (!input) return ''; - - const result: string[] = []; - const lines = input.split('\n'); - - // Process Input Lines - lines.forEach((line) => { - const formattedDate = computeDateToUnix(line, useLocalTime); - result.push(formattedDate); - }); - - return result.join('\n'); -} diff --git a/src/pages/tools/time/convert-unix-to-date/index.tsx b/src/pages/tools/time/convert-unix-to-date/index.tsx index 77349d3..8e5c249 100644 --- a/src/pages/tools/time/convert-unix-to-date/index.tsx +++ b/src/pages/tools/time/convert-unix-to-date/index.tsx @@ -7,14 +7,16 @@ import ToolTextResult from '@components/result/ToolTextResult'; import { GetGroupsType } from '@components/options/ToolOptions'; import { CardExampleType } from '@components/examples/ToolExamples'; import CheckboxWithDesc from '@components/options/CheckboxWithDesc'; -import { convertUnixToDate } from './service'; +import { convertString } from './service'; import { useTranslation } from 'react-i18next'; +import InitialValuesType from './types'; +import SimpleRadio from '@components/options/SimpleRadio'; -const initialValues = { +const initialValues: InitialValuesType = { + mode: 'unix-to-date', withLabel: true, useLocalTime: false }; -type InitialValuesType = typeof initialValues; const exampleCards: CardExampleType[] = [ { @@ -27,7 +29,11 @@ const exampleCards: CardExampleType[] = [ sampleResult: `1970-01-01 00:00:00:000 UTC 2024-07-18 10:00:27:000 UTC 2038-01-19 03:14:07:000 UTC`, - sampleOptions: { withLabel: true, useLocalTime: false } + sampleOptions: { + mode: 'unix-to-date', + withLabel: true, + useLocalTime: false + } }, { title: 'Without UTC Suffix', @@ -37,7 +43,11 @@ const exampleCards: CardExampleType[] = [ 1672531199`, sampleResult: `2009-02-13 23:31:30 2022-12-31 23:59:59:000`, - sampleOptions: { withLabel: false, useLocalTime: false } + sampleOptions: { + mode: 'unix-to-date', + withLabel: false, + useLocalTime: false + } }, { title: 'Use Local Time', @@ -45,7 +55,51 @@ const exampleCards: CardExampleType[] = [ 'This example demonstrates how timestamps are shown in your local timezone rather than UTC. The UTC suffix is omitted in this case.', sampleText: `1721287227`, sampleResult: `2024-07-18 12:00:27`, - sampleOptions: { withLabel: true, useLocalTime: true } + sampleOptions: { + mode: 'unix-to-date', + withLabel: true, + useLocalTime: true + } + }, + { + title: 'Basic Date to Unix', + description: + 'In this example, a plain human-readable timeframe is converted into a Unix timestamp. The UTC offset is treated as +00:00. ', + sampleText: `1990-03-17 14:23:00 +2012-12-21 00:00:00`, + sampleResult: `637654980 +1356019200`, + sampleOptions: { + mode: 'date-to-unix', + withLabel: true, + useLocalTime: false + } + }, + { + title: 'With UTC Offset Provided', + description: + 'In this example, the UTC offset is provided after stating the timeframe with a space.', + sampleText: `1985-06-15 12:00:00 +00:00 +2025-04-04 10:00:00 +08:00`, + sampleResult: `487684800 +1743732000`, + sampleOptions: { + mode: 'date-to-unix', + withLabel: true, + useLocalTime: false + } + }, + { + title: 'Use Local Time', + description: + "This example uses your browser's timezone. Any suffix provided as the UTC offset is ignored. In this example we assume the timezone of the user to be UTC+6.", + sampleText: `2025-04-04 11:30:00`, + sampleResult: `1743744600`, + sampleOptions: { + mode: 'date-to-unix', + withLabel: true, + useLocalTime: true + } } ]; @@ -55,23 +109,45 @@ export default function ConvertUnixToDate({ title }: ToolComponentProps) { const [result, setResult] = useState(''); const compute = (values: typeof initialValues, input: string) => { - setResult(convertUnixToDate(input, values.withLabel, values.useLocalTime)); + setResult( + convertString(input, values.mode, values.useLocalTime, values.withLabel) + ); }; const getGroups: GetGroupsType | null = ({ values, updateField }) => [ + { + title: t('convertUnixToDate.optionsTitle'), + component: ( + + updateField('mode', 'unix-to-date')} + checked={values.mode === 'unix-to-date'} + title={t('convertUnixToDate.unix-to-date')} + /> + updateField('mode', 'date-to-unix')} + checked={values.mode === 'date-to-unix'} + title={t('convertUnixToDate.date-to-unix')} + /> + + ) + }, { title: t('convertUnixToDate.withLabel'), component: ( - updateField('withLabel', val)} - checked={values.withLabel} - title={t('convertUnixToDate.addUtcLabel')} - description={t('convertUnixToDate.addUtcLabelDescription')} - /> + {/* Only Show Add UTC Suffix Option on Convert Unix to Date */} + {values.mode === 'unix-to-date' && ( + updateField('withLabel', val)} + checked={values.withLabel} + title={t('convertUnixToDate.addUtcLabel')} + description={t('convertUnixToDate.addUtcLabelDescription')} + /> + )} updateField('useLocalTime', val)} checked={values.useLocalTime} diff --git a/src/pages/tools/time/convert-unix-to-date/service.ts b/src/pages/tools/time/convert-unix-to-date/service.ts index 8f1ec0d..faa3433 100644 --- a/src/pages/tools/time/convert-unix-to-date/service.ts +++ b/src/pages/tools/time/convert-unix-to-date/service.ts @@ -1,5 +1,19 @@ import { containsOnlyDigits } from '@utils/string'; +export function convertString( + input: string, + mode: string, + useLocalTime: boolean, + withLabel: boolean +): string { + if (mode === 'unix-to-date') { + return convertUnixToDate(input, withLabel, useLocalTime); + } else { + return convertDateToUnix(input, useLocalTime); + } +} + +// Unix to Date function computeUnixToDate(input: string, useLocalTime: boolean): string { if (!containsOnlyDigits(input)) { return ''; @@ -20,7 +34,7 @@ function computeUnixToDate(input: string, useLocalTime: boolean): string { } } -export function convertUnixToDate( +function convertUnixToDate( input: string, withLabel: boolean, useLocalTime: boolean @@ -40,3 +54,82 @@ export function convertUnixToDate( return result.join('\n'); } + +// Date to Unix +function computeDateToUnix(input: string, useLocalTime: boolean): string { + try { + // Extract Data + const splitInput = input.split(' '); + const timeFrame = splitInput[0] + ' ' + splitInput[1]; + const utcOffset = splitInput.length > 2 ? splitInput[2] : null; + + // Convert into Unix (This Has The Local Offset) + const localUnixValue: number = Date.parse(timeFrame) / 1000; + + // Unsuccesful Conversion + if (isNaN(localUnixValue)) { + return 'Invalid Date Time'; + } + + // Case 1: Base Scenario (Assume Time with GMT +00) + if (!useLocalTime && !utcOffset) { + const processedDateTime = convertToGMTZero(localUnixValue); + return `${processedDateTime}`; + } + + // Case 2: Time Given with Custom Offset + if (!useLocalTime && utcOffset) { + // Verify Offset (-12:00 to +14.00 in 15 Minute Intervals) + const regexMatch = utcOffset.match(/^[+-](0\d|1[0-4]):(00|15|30|45)$/); + + // Invalid Regex + if (!regexMatch) { + return 'Invalid Date Time'; + } + + // Variables to Be Processed + const hours = Number(regexMatch[1]); + const minutes = Number(regexMatch[2]) + hours * 60; + const offset = minutes * 60; + + // Remove The Offset to The Time (Opposite of First Symbol - If Input Time is +08:00 Then We Need to Deduct by 8 Hours) + const processedDateTime = convertToGMTZero(localUnixValue) - offset; + return `${processedDateTime}`; + } + + // Case 3: Local (Time Given is Local Time) + if (useLocalTime) { + return `${localUnixValue}`; + } + } catch (err) { + // Not Using Local Time (Either UTC is Given or Assume +00) + console.log('Error', err); + return 'Invalid Date Time'; + } +} + +function convertToGMTZero(localUnixValue: number): number { + // Get A Reference Object + const refTimeDate = new Date(); + const offsetMinutes = refTimeDate.getTimezoneOffset(); + + // Remove the Offset + const processedDateTime = localUnixValue - offsetMinutes * 60; + return processedDateTime; +} + +function convertDateToUnix(input: string, useLocalTime: boolean): string { + // Empty Input + if (!input) return ''; + + const result: string[] = []; + const lines = input.split('\n'); + + // Process Input Lines + lines.forEach((line) => { + const formattedDate = computeDateToUnix(line, useLocalTime); + result.push(formattedDate); + }); + + return result.join('\n'); +} diff --git a/src/pages/tools/time/convert-date-to-unix/types.ts b/src/pages/tools/time/convert-unix-to-date/types.ts similarity index 65% rename from src/pages/tools/time/convert-date-to-unix/types.ts rename to src/pages/tools/time/convert-unix-to-date/types.ts index 0ddf3ad..6c2d08e 100644 --- a/src/pages/tools/time/convert-date-to-unix/types.ts +++ b/src/pages/tools/time/convert-unix-to-date/types.ts @@ -1,3 +1,5 @@ export default interface InitialValuesType { + mode: string; + withLabel: boolean; useLocalTime: boolean; } diff --git a/src/pages/tools/time/index.ts b/src/pages/tools/time/index.ts index d42a499..9a51a61 100644 --- a/src/pages/tools/time/index.ts +++ b/src/pages/tools/time/index.ts @@ -9,7 +9,6 @@ import { tool as convertTimetoSeconds } from './convert-time-to-seconds/meta'; import { tool as truncateClockTime } from './truncate-clock-time/meta'; import { tool as checkLeapYear } from './check-leap-years/meta'; import { tool as discordTimestamp } from './discord-timestamp/meta'; -import { tool as timeConvertDateToUnix } from './convert-date-to-unix/meta'; export const timeTools = [ daysDoHours, @@ -21,7 +20,6 @@ export const timeTools = [ timeCrontabGuru, checkLeapYear, timeConvertUnixToDate, - timeConvertDateToUnix, timeConvertTimeToDecimal, discordTimestamp ];