From 503ce793e8467c5f5bb08df4b8e43b0333f51a32 Mon Sep 17 00:00:00 2001 From: iamkun Date: Wed, 11 Apr 2018 15:56:25 +0800 Subject: [PATCH 1/2] test: new test method use moment --- package-lock.json | 6 ++++++ package.json | 1 + src/index.js | 6 +++--- test/index.test.js | 5 +++-- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/package-lock.json b/package-lock.json index f36f4b365..368debec3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5352,6 +5352,12 @@ } } }, + "moment": { + "version": "2.22.0", + "resolved": "http://registry.npm.taobao.org/moment/download/moment-2.22.0.tgz", + "integrity": "sha1-eSGt4BAX3UUYbn/uX0JPC4ZjpzA=", + "dev": true + }, "ms": { "version": "2.0.0", "resolved": "http://registry.npm.taobao.org/ms/download/ms-2.0.0.tgz", diff --git a/package.json b/package.json index e5cf70d62..4d15ec883 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "eslint-config-airbnb-base": "^12.1.0", "eslint-plugin-import": "^2.10.0", "jest": "^22.4.3", + "moment": "^2.22.0", "pre-commit": "^1.2.2" } } diff --git a/src/index.js b/src/index.js index d36a29d70..580f0b04f 100644 --- a/src/index.js +++ b/src/index.js @@ -1,13 +1,13 @@ -class Moment { +class Dayjs { constructor(config = new Date().getTime()) { this.date = new Date(config) } toString() { - return this.date.toString() + return this.date.toString().replace(' (CST)', '') } } export default config => ( - new Moment(config) + new Dayjs(config) ) diff --git a/test/index.test.js b/test/index.test.js index 1bb1344b3..b635f87df 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -1,5 +1,6 @@ -import moment from '../src' +import moment from 'moment' +import dayjs from '../src' test('toString', () => { - expect(moment().toString()).toBe((new Date()).toString()); + expect(dayjs().toString()).toBe(moment().toString()); }); \ No newline at end of file From 9eaaee6564c3f111c479459af83d218a1e1bbff3 Mon Sep 17 00:00:00 2001 From: iamkun Date: Wed, 11 Apr 2018 16:27:32 +0800 Subject: [PATCH 2/2] feat(unix): .unix() to unix timestamp --- src/index.js | 25 ++++++++++++++++++++++--- test/index.test.js | 6 ------ test/parse.test.js | 10 ++++++++++ 3 files changed, 32 insertions(+), 9 deletions(-) delete mode 100644 test/index.test.js create mode 100644 test/parse.test.js diff --git a/src/index.js b/src/index.js index 580f0b04f..3fab13494 100644 --- a/src/index.js +++ b/src/index.js @@ -1,10 +1,29 @@ class Dayjs { - constructor(config = new Date().getTime()) { - this.date = new Date(config) + constructor(config) { + this.utc = false + const args = this.parseConfig(config) + this.date = new Date(args) + } + + parseConfig(config) { + if (!config) return new Date() + if (/^(\d){8}$/.test(config)) { + this.utc = true + const y = config.substr(0, 4) + const m = config.substr(4, 2) + const d = config.substr(6, 2) + return `${y}-${m}-${d}` + } + return false + } + + unix() { + const zonePad = this.utc ? this.date.getTimezoneOffset() * 60 * 1000 : 0 + return Math.floor((this.date.getTime() + zonePad) / 1000) } toString() { - return this.date.toString().replace(' (CST)', '') + return this.date.toUTCString() } } diff --git a/test/index.test.js b/test/index.test.js deleted file mode 100644 index b635f87df..000000000 --- a/test/index.test.js +++ /dev/null @@ -1,6 +0,0 @@ -import moment from 'moment' -import dayjs from '../src' - -test('toString', () => { - expect(dayjs().toString()).toBe(moment().toString()); -}); \ No newline at end of file diff --git a/test/parse.test.js b/test/parse.test.js new file mode 100644 index 000000000..e503ef1b6 --- /dev/null +++ b/test/parse.test.js @@ -0,0 +1,10 @@ +import moment from 'moment' +import dayjs from '../src' + +test('Now', () => { + expect(dayjs().unix()).toBe(moment().unix()); +}); + +test('String 20130208', () => { + expect(dayjs('20130208').unix()).toBe(moment('20130208').unix()); +}); \ No newline at end of file