mirror of
https://github.com/iib0011/omni-tools.git
synced 2025-09-18 21:49:31 +02:00
Merge branch 'main' into chesterking
This commit is contained in:
@@ -98,5 +98,21 @@
|
||||
"zeroPaddingDescription": "Make all time components always be two digits wide.",
|
||||
"zeroPrintDescription": "Display the dropped parts as zero values \"00\".",
|
||||
"zeroPrintTruncatedParts": "Zero-print Truncated Parts"
|
||||
},
|
||||
"convertUnixToDate": {
|
||||
"title": "Convert Unix to Date",
|
||||
"description": "Convert a Unix timestamp to a human-readable date.",
|
||||
"shortDescription": "Convert Unix timestamp to date",
|
||||
"longDescription": "",
|
||||
"withLabel": "Options",
|
||||
"outputOptions": "Output Options",
|
||||
"addUtcLabel": "Add 'UTC' suffix",
|
||||
"addUtcLabelDescription": "Display 'UTC' after the converted date (only for UTC mode)",
|
||||
"useLocalTime": "Use Local Time",
|
||||
"useLocalTimeDescription": "Show converted date in your local timezone instead of UTC",
|
||||
"toolInfo": {
|
||||
"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."
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -96,5 +96,21 @@
|
||||
"zeroPaddingDescription": "Faites en sorte que tous les composants de temps aient toujours une largeur de deux chiffres.",
|
||||
"zeroPrintDescription": "Afficher les parties supprimées sous forme de valeurs nulles « 00 ».",
|
||||
"zeroPrintTruncatedParts": "Parties tronquées sans impression"
|
||||
},
|
||||
"convertUnixToDate": {
|
||||
"title": "Convertir un timestamp Unix en date",
|
||||
"description": "Convertit un timestamp Unix en une date lisible par un humain.",
|
||||
"shortDescription": "Conversion de timestamp Unix en date",
|
||||
"longDescription": "Cet outil permet de convertir un timestamp Unix (en secondes) en une date lisible au format AAAA-MM-JJ HH:MM:SS. Il prend en charge l'affichage en UTC ou dans le fuseau horaire local, ce qui est pratique pour interpréter rapidement des horodatages issus de journaux, d'API ou de systèmes utilisant le temps Unix.",
|
||||
"withLabel": "Options",
|
||||
"outputOptions": "Options de sortie",
|
||||
"addUtcLabel": "Ajouter le suffixe 'UTC'",
|
||||
"addUtcLabelDescription": "Affiche 'UTC' après la date convertie (uniquement en mode UTC)",
|
||||
"useLocalTime": "Utiliser l’heure locale",
|
||||
"useLocalTimeDescription": "Affiche la date convertie dans votre fuseau horaire local au lieu de l’heure UTC",
|
||||
"toolInfo": {
|
||||
"title": "Convertir un timestamp Unix en date",
|
||||
"description": "Cet outil convertit un timestamp Unix (en secondes) en une date lisible (par ex. AAAA-MM-JJ HH:MM:SS). Il prend en charge l'affichage en heure locale ou en UTC, ce qui le rend utile pour analyser rapidement des données issues de journaux ou d’APIs."
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,56 @@
|
||||
import { expect, describe, it } from 'vitest';
|
||||
import { convertUnixToDate } from './service';
|
||||
|
||||
describe('convertUnixToDate', () => {
|
||||
it('should convert a single Unix timestamp with label (UTC)', () => {
|
||||
const input = '0';
|
||||
const result = convertUnixToDate(input, true, false);
|
||||
expect(result).toBe('1970-01-01 00:00:00.000 UTC');
|
||||
});
|
||||
|
||||
it('should convert a single Unix timestamp without label (UTC)', () => {
|
||||
const input = '1234567890';
|
||||
const result = convertUnixToDate(input, false, false);
|
||||
expect(result).toBe('2009-02-13 23:31:30.000');
|
||||
});
|
||||
|
||||
it('should convert a single Unix timestamp in local time', () => {
|
||||
const input = '1234567890';
|
||||
const result = convertUnixToDate(input, true, true);
|
||||
expect(result.endsWith('UTC')).toBe(false);
|
||||
});
|
||||
|
||||
it('should handle multiple lines with label (UTC)', () => {
|
||||
const input = '0\n2147483647';
|
||||
const result = convertUnixToDate(input, true, false);
|
||||
expect(result).toBe(
|
||||
'1970-01-01 00:00:00.000 UTC\n2038-01-19 03:14:07.000 UTC'
|
||||
);
|
||||
});
|
||||
|
||||
it('should handle multiple lines with local time', () => {
|
||||
const input = '1672531199\n1721287227';
|
||||
const result = convertUnixToDate(input, false, true);
|
||||
const lines = result.split('\n');
|
||||
expect(lines.length).toBe(2);
|
||||
expect(lines[0].endsWith('UTC')).toBe(false);
|
||||
});
|
||||
|
||||
it('should return empty string for invalid input', () => {
|
||||
const input = 'not_a_number';
|
||||
const result = convertUnixToDate(input, true, false);
|
||||
expect(result).toBe('');
|
||||
});
|
||||
|
||||
it('should return empty string for empty input', () => {
|
||||
const input = '';
|
||||
const result = convertUnixToDate(input, false, false);
|
||||
expect(result).toBe('');
|
||||
});
|
||||
|
||||
it('should ignore invalid lines in multiline input', () => {
|
||||
const input = 'abc\n1600000000';
|
||||
const result = convertUnixToDate(input, true, false);
|
||||
expect(result).toBe('\n2020-09-13 12:26:40.000 UTC');
|
||||
});
|
||||
});
|
102
src/pages/tools/time/convert-unix-to-date/index.tsx
Normal file
102
src/pages/tools/time/convert-unix-to-date/index.tsx
Normal file
@@ -0,0 +1,102 @@
|
||||
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 { CardExampleType } from '@components/examples/ToolExamples';
|
||||
import CheckboxWithDesc from '@components/options/CheckboxWithDesc';
|
||||
import { convertUnixToDate } from './service';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
|
||||
const initialValues = {
|
||||
withLabel: true,
|
||||
useLocalTime: false
|
||||
};
|
||||
type InitialValuesType = typeof initialValues;
|
||||
|
||||
const exampleCards: CardExampleType<InitialValuesType>[] = [
|
||||
{
|
||||
title: 'Basic Unix Time to Date',
|
||||
description:
|
||||
'This example shows how Unix timestamps are converted into human-readable dates. Each timestamp represents the number of seconds that have elapsed since January 1, 1970 (UTC).',
|
||||
sampleText: `0
|
||||
1721287227
|
||||
2147483647`,
|
||||
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 }
|
||||
},
|
||||
{
|
||||
title: 'Without UTC Suffix',
|
||||
description:
|
||||
'In this example, the UTC suffix is removed from the output. This might be useful for embedding timestamps into other formats or for cleaner display.',
|
||||
sampleText: `1234567890
|
||||
1672531199`,
|
||||
sampleResult: `2009-02-13 23:31:30
|
||||
2022-12-31 23:59:59:000`,
|
||||
sampleOptions: { withLabel: false, useLocalTime: false }
|
||||
},
|
||||
{
|
||||
title: 'Use Local Time',
|
||||
description:
|
||||
'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 }
|
||||
}
|
||||
];
|
||||
|
||||
export default function ConvertUnixToDate({ title }: ToolComponentProps) {
|
||||
const { t } = useTranslation('time');
|
||||
const [input, setInput] = useState<string>('');
|
||||
const [result, setResult] = useState<string>('');
|
||||
|
||||
const compute = (values: typeof initialValues, input: string) => {
|
||||
setResult(convertUnixToDate(input, values.withLabel, values.useLocalTime));
|
||||
};
|
||||
|
||||
const getGroups: GetGroupsType<InitialValuesType> | null = ({
|
||||
values,
|
||||
updateField
|
||||
}) => [
|
||||
{
|
||||
title: t('convertUnixToDate.withLabel'),
|
||||
component: (
|
||||
<Box>
|
||||
<CheckboxWithDesc
|
||||
onChange={(val) => updateField('withLabel', val)}
|
||||
checked={values.withLabel}
|
||||
title={t('convertUnixToDate.addUtcLabel')}
|
||||
description={t('convertUnixToDate.addUtcLabelDescription')}
|
||||
/>
|
||||
<CheckboxWithDesc
|
||||
onChange={(val) => updateField('useLocalTime', val)}
|
||||
checked={values.useLocalTime}
|
||||
title={t('convertUnixToDate.useLocalTime')}
|
||||
description={t('convertUnixToDate.useLocalTimeDescription')}
|
||||
/>
|
||||
</Box>
|
||||
)
|
||||
}
|
||||
];
|
||||
return (
|
||||
<ToolContent
|
||||
title={title}
|
||||
input={input}
|
||||
inputComponent={<ToolTextInput value={input} onChange={setInput} />}
|
||||
resultComponent={<ToolTextResult value={result} />}
|
||||
initialValues={initialValues}
|
||||
exampleCards={exampleCards}
|
||||
getGroups={getGroups}
|
||||
setInput={setInput}
|
||||
compute={compute}
|
||||
toolInfo={{
|
||||
title: t('convertUnixToDate.toolInfo.title'),
|
||||
description: t('convertUnixToDate.toolInfo.description')
|
||||
}}
|
||||
/>
|
||||
);
|
||||
}
|
15
src/pages/tools/time/convert-unix-to-date/meta.ts
Normal file
15
src/pages/tools/time/convert-unix-to-date/meta.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { defineTool } from '@tools/defineTool';
|
||||
import { lazy } from 'react';
|
||||
|
||||
export const tool = defineTool('time', {
|
||||
i18n: {
|
||||
name: 'time:convertUnixToDate.title',
|
||||
description: 'time:convertUnixToDate.description',
|
||||
shortDescription: 'time:convertUnixToDate.shortDescription',
|
||||
longDescription: 'time:convertUnixToDate.longDescription'
|
||||
},
|
||||
path: 'convert-unix-to-date',
|
||||
icon: 'material-symbols:schedule',
|
||||
keywords: ['convert', 'unix', 'to', 'date'],
|
||||
component: lazy(() => import('./index'))
|
||||
});
|
42
src/pages/tools/time/convert-unix-to-date/service.ts
Normal file
42
src/pages/tools/time/convert-unix-to-date/service.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { containsOnlyDigits } from '@utils/string';
|
||||
|
||||
function computeUnixToDate(input: string, useLocalTime: boolean): string {
|
||||
if (!containsOnlyDigits(input)) {
|
||||
return '';
|
||||
}
|
||||
const timestamp = parseInt(input, 10);
|
||||
const date = new Date(timestamp * 1000); // Convert from seconds to milliseconds
|
||||
|
||||
if (useLocalTime) {
|
||||
const year = date.getFullYear();
|
||||
const month = String(date.getMonth() + 1).padStart(2, '0');
|
||||
const day = String(date.getDate()).padStart(2, '0');
|
||||
const hours = String(date.getHours()).padStart(2, '0');
|
||||
const minutes = String(date.getMinutes()).padStart(2, '0');
|
||||
const seconds = String(date.getSeconds()).padStart(2, '0');
|
||||
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
|
||||
} else {
|
||||
return date.toISOString().replace('T', ' ').replace('Z', '');
|
||||
}
|
||||
}
|
||||
|
||||
export function convertUnixToDate(
|
||||
input: string,
|
||||
withLabel: boolean,
|
||||
useLocalTime: boolean
|
||||
): string {
|
||||
const result: string[] = [];
|
||||
|
||||
const lines = input.split('\n');
|
||||
|
||||
lines.forEach((line) => {
|
||||
const parts = line.split(' ');
|
||||
const timestamp = parts[0];
|
||||
const formattedDate = computeUnixToDate(timestamp, useLocalTime);
|
||||
|
||||
const label = !useLocalTime && withLabel ? ' UTC' : '';
|
||||
result.push(formattedDate ? `${formattedDate}${label}` : '');
|
||||
});
|
||||
|
||||
return result.join('\n');
|
||||
}
|
@@ -1,3 +1,4 @@
|
||||
import { tool as timeConvertUnixToDate } from './convert-unix-to-date/meta';
|
||||
import { tool as timeCrontabGuru } from './crontab-guru/meta';
|
||||
import { tool as timeBetweenDates } from './time-between-dates/meta';
|
||||
import { tool as daysDoHours } from './convert-days-to-hours/meta';
|
||||
@@ -15,5 +16,6 @@ export const timeTools = [
|
||||
truncateClockTime,
|
||||
timeBetweenDates,
|
||||
timeCrontabGuru,
|
||||
checkLeapYear
|
||||
checkLeapYear,
|
||||
timeConvertUnixToDate
|
||||
];
|
||||
|
Reference in New Issue
Block a user