Skip to content

Commit

Permalink
Merge branch 'master' into combined
Browse files Browse the repository at this point in the history
  • Loading branch information
prantlf committed Sep 27, 2018
2 parents bdc37a8 + 88548e9 commit 75c099c
Show file tree
Hide file tree
Showing 12 changed files with 255 additions and 23 deletions.
15 changes: 14 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,24 @@

# [1.8.0](https://github.com/prantlf/dayjs/compare/v1.7.5...v1.8.0) (2018-09-13)


### Features

* Introduce dayjs-ext - fork of days with extensions ([4ab9f9a](https://github.com/prantlf/dayjs/commit/4ab9f9a))

## [1.7.7](https://github.com/iamkun/dayjs/compare/v1.7.6...v1.7.7) (2018-09-26)


### Bug Fixes

* **DST:** fix daylight saving time DST bug && add test ([#354](https://github.com/iamkun/dayjs/issues/354)) ([6fca6d5](https://github.com/iamkun/dayjs/commit/6fca6d5))

## [1.7.6](https://github.com/iamkun/dayjs/compare/v1.7.5...v1.7.6) (2018-09-25)


### Bug Fixes

* **add dayjs.unix:** add dayjs.unix to parse timestamp in seconds && locale update ([5711c5e](https://github.com/iamkun/dayjs/commit/5711c5e))

## [1.7.5](https://github.com/iamkun/dayjs/compare/v1.7.4...v1.7.5) (2018-08-10)


Expand Down
23 changes: 18 additions & 5 deletions docs/en/API-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ The `Dayjs` object is immutable, that is, all API operations that change the `Da
- [Parsing](#parsing)
- [Constructor `dayjs(existing?: string | number | Date | Dayjs)`](#constructor-dayjsexisting-string--number--date--dayjs)
- [ISO 8601 string](#iso-8601-string)
- [Unix Timestamp (milliseconds since the Unix Epoch - Jan 1 1970, 12AM UTC)](#unix-timestamp-milliseconds-since-the-unix-epoch---jan-1-1970-12am-utc)
- [Native Javascript Date object](#native-javascript-date-object)
- [Clone `.clone() | dayjs(original: Dayjs)`](#clone-clone-dayjsoriginal-dayjs)
- [Unix Timestamp (milliseconds)](#unix-timestamp-milliseconds)
- [Unix Timestamp (seconds)](#unix-timestamp-seconds-unixvalue-number)
- [Clone `.clone() | dayjs(original: Dayjs)`](#clone-clone--dayjsoriginal-dayjs)
- [Validation `.isValid()`](#validation-isvalid)
- [Get and Set](#get-and-set)
- [Year `.year()`](#year-year)
Expand All @@ -22,6 +23,7 @@ The `Dayjs` object is immutable, that is, all API operations that change the `Da
- [Second `.second()`](#second-second)
- [Millisecond `.millisecond()`](#millisecond-millisecond)
- [Set `.set(unit: string, value: number)`](#set-setunit-string-value-number)
- [List of all available units](#list-of-all-available-units)
- [Manipulating](#manipulating)
- [Add `.add(value: number, unit: string)`](#add-addvalue-number-unit-string)
- [Subtract `.subtract(value: number, unit: string)`](#subtract-subtractvalue-number-unit-string)
Expand Down Expand Up @@ -75,16 +77,27 @@ Day.js also parses other date formats.
dayjs('2018-04-04T16:00:00.000Z');
```

#### Unix Timestamp (milliseconds since the Unix Epoch - Jan 1 1970, 12AM UTC)
#### Native Javascript Date object

```js
dayjs(new Date(2018, 8, 18));
```

#### Unix Timestamp (milliseconds)

Returns a `Dayjs` from a Unix timestamp (milliseconds since the Unix Epoch)

```js
dayjs(1318781876406);
```

#### Native Javascript Date object
### Unix Timestamp (seconds) `.unix(value: number)`

Returns a `Dayjs` from a Unix timestamp (seconds since the Unix Epoch)

```js
dayjs(new Date(2018, 8, 18));
dayjs.unix(1318781876);
dayjs.unix(1318781876.721);
```

### Clone `.clone() | dayjs(original: Dayjs)`
Expand Down
21 changes: 15 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,23 @@ class Dayjs {
const date = this.set(C.DATE, 1).set(u, n + number)
return date.set(C.DATE, Math.min(this.$D, date.daysInMonth()))
}
const instanceFactorySet = (n) => {
const date = new Date(this.$d)
date.setDate(date.getDate() + (n * number))
return wrapper(date, this)
}
if (unit === C.M) {
return instanceFactory(C.M, this.$M)
}
if (unit === C.Y) {
return instanceFactory(C.Y, this.$y)
}
if (unit === C.D) {
return instanceFactorySet(1)
}
if (unit === C.W) {
return instanceFactorySet(7)
}
let step
switch (unit) {
case C.MIN:
Expand All @@ -301,12 +312,6 @@ class Dayjs {
case C.H:
step = C.MILLISECONDS_A_HOUR
break
case C.D:
step = C.MILLISECONDS_A_DAY
break
case C.W:
step = C.MILLISECONDS_A_WEEK
break
case C.S:
step = C.MILLISECONDS_A_SECOND
break
Expand Down Expand Up @@ -517,6 +522,10 @@ dayjs.locale = parseLocale

dayjs.isDayjs = isDayjs

dayjs.unix = timestamp => (
dayjs(timestamp * 1e3)
)

dayjs.en = Ls[L]

export default dayjs
39 changes: 39 additions & 0 deletions src/locale/fi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import dayjs from 'dayjs'

const locale = {
name: 'fi', // Finnish
weekdays: 'sunnuntai_maanantai_tiistai_keskiviikko_torstai_perjantai_lauantai'.split('_'), // Note weekdays are not capitalized in Finnish
weekdaysShort: 'su_ma_ti_ke_to_pe_la'.split('_'), // There is no short form of weekdays in Finnish except this 2 letter format so it is used for both 'weekdaysShort' and 'weekdaysMin'
weekdaysMin: 'su_ma_ti_ke_to_pe_la'.split('_'),
months: 'tammikuu_helmikuu_maaliskuu_huhtikuu_toukokuu_kesäkuu_heinäkuu_elokuu_syyskuu_lokakuu_marraskuu_joulukuu'.split('_'), // Note month names are not capitalized in Finnish
monthsShort: 'tammikuu_helmikuu_maaliskuu_huhtikuu_toukokuu_kesäkuu_heinäkuu_elokuu_syyskuu_lokakuu_marraskuu_joulukuu'.split('_'), // There is no short form of months in Finnish so just use the regular long form always
ordinal: n => `${n}.`,
/*
* This relativeTime is currently configured for having proper past
* tense forms since Finnish needs a separate version for future tense
* and I think past tense is a more common use case for this kind of
* library.
*
* Doing this properly requires this issue to be fixed:
* https://github.com/iamkun/dayjs/issues/302
*/
relativeTime: {
future: '%s kuluttua',
past: '%s sitten',
s: 'muutama sekunti', // for past tense
m: 'minuutti', // for past tense
mm: '%d minuuttia', // for past tense
h: 'tunti', // for past tense
hh: '%d tuntia', // for past tense
d: 'päivä', // for past tense
dd: '%d päivää', // for past tense
M: 'kuukausi', // for past tense
MM: '%d kuukautta', // for past tense
y: 'vuosi', // for past tense
yy: '%d vuotta' // for past tense
}
}

dayjs.locale(locale, null, true)

export default locale
30 changes: 30 additions & 0 deletions src/locale/hu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import dayjs from 'dayjs'

const locale = {
name: 'hu',
weekdays: 'vasárnap_hétfő_kedd_szerda_csütörtök_péntek_szombat'.split('_'),
weekdaysShort: 'vas_hét_kedd_sze_csüt_pén_szo'.split('_'),
weekdaysMin: 'v_h_k_sze_cs_p_szo'.split('_'),
months: 'január_február_március_április_május_június_július_augusztus_szeptember_október_november_december'.split('_'),
monthsShort: 'jan_feb_márc_ápr_máj_jún_júl_aug_szept_okt_nov_dec'.split('_'),
ordinal: n => `${n}.`,
relativeTime: {
future: '%s múlva',
past: '%s',
s: 'néhány másodperc',
m: 'egy perc',
mm: '%d perc',
h: 'egy óra',
hh: '%d óra',
d: 'egy nap',
dd: '%d nap',
M: 'egy hónap',
MM: '%d hónap',
y: 'egy éve',
yy: '%d év'
}
}

dayjs.locale(locale, null, true)

export default locale
22 changes: 11 additions & 11 deletions src/locale/ko.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ const locale = {
LLLL: 'YYYY년 MMMM D일 dddd A h:mm'
},
relativeTime: {
future: '%s 이내',
future: '%s ',
past: '%s 전',
s: '몇 초',
m: '1 분',
mm: '%d 분',
h: '1 시간',
hh: '%d 시간',
d: '1 일',
dd: '%d 일',
M: '1 개월',
MM: '%d 개월',
y: '1 년',
yy: '%d 년'
m: '1분',
mm: '%d분',
h: ' 시간',
hh: '%d시간',
d: '하루',
dd: '%d일',
M: '한 달',
MM: '%d달',
y: ' 년',
yy: '%d년'
}
}

Expand Down
30 changes: 30 additions & 0 deletions src/locale/nn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import dayjs from 'dayjs'

const locale = {
name: 'nn',
weekdays: 'sundag_måndag_tysdag_onsdag_torsdag_fredag_laurdag'.split('_'),
weekdaysShort: 'sun_mån_tys_ons_tor_fre_lau'.split('_'),
weekdaysMin: 'su_må_ty_on_to_fr_la'.split('_'),
months: 'januar_februar_mars_april_mai_juni_juli_august_september_oktober_november_desember'.split('_'),
monthsShort: 'jan_feb_mar_apr_mai_jun_jul_aug_sep_okt_nov_des'.split('_'),
ordinal: n => `${n}.`,
relativeTime: {
future: 'om %s',
past: 'for %s sidan',
s: 'nokre sekund',
m: 'eitt minutt',
mm: '%d minutt',
h: 'ein time',
hh: '%d timar',
d: 'ein dag',
dd: '%d dagar',
M: 'ein månad',
MM: '%d månadar',
y: 'eitt år',
yy: '%d år'
}
}

dayjs.locale(locale, null, true)

export default locale
27 changes: 27 additions & 0 deletions src/locale/pl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import dayjs from 'dayjs'

const locale = {
name: 'pl',
weekdays: 'Niedziela_Poniedziałek_Wtorek_Środa_Czwartek_Piątek_Sobota'.split('_'),
months: 'Styczeń_Luty_Marzec_Kwiecień_Maj_Czerwiec_Lipiec_Sierpień_Wrzesień_Październik_Listopad_Grudzień'.split('_'),
ordinal: n => `${n}.`,
relativeTime: {
future: 'za %s',
past: 'po %s',
s: 'kilka Sekund',
m: 'Minuta',
mm: '%d Minut',
h: 'Godzina',
hh: '%d Godzin',
d: 'Tydzień',
dd: '%d Tygodni',
M: 'Miesiąc',
MM: '%d Miesięcy',
y: 'Rok',
yy: '%d Lat'
}
}

dayjs.locale(locale, null, true)

export default locale
27 changes: 27 additions & 0 deletions src/locale/sr-cyrl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import dayjs from 'dayjs'

const locale = {
name: 'sr-cyrl',
weekdays: 'Недеља_Понедељак_Уторак_Среда_Четвртак_Петак_Субота'.split('_'),
months: 'Јануар_Фебруар_Март_Април_Мај_Јун_Јул_Август_Септембар_Октобар_Новембар_Децембар'.split('_'),
relativeTime: {
future: 'за %s',
past: 'пре %s',
s: 'секунда',
m: 'минут',
mm: '%d минута',
h: 'сат',
hh: '%d сати',
d: 'дан',
dd: '%d дана',
M: 'месец',
MM: '%d месеци',
y: 'година',
yy: '%d године'
},
ordinal: n => `${n}.`
}

dayjs.locale(locale, null, true)

export default locale
27 changes: 27 additions & 0 deletions src/locale/sr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import dayjs from 'dayjs'

const locale = {
name: 'sr',
weekdays: 'Nedelja_Ponedeljak_Utorak_Sreda_Četvrtak_Petak_Subota'.split('_'),
months: 'Januar_Februar_Mart_April_Maj_Jun_Jul_Avgust_Septembar_Oktobar_Novembar_Decembar'.split('_'),
relativeTime: {
future: 'za %s',
past: 'pre %s',
s: 'sekunda',
m: 'minut',
mm: '%d minuta',
h: 'sat',
hh: '%d sati',
d: 'dan',
dd: '%d dana',
M: 'mesec',
MM: '%d meseci',
y: 'godina',
yy: '%d godine'
},
ordinal: n => `${n}.`
}

dayjs.locale(locale, null, true)

export default locale
10 changes: 10 additions & 0 deletions test/manipulate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,13 @@ it('Add Time days', () => {
it('Subtract Time days', () => {
expect(dayjs().subtract(1, 'days').valueOf()).toBe(moment().subtract(1, 'days').valueOf())
})

it('Add Time days (DST)', () => {
// change timezone before running test
// New Zealand (-720)
expect(dayjs('2018-04-01').add(1, 'd').format()).toBe(moment('2018-04-01').add(1, 'd').format())
expect(dayjs('2018-03-28').add(1, 'w').format()).toBe(moment('2018-03-28').add(1, 'w').format())
// London (-60)
expect(dayjs('2018-10-28').add(1, 'd').format()).toBe(moment('2018-10-28').add(1, 'd').format())
expect(dayjs('2018-10-26').add(1, 'w').format()).toBe(moment('2018-10-26').add(1, 'w').format())
})
7 changes: 7 additions & 0 deletions test/parse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ it('Unix Timestamp Number (milliseconds) 1523520536000', () => {
expect(dayjs(timestamp).valueOf()).toBe(moment(timestamp).valueOf())
})

it('Unix Timestamp Number (seconds) 1318781876', () => {
const timestamp1 = 1318781876
const timestamp2 = 1318781876.721
expect(dayjs.unix(timestamp1).valueOf()).toBe(moment.unix(timestamp1).valueOf())
expect(dayjs.unix(timestamp2).valueOf()).toBe(moment.unix(timestamp2).valueOf())
})

it('String and Number 20180101', () => {
expect(dayjs(20180101).valueOf()).toBe(moment(20180101).valueOf())
expect(dayjs('20180101').valueOf()).toBe(moment('20180101').valueOf())
Expand Down

0 comments on commit 75c099c

Please sign in to comment.