iamkun / iamkun/dayjs

Behavior question, Timezone. First parameter in tz() already wrote timezone? 關於時區的行為問題

Open
#1,436 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
48.7k
Forks
2.5k
PR merge metrics
No merged PRs in 30d

Description

Hi,

### Updated#1
I found out `dayjs.tz()` do it on purpose, for the reason. So `dayjs.tz()` doesn't support first parameter with timezone.
https://github.com/iamkun/dayjs/blob/9c20e77caf7b1b5eccf418175203b198d4e29535/src/plugin/timezone/index.js#L3
```js
const typeToPos = {
year: 0,
month: 1,
day: 2,
hour: 3,
minute: 4,
second: 5
}
```

### Q1
If first parameter have been added -0400, matched timezone Etc/GMT+4
should function `fixOffset` be called?

```js
// notice the hour part

// situation 1 output: 2021-03-31T04:00:00-04:00
dayjs.tz('2021-03-31T00:00:00-0400', 'Etc/GMT+4').format();

// situation 2 output: 2021-03-31T00:00:00-04:00
dayjs.tz('2021-03-31T00:00:00', 'Etc/GMT+4').format();
```

In moment,
I've add -0400 in moment.tz, it will check needsOffset(), which return false for m._tzm is -240.

https://github.com/moment/moment-timezone/blob/ffc41900fed9efa9a31e87f0eadd66ec86b5046a/moment-timezone.js#L564
```js
// situation 3 output: 2021-03-31T00:00:00-04:00
moment.tz('2021-03-31T00:00:00-0400', 'Etc/GMT+4').format();

// situation 4 output: 2021-03-31T00:00:00-04:00
moment.tz('2021-03-31T00:00:00', 'Etc/GMT+4').format();
```

### Q2
And there is one other situation, time with different timezone.
```js
dayjs.tz('2021-03-31T00:00:00+0800', 'Etc/GMT+4').format(); // "2021-03-30T16:00:00-04:00"

moment.tz('2021-03-31T00:00:00+0800', 'Etc/GMT+4').format(); // "2021-03-30T12:00:00-04:00"
```

### question is
How would it be designed in dayjs?

=====
你好,

假設系統是北京時間。

在第一個情況可以看到因為多寫了 -0400,因此`new Date()`解析後,把時區差也給算上了,但後續的`fixOffset`為了補上時間差,反而造成了跟 moment 不一樣的結果,因為 moment 有多一層確認。

在第二種情況,多寫了北京時區+0800,tz 第二個參數帶入美東時區,亦跟 moment 不一樣

並不是說全然跟 moment 一致就是對的

只是好奇的是,這是故意被設計成這樣的嗎,或是其中有什麼概念在裡頭,想了解。

感謝

Contributor guide

Open the contributing guide

Research direction

Start with src/plugin/timezone/index.js at the linked typeToPos and fixOffset logic, then compare the four Day.js and Moment timezone examples in the issue. Determine the intended behavior for explicit offsets, matching offsets, and different timezones; done requires a clear design decision for these cases, supported by the project’s timezone behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
internationalization
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.