Move date formatter to format (#534)

* Move date formatter to format

* Cleanup padding
This commit is contained in:
Jaco Greeff
2020-02-15 11:24:16 +01:00
committed by GitHub
parent 78e39dcf34
commit 81a31519db
5 changed files with 38 additions and 12 deletions
+1
View File
@@ -1,5 +1,6 @@
# 2.3.0-beta.x
- Remove dependency on moment.js, with internal format function (Thanks to https://github.com/AndreasGassmann)
- Move TypeScript `@types/*` to dev deps (Thanks to https://github.com/AndreasGassmann)
- Move to `@polkadot/wasm-crypto` 1.0
- Cleanup dependencies to pave the way for using yarn2 (Explicit instead of implicit)
@@ -0,0 +1,16 @@
// Copyright 2017-2020 @polkadot/util authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import formatDate from './formatDate';
describe('formatDate', (): void => {
it('formats a known date into the correct format', (): void => {
const date = new Date(Date.UTC(2020, 1, 15, 11, 14, 34));
// make this test-locale agnostic
date.setTime(date.getTime() + date.getTimezoneOffset() * 60 * 1000);
expect(formatDate(date)).toEqual('2020-02-15 11:14:34');
});
});
+19
View File
@@ -0,0 +1,19 @@
// Copyright 2017-2020 @polkadot/util authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
/** @internal */
function zeroPad (value: number): string {
return value.toString().padStart(2, '0');
}
export default function formatDate (date: Date): string {
const year = date.getFullYear().toString();
const month = zeroPad((date.getMonth() + 1));
const day = zeroPad(date.getDate());
const hour = zeroPad(date.getHours());
const minute = zeroPad(date.getMinutes());
const second = zeroPad(date.getSeconds());
return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
}
+1
View File
@@ -3,6 +3,7 @@
// of the Apache-2.0 license. See the LICENSE file for details.
export { default as formatBalance } from './formatBalance';
export { default as formatDate } from './formatDate';
export { default as formatDecimal } from './formatDecimal';
export { default as formatElapsed } from './formatElapsed';
export { default as formatNumber } from './formatNumber';
+1 -12
View File
@@ -6,6 +6,7 @@ import { Logger, Logger$Data } from './types';
import chalk from 'chalk';
import formatDate from './format/formatDate';
import isBn from './is/bn';
import isBuffer from './is/buffer';
import isFunction from './is/function';
@@ -69,18 +70,6 @@ export function format (value: any): any {
return value;
}
function formatDate (date: Date): string {
const year = date.getFullYear().toString();
const month = (date.getMonth() + 1).toString().padStart(2, '0');
const day = date.getDate().toString().padStart(2, '0');
const hour = date.getHours().toString().padStart(2, '0');
const minute = date.getMinutes().toString().padStart(2, '0');
const second = date.getSeconds().toString().padStart(2, '0');
return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
}
function apply (log: LogType, type: string, values: Logger$Data): void {
if (values.length === 1 && isFunction(values[0])) {
const fnResult = (values[0] as Function)();