Skip to content

Commit

Permalink
allow date formatting with custom timezone
Browse files Browse the repository at this point in the history
  • Loading branch information
Leo4815162342 committed Feb 28, 2024
1 parent 7126679 commit f8cebae
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
17 changes: 13 additions & 4 deletions src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ import chalk from 'chalk';
import debug from 'debug';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import tz from 'dayjs/plugin/timezone';

import { version } from '../../package.json';
import { BatchStreamWriter } from '../stream-writer';
import { BufferObject } from '../buffer-fetcher/types';
import { formatTimeDuration } from '../utils/formatTimeDuration';

dayjs.extend(utc);
dayjs.extend(tz);

const DEBUG_NAMESPACE = 'dukascopy-node:cli';

Expand Down Expand Up @@ -52,7 +54,8 @@ export async function run(argv: NodeJS.Process['argv']) {
retryOnEmpty,
pauseBetweenRetriesMs,
fileName: customFileName,
dateFormat
dateFormat,
timeZone
} = input;

if (isDebugActive) {
Expand Down Expand Up @@ -196,9 +199,15 @@ export async function run(argv: NodeJS.Process['argv']) {
processedBatch,
dateFormat
? timeStamp => {
return dateFormat === 'iso'
? new Date(timeStamp).toISOString()
: dayjs(timeStamp).utc().format(dateFormat);
if (dateFormat === 'iso') {
return new Date(timeStamp).toISOString();
}

if (timeZone) {
return dayjs(timeStamp).tz(timeZone).format(dateFormat);
}

return dayjs(timeStamp).utc().format(dateFormat);
}
: undefined
);
Expand Down
8 changes: 6 additions & 2 deletions src/cli/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface CliConfig extends ConfigBase {
inline: boolean;
fileName: string;
dateFormat: string;
timeZone: string;
}

const now = 'now';
Expand Down Expand Up @@ -46,6 +47,7 @@ const commanderSchema = program
.option('-ch, --cache', 'Use cache', false)
.option('-chpath, --cache-path <value>', 'Folder path for cache data', './.dukascopy-cache')
.option('-df, --date-format <value>', 'Date format', '')
.option('-tz, --time-zone <value>', 'Timezone', '')
.option('-r, --retries <value>', 'Number of retries for a failed artifact download', Number, 0)
.option('-rp, --retry-pause <value>', 'Pause between retries in milliseconds', Number, 500)
.option(
Expand Down Expand Up @@ -98,7 +100,8 @@ export function getConfigFromCliArgs(argv: NodeJS.Process['argv']) {
debug: options.debug,
inline: options.inline,
fileName: options.fileName,
dateFormat: options.dateFormat
dateFormat: options.dateFormat,
timeZone: options.timeZone
};

const cliSchema: InputSchema<CliConfig> = {
Expand All @@ -109,7 +112,8 @@ export function getConfigFromCliArgs(argv: NodeJS.Process['argv']) {
debug: { type: 'boolean', required: false } as RuleBoolean,
inline: { type: 'boolean', required: false } as RuleBoolean,
fileName: { type: 'string', required: false } as RuleString,
dateFormat: { type: 'string', required: false } as RuleString
dateFormat: { type: 'string', required: false } as RuleString,
timeZone: { type: 'string', required: false } as RuleString
}
};

Expand Down

0 comments on commit f8cebae

Please sign in to comment.