utc.utcOffset does not return a new instance when keepLocalTime is set to true
- Dominant language
- JavaScript
- Stars
- 48.7k
- Forks
- 2.5k
- PR merge metrics
- No merged PRs in 30d
Description
**Describe the bug**
`utcOffset(value, keepLocalTime)` method of `utc` plugin is not returning a new `Dayjs` instance when `keepLocalTime = true`. This then breaks everything, because the internal variable of the original instance are not properly re-initialized with the new `$offset` and `$u` variables. This also breaks `startOf` in `timezone` plugin and probably more?
```typescript
/**
* EXPECTED SYSTEM DATE SETTINGS:
* DATE: 2022-02-10T10:00:00Z
* TIMEZONE: Europe/Prague (GMT+1)
*/
const dayjs = require('dayjs')
const utc = require('dayjs/plugin/utc')
dayjs.extend(utc)
const date = dayjs('2022-02-10T10:00:00Z')
const dateWithZero = date.utcOffset(0, true)
console.log(date.format()) // incorrect - 2022-02-10T11:00:00Z, expected 2022-02-10T11:00:00+01:00
console.log(dateWithZero.format()) // correct - 2022-02-10T11:00:00Z
console.log(dateWithZero.add(1, 'hour').format()) // incorrect - 2022-02-10T11:00:00Z, expected 2022-02-10T12:00:00Z
console.log(date === dateWithZero) // incorrect - true
```
The issue is here, the `inc` should probably be cloned at line 92? Not sure.
https://github.com/iamkun/dayjs/blob/4a7b7d07c885bb9338514c234dbb708e24e9863e/src/plugin/utc/index.js#L90-L95
**Expected behavior**
New `Dayjs` instance with proper internal variables should be returned.
**Information**
- Day.js Version 1.10.7
- Time zone: GMT+1
- broken since https://github.com/iamkun/dayjs/commit/9f488e5aca92f0b4c2951459436829d79f86d8d7
Contributor guide
Research direction
The affected entry point is src/plugin/utc/index.js around lines 90-95; reproduce the provided utcOffset(0, true) example and inspect how the instance is handled there. Done means the call returns a distinct Dayjs instance with correct offset state, and the shown format and add behavior match the expected results.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- api
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 52/100