Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
FPNL opened this issue Mar 31, 2021 · 0 comments

Comments

@FPNL
Copy link

FPNL commented Mar 31, 2021

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.

const typeToPos = {

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?

// 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

// 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.

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 一致就是對的

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

感謝

@FPNL FPNL changed the title Behavior question, Timezone. First parameter in tz() already wrote timezone? Behavior question, Timezone. First parameter in tz() already wrote timezone? 關於時區的行為問題 Mar 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant