Skip to content

Commit

Permalink
Merge pull request #8 from xx45/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
iamkun authored Apr 11, 2018
2 parents 6bc6aae + 9eaaee6 commit 0eaf070
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 10 deletions.
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
29 changes: 24 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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)
)
5 changes: 0 additions & 5 deletions test/index.test.js

This file was deleted.

10 changes: 10 additions & 0 deletions test/parse.test.js
Original file line number Diff line number Diff line change
@@ -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());
});

0 comments on commit 0eaf070

Please sign in to comment.