From 36b10808bff6143c2abef355bd6e4967c7423e61 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Mon, 10 Jan 2022 22:22:49 +0100 Subject: [PATCH] feat: migrate time --- src/index.ts | 2 ++ src/time.ts | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 src/time.ts diff --git a/src/index.ts b/src/index.ts index ea724d0440e..4bc422e0fe5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,7 @@ import { Datatype } from './datatype'; import { Mersenne } from './mersenne'; import { Random } from './random'; +import { Time } from './time'; export interface FakerOptions { locales?: string[]; @@ -17,6 +18,7 @@ export class Faker { readonly mersenne: Mersenne = new Mersenne(); random = new Random(this); + time: Time = new Time(); datatype: Datatype = new Datatype(this); constructor(opts: FakerOptions = {}) { diff --git a/src/time.ts b/src/time.ts new file mode 100644 index 00000000000..18969d6fa0e --- /dev/null +++ b/src/time.ts @@ -0,0 +1,26 @@ +export class Time { + /** + * recent + * + * @method faker.time.recent + * @param outputType 'abbr' || 'wide' || 'unix' (default choice) + */ + recent(outputType: 'abbr' | 'wide' | 'unix' = 'unix'): string | number { + let date: string | number | Date = new Date(); + + switch (outputType) { + case 'abbr': + date = date.toLocaleTimeString(); + break; + case 'wide': + date = date.toTimeString(); + break; + case 'unix': + // TODO @Shinigami92 2022-01-10: add default case + date = date.getTime(); + break; + } + + return date; + } +}