Skip to content

Main content

Random Timestamp Generator Source Code

Browse the implementation files for Random Timestamp Generator.
tool.ts typescript
import {
	calculateRandomTimestampValue,
	type TimestampUnit
} from '@tool.io/tools/random/timestamp';
import type { RandomSource } from '@tool.io/tools/random/number';
import { validateChoice } from '../../../tool-runtime/validateChoice';

const UNITS = ['seconds', 'milliseconds'] as const;
const currentYear = new Date().getUTCFullYear();
const defaultStartTimestamp = `${currentYear}-01-01T00:00:00Z`;
const defaultEndTimestamp = `${currentYear}-12-31T23:59:59Z`;

export type { TimestampUnit };

const parseTimestamp = (input: unknown): number | null => {
	if (typeof input !== 'string' || input.trim() === '')
		return null;

	const value = Date.parse(input);

	return Number.isFinite(value) ? value : null;
};

export const calculateRandomTimestamp = (
	startMilliseconds: number,
	endMilliseconds: number,
	unit: TimestampUnit,
	random: RandomSource = Math.random
): number | null => {
	return calculateRandomTimestampValue(
		startMilliseconds,
		endMilliseconds,
		unit,
		random
	);
};

export const randomTimestamp = (
	startInput: unknown,
	endInput: unknown,
	unitInput: unknown,
	random: RandomSource = Math.random
): string => {
	const start = parseTimestamp(startInput);
	const end = parseTimestamp(endInput);
	const unit = validateChoice(unitInput, UNITS);

	if (
		start === null ||
		end === null ||
		start > end ||
		unit === null
	) {
		return '';
	}

	const value = calculateRandomTimestamp(
		start,
		end,
		unit,
		random
	);

	return value === null ? '' : String(value);
};

export const Tool: TypeTool = {
	name: 'Random Timestamp Generator',
	slug: 'random/timestamp',
	intro:
		'Generate a random Unix timestamp within a date-time range.',
	tool: randomTimestamp,
	i: [
		{
			example: defaultStartTimestamp,
			label: 'Start date and time',
			placeholder: 'ISO date and time',
			type: 'text',
			value: defaultStartTimestamp
		},
		{
			example: defaultEndTimestamp,
			label: 'End date and time',
			placeholder: 'ISO date and time',
			type: 'text',
			value: defaultEndTimestamp
		},
		{
			example: 'seconds',
			label: 'Unit',
			placeholder: 'Timestamp unit',
			type: 'select',
			value: 'seconds',
			options: [
				{ value: 'seconds', text: 'Seconds', selected: true },
				{ value: 'milliseconds', text: 'Milliseconds' }
			]
		}
	],
	o: [
		{
			example: '1784743242',
			label: 'Random timestamp',
			placeholder: 'Random timestamp',
			render: 'textarea',
			type: 'text'
		}
	],
	rerunnable: true,
	alternative: [
		{ title: 'Random Date', url: '/random/date' },
		{ title: 'Random Time', url: '/random/time' }
	],
	cite: [
		{
			title: 'Unix time - Wikipedia',
			url: 'https://en.wikipedia.org/wiki/Unix_time'
		}
	]
};

export default Tool;

Description

Browse the implementation files for Random Timestamp Generator.

References

  1. Tool.io on GitHub
  2. Tool.io TypeScript tools library

Similar or alternative tools