Repository: iamkun/dayjs

Problem statement:
diff() in "days" broken after making add() "days" aware of daylight-saving changes
Computing difference between two dates, which one was subtracted 1000 days from, returns 999 days, if the date is on the boundary of daylight-saving changes:

```
> d1 = dayjs('2018-10-28')
2018-10-27T22:00:00.000Z
> d2 = dayjs('2018-10-28').add(-1000, 'days')
2016-01-31T23:00:00.000Z
> d1.diff(d2, 'hours')
23999
> d1.diff(d2, 'days')
999
```

Moment.js computes the difference correctly. Notice, that the difference in hours is correct in Day.js too:

```
> m1 = moment('2018-10-28')
moment("2018-10-28T00:00:00.000")
> m1.toDate()
2018-10-27T22:00:00.000Z
> m2 = moment('2018-10-28').add(-1000, 'day')
moment("2016-02-01T00:00:00.000")
> m2.toDate()
2016-01-31T23:00:00.000Z
> m1.diff(m2, 'hours')
23999
> m1.diff(m2, 'days')
1000
```

This problem has been caused by 969acedc94461dca2b1d9313299a2c9db4aed7a9. It adds/subtracts days using the local time zone. Previously, days were added/subtracted in UTC (using UNIX time), like hours and shorter units. While it helps people, who were not aware of Day.js working in the local time zone (#262 and #329), it also made Day.js inconsistent, because computing difference in days is still done in UTC (using UNIX time).

Once `add()` and `subtract()` methods were modified to expect the day count in the local time zone, the `diff()` method should be modified to return the day count in the local time zone too.

Adding/subtracting hours and shorter units happens in UTC, computing the difference in these units happens in UTC as well. Handling hours and shorter units by Day.js is consistent.

The tests were executed in Node.js 8 in the time zone "Europe/Prague" using Day.js 1.7.7 and Moment.js 2.22.2.


Hints:
Related: Adding one `'day'` on a daylight-changing day is broken. It adds 24 hours, no matter if that day has 23, 24 or 25 hours.
```
let a = dayjs('2018-11-04');
console.log(a);
// h {$d: Sun Nov 04 2018 00:00:00 GMT-0400 (Eastern Daylight Time), $y: 2018, $M: 10, $D: 4, ...}
a = a.add(1, 'day');
console.log(a);
// h {$d: Sun Nov 04 2018 23:00:00 GMT-0500 (Eastern Standard Time), $y: 2018, $M: 10, $D: 4, …}
```

Patch:
diff --git a/src/constant.js b/src/constant.js
--- a/src/constant.js
+++ b/src/constant.js
@@ -34,4 +34,3 @@ export const en = {
   weekdays: 'Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday'.split('_'),
   months: 'January_February_March_April_May_June_July_August_September_October_November_December'.split('_')
 }
-
diff --git a/src/index.js b/src/index.js
--- a/src/index.js
+++ b/src/index.js
@@ -212,7 +212,6 @@ class Dayjs {
     return this
   }
 
-
   set(string, int) {
     return this.clone().$set(string, int)
   }
@@ -303,9 +302,16 @@ class Dayjs {
     })
   }
 
+  utcOffset() {
+    // Because a bug at FF24, we're rounding the timezone offset around 15 minutes
+    // https://github.com/moment/moment/pull/1871
+    return -Math.round(this.$d.getTimezoneOffset() / 15) * 15
+  }
+
   diff(input, units, float) {
     const unit = Utils.prettyUnit(units)
     const that = dayjs(input)
+    const zoneDelta = (that.utcOffset() - this.utcOffset()) * C.MILLISECONDS_A_MINUTE
     const diff = this - that
     let result = Utils.monthDiff(this, that)
 
@@ -313,8 +319,8 @@ class Dayjs {
       [C.Y]: result / 12,
       [C.M]: result,
       [C.Q]: result / 3,
-      [C.W]: diff / C.MILLISECONDS_A_WEEK,
-      [C.D]: diff / C.MILLISECONDS_A_DAY,
+      [C.W]: (diff - zoneDelta) / C.MILLISECONDS_A_WEEK,
+      [C.D]: (diff - zoneDelta) / C.MILLISECONDS_A_DAY,
       [C.H]: diff / C.MILLISECONDS_A_HOUR,
       [C.MIN]: diff / C.MILLISECONDS_A_MINUTE,
       [C.S]: diff / C.MILLISECONDS_A_SECOND


Test patch:
diff --git a/test/display.test.js b/test/display.test.js
--- a/test/display.test.js
+++ b/test/display.test.js
@@ -86,7 +86,6 @@ it('Format Second s ss SSS', () => {
   expect(dayjs(date).format('s-ss-SSS')).toBe(moment(date).format('s-ss-SSS'))
 })
 
-
 it('Format Time Zone ZZ', () => {
   MockDate.set(new Date('2018-05-02T23:00:00.000'), 60 * 8)
   expect(dayjs().format('Z')).toBe(moment().format('Z'))
@@ -104,9 +103,21 @@ it('Format Time Zone ZZ', () => {
 })
 
 it('Format ddd dd MMM with short locale', () => {
-  expect(dayjs().locale(th).format('dd')).toBe(moment().locale('th').format('dd'))
-  expect(dayjs().locale(th).format('ddd')).toBe(moment().locale('th').format('ddd'))
-  expect(dayjs().locale(th).format('MMM')).toBe(moment().locale('th').format('MMM'))
+  expect(dayjs()
+    .locale(th)
+    .format('dd')).toBe(moment()
+    .locale('th')
+    .format('dd'))
+  expect(dayjs()
+    .locale(th)
+    .format('ddd')).toBe(moment()
+    .locale('th')
+    .format('ddd'))
+  expect(dayjs()
+    .locale(th)
+    .format('MMM')).toBe(moment()
+    .locale('th')
+    .format('MMM'))
 })
 
 it('Format Complex with other string - : / ', () => {
@@ -170,7 +181,6 @@ describe('Difference', () => {
     })
   })
 
-
   it('MonthDiff', () => {
     expect(dayjs('2018-08-08').diff(dayjs('2018-08-08'), 'month')).toEqual(0)
     expect(dayjs('2018-09-08').diff(dayjs('2018-08-08'), 'month')).toEqual(1)
@@ -192,6 +202,11 @@ it('Days in Month', () => {
   expect(dayjs('20140201').daysInMonth()).toBe(moment('20140201').daysInMonth())
 })
 
+it('Utc Offset', () => {
+  expect(dayjs('2013-01-01T00:00:00.000').utcOffset()).toBe(moment('2013-01-01T00:00:00.000').utcOffset())
+  expect(dayjs('2013-01-01T05:00:00.000').utcOffset()).toBe(moment('2013-01-01T05:00:00.000').utcOffset())
+})
+
 it('As Javascript Date -> toDate', () => {
   const base = dayjs()
   const momentBase = moment()