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

app: fix wallet birthday is UTC date #2236

Merged
merged 1 commit into from Mar 24, 2023

Conversation

chappjc
Copy link
Member

@chappjc chappjc commented Mar 21, 2023

Resolves #2192

When using a <input type='date'> element, it is tricky to get the time zone correct when using the value, valueAsNumber, and valueAsDate fields. This is because the browser will always display the date as UTC, regardless of how you have set valueAsDate. Also, when getting the date out with new Date(input.value), where the value is yyyy-mm-dd, the Date parses as in UTC (12am UTC instead of local).

When the user is interacting with the input element, they are thinking in local time. However, unless we offset the time that we set, it can show an incorrect date. This change sets the value date string manually to the local date. We could also offset the Date that we assign to valueAsDate so that it will show a local date, or similarly with valueAsNumber, but this is harder to follow and we do not benefit from finer precision than simply 12am of the current day.

When reading the selected date back into javascript, we also have to interpret it as a local date string. We do this by forcing the Date constructor to interpret the date as local by appending "T00:00" to the date string from input.value. We also must use this trick when reading the min and max date strings. We could instead use valueAsDate and manually shift in the opposite direction as when we set it, but this seems better.

See https://dev.to/mendyberger/input-and-js-dates-2lhc for a better explanation

@chappjc chappjc force-pushed the ui-wallet-bday-local branch 3 times, most recently from 74fe56d to a538357 Compare March 21, 2023 20:05
@chappjc
Copy link
Member Author

chappjc commented Mar 21, 2023

Seems to be working as expected, but I'm going to try again in a couple hours when it's tomorrow in GMT.

@chappjc chappjc marked this pull request as ready for review March 22, 2023 00:08
@chappjc
Copy link
Member Author

chappjc commented Mar 22, 2023

No longer getting a future date when it's tomorrow in GMT but not locally. Wallet backend gets the right date as well, logged as 2023-03-21 00:00:00 -0500 CDT 1679374800 (beginning of current day).

@chappjc chappjc added this to the 0.6 milestone Mar 22, 2023
Copy link
Member

@JoeGruffins JoeGruffins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you change the btc or dcr wallet from rpc to spv the date is not current. It seems like something random. Is this date intended?

Just created rpc wallet on simnet changing to spv:

image

@chappjc
Copy link
Member Author

chappjc commented Mar 22, 2023

If you change the btc or dcr wallet from rpc to spv the date is not current. It seems like something random. Is this date intended?

Yeah, sorta intended. @martonp would probably be most familiar with the logic, but that may 27, 2022 day is the default birthday under certain conditions. Perhaps the workflow you described of switching wallet types evades the logic that detects the "is seeded" check that should use a more recent time.

Copy link
Contributor

@martonp martonp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

When using a <input type='date'> element, it is tricky to get the time
zone correct when using the value, valueAsNumber, and valueAsDate
fields. This is because the browser will always display the date as UTC,
regardless of how you have set valueAsDate. Also, when getting the date
out with new Date(input.value), where the value is yyyy-mm-dd, the Date
parses as in UTC.

When the user is interacting with the input element, they are thinking
in local time. However, unless we offset the time that we set, it can
show an incorrect date. This change sets the value date string manually
to the local date. We could also offsets the Date that we assign to
valueAsDate so that it will show a local date, or similarly with
valueAsNumber, but this is harder to follow and we do not benefit from
finer precision that simply 12am of the current day.

When reading the selected date back into javascript, we also have to
interpret it as a local date string. We do this by forcing the Date
constructor to interpret the date as local by appending "T00:00" to the
date string from input.value. We also must use this trick when reading
the min and max date strings. We could instead use valueAsDate and
manually shift in the opposite direction as when we set it, but this
seems better.
@chappjc chappjc merged commit 4519576 into decred:master Mar 24, 2023
5 checks passed
@chappjc chappjc deleted the ui-wallet-bday-local branch March 24, 2023 21:32
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

Successfully merging this pull request may close these issues.

wallet birthday at creation assumes GMT (just UI or backend too?)
4 participants