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

refactor(timestamp): remove redundant .UTC() call and mentions #108

Closed
wants to merge 2 commits into from

Conversation

scop
Copy link
Contributor

@scop scop commented Sep 8, 2023

Unix timestamps are always from UTC epoch, and calculation from given time.Time is independent of its location.

@peterbourgon
Copy link
Member

I'm happy to accept a change that removes the call to UTC in Timestamp, can you add a benchmark that demonstrates the benefit? But I'm not sure it's common knowledge that "Unix timestamp" means normalizing to UTC, so I'd prefer that bit of information to remain in the doc comment.

@scop scop force-pushed the refactor/unix-timestamp-utc branch from bcd6985 to 461ae4e Compare September 10, 2023 18:42
@scop
Copy link
Contributor Author

scop commented Sep 10, 2023

To me, mentioning UTC the way it currently is in the docs casts a shadow of doubt on the underlying implementation, e.g. whether Now() would (incorrectly) be somehow affected by the system time zone (which it isn't). To me, fixing this by removing that part of the comment is the most important part of this PR because it is about semantic correctness, which should be reflected in the comments.

Performance improvements are a secondary concern, but there are some benefits there, too. Benchmark added in 461ae4e.

@scop
Copy link
Contributor Author

scop commented Sep 10, 2023

I'd like also to point out that neither MaxTime(), Timestamp(Time), Time(uint64), or SetTime(uint64) mention UTC in any way in their docs, which is all fine. Removing the (unnecessary) mention from Now() brings it in line with these.

@ross-spencer
Copy link

Distant observer here, I wanted to verify the assertions here, and after writing some code to verify for myself I can see the information is accurate. Personally, however, I also appreciate it when the docs are explicit about UTC. It's a tricky one that catches folk out.

NB. as I was writing the above tests, I noticed, Go 1.17 introduced a timestamp in milliseconds which may also simplify some of the code in this package. May also be worth thinking about if the version can be upped at all.

UnixMilli: https://pkg.go.dev/time#UnixMilli

@peterbourgon
Copy link
Member

peterbourgon commented Sep 12, 2023

whether Now() would (incorrectly) be somehow affected by the system time zone (which it isn't). To me, fixing this by removing that part of the comment is the most important part of this PR because it is about semantic correctness, which should be reflected in the comments.

It's important to make clear that ULID timestamps are always UTC. It's not obvious that e.g. ulid.Now will produce UTC timestamps, because it's not obvious that Unix timestamps are always UTC.

I'm fine to remove the call to UTC() in the implementation, but rather than removing the note about UTC in the docs for that one function, I think we should actually add a note about UTC to the docs of every function that produces timestamps.

Unix timestamps are always from UTC epoch, and calculation from given
`time.Time` is independent of its location.
@scop scop force-pushed the refactor/unix-timestamp-utc branch from 461ae4e to a66f10a Compare September 12, 2023 19:09
@scop
Copy link
Contributor Author

scop commented Sep 12, 2023

it's not obvious that Unix timestamps are always UTC.

I disagree with this and the conclusion to add more redundant information to docs due to reasons stated earlier. To me, the definition of Unix time is clear about its relation to UTC.

Anyway, rebased and removed the contentious doc tweak, I'll leave addressing the docs to someone else.

ulid_test.go Outdated Show resolved Hide resolved
@scop scop force-pushed the refactor/unix-timestamp-utc branch from df7c380 to 4147f3e Compare September 13, 2023 03:59
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.

3 participants