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..3fab13494 100644 --- a/src/index.js +++ b/src/index.js @@ -1,13 +1,32 @@ -class Moment { - constructor(config = new Date().getTime()) { - this.date = new Date(config) +class Dayjs { + 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() + return this.date.toUTCString() } } export default config => ( - new Moment(config) + new Dayjs(config) ) diff --git a/test/index.test.js b/test/index.test.js deleted file mode 100644 index 1bb1344b3..000000000 --- a/test/index.test.js +++ /dev/null @@ -1,5 +0,0 @@ -import moment from '../src' - -test('toString', () => { - expect(moment().toString()).toBe((new Date()).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