-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
time: set specific rule for Date on daylight savings transitions #24551
Comments
Go chooses PDT to parse time, which is not wrong. I don't think this is a bug. |
The specific time does not exist. The documentation for |
I would like this issue to be reopened, as I have reasons other than those addressed in the comments above. Specifically, I am not saying that the behavior does not conform to the documentation and is therefore a bug. I am saying that I'd like to see a stronger guarantee from the library, beyond what is currently documented. More specifically, my issue is with the library interpreting the input in one time zone, but printing the time in another time zone. I would prefer if it printed the same time zone as the one in which it interprets the input. To reply to some of the comments above: 1:00 PST does not "exist" in precisely the same way as 2:00 PDT does not exist. The library therefore has no more reason to print the former and not the latter from that perspective. However, there is another reason to prefer printing 2:00 PDT - the fact that that's how it interpreted the input in the first place. |
You can think of this as a feature request rather than a bug report if you wish. So, rather than being convinced that this is not a bug, I need to be convinced that this is not a useful feature. To support my claim that this is a useful feature, think of the following analogy. Imagine a program that can answer any question asked in any natural language. Suppose you ask that program in English: "Is the Earth round?" How happy would you be with the program providing the answer: "Oui"? By analogy, I would like the library to print the resulting time in the timezone in which it intepreted the input, to avoid surprises. |
Neither 2:00 PDT nor 2:00 PST exist. The time goes from 1:59:59 to 3:00:00. The time package is using the same location to look up the time and to return the time: that location is the one you loaded, America/Vancouver. You are asking the time package to represent a time that simply doesn't exist. Probably the best response would be to return an error--that is what the Unix date command does--but unfortunately the I think you may be operating under a misapprehension that the time package is first picking PDT or PST and then looking up the time in that zone. That's not what it does. It actually first calculates the time without regard to timezone, then looks up the timezone offset for that time in the selected location, and then adjusts the time by that offset. Since the time 2:00 does not exist on that day on that location, the result is unpredictable, just as the documentation says. |
I do not agree. The documentation says:
In order to explain conformance of the behavior to the documentation, you have to explain what "correct" in the quote above means. The way I understand the documentation is that Please note again what I already stated above: the time printed by the library is 1:00 PST, which does not exist any more thant 2:00 PDT exists. Also, my apprehension about how the time package is implemented, and how it is actually implemented, has nothing to do with this argument: I am discussing the interface of the time package, not the implementation. |
Perhaps I'm completely confused, but why do you say that 1:00 PST does not exist? As far as I can tell, it does. The times before 2:00 are PST. The times 3:00 and after are PDT. |
I agree with the following though: returning an error would make sense, and would be less surprising than the current behavior. |
Unfortunately we can't change the API, due to https://golang.org/doc/go1compat. |
Oh, you're right, the time 1:00 PST does indeed exist. Sorry. Okay, I see how the documentation should be interpreted now: by "correct in one of the time zones" it probably means "exists in one of the time zones." Still, I think the behavior is rather surprising, and the documentation doesn't help much. If an error can not be returned, then I would at least like a stronger guarantee from the documentation as to how the input to I am concerned, because the |
Any thoughts on what that guarantee could look like? |
Also, for the record, I would like to add to the discussion about the "existence" of times. I do not actually agree with a statement that the time "2018, March 11, 2:00 PST" does not exist. I regard PST, PDT, UTC and similar entities as bijective functions from the set of all "real" time moments to the set of notations representing those time moments. In this sense 2:00 PST or even 2:30 PST and 2:30 PDT do exist: they are notations unambiguously assigned to some moments in real time by the PST or PDT function. So from this perspective, I don't think it's absolutely wrong to ever print "2018, March 11, 2:00 PST". It's simply a fact that at the time denoted by this notation, the clocks in Vancouver do not actually show "2:00". |
Here's an idea for a stronger guarantee for
Note: In the above rules "observed" means the time clocks are set to according to the laws. Rule 1. would conform to the current behavior when 2:00 is requested on the day when PST switches to PDT and 2:00 is never observed. Rule 2. would result in returning 1:00 PDT on the day when PDT switches to PST and 1:00 is observed twice. |
Hmm, the example of the effect of rule 1 should be corrected: the latest observed time before 2:00 would actually be 1:59:59 PST. Actually, it's not even well defined because time can be subdivided infinitely. So Rule 1 should be adjusted... |
Here is a better idea: First, let me define a couple terms. Let a "time zone" be a particular offset of a clock from UTC. For example, the PDT in 2018 is a rule that the clock should be offset by -7 hours from UTC. Let an "effective time zone period" be a contiguous period when a particular time zone is in effect at a particular location. For example, an effective time zone period is the time between the moment PDT starts to be observed and the moment it stops to be observed in the year 2018 in Vancouver. Now, consider the arguments to
Example 1: Daylight savings time begins
Example 2: Daylight savings time ends
This algorithm also returns the usual result when the input time is observed unambiguously at exactly one moment in history at the given location. |
The above algorithm chooses the alternative to the current implementation for both examples. The algorithm can be adjusted slightly though, to produce the result of the current implementation: Algorithm:
Example 1:
Example 2:
|
For the purpose of documentation, the proposed algorithm provides the following guarantees, stated in plain English: Variant 1:
Variant 2:
The documentation could also be clarified by replacing the term "correct" with the term "observed": "Regardless of how the arguments are interpreted, I believe the algorithm could also be tweaked to produce a mix of the effects of variant 1 and variant 2, if desired. In addition, the algorithm precisely states how more complex transitions are handled, for example:
|
What you requested is to make Returning Besides, I don't think a specific rule for DST is a better thing. No matter want time zone you choose, some people will want the other. |
Please disregard my initial request to return 2:00 PST or 2:00 PDT. My interests have shifted, as visible from the discussion above. I disagree that having a specific rule is not beneficial. Having a specific rule is definitely better than having none. Even if users don't like your choice, at least they can't complain because the choice is clearly stated and documented. Also, without a specific rule, algorithms can not rely on the behavior of |
@cznic Would you please care to comment why you downvoted my proposed algorithm? |
Search the stdlib for functions with such specialized, complex, hard to understand and explain/document behavior as proposed. You should probably/hopefully find none. Moreover, all of that for handling what basically amounts to an invalid argument. |
@cznic Can you please explain what the invalid argument is? Currently, In the absence of the possibility to return an error when the user requests a time that is not observed at a certain location (the interface of function can not be changed), there must be a well-defined return value. Yes, the algorithm I proposed above looks complicated, because it is stated precisely and unambiguously. If you look carefully through the discussion above, you will see though that I have also provided a simple, plain English explanation of the effect of the algorithm on handling a daylight savings transition which is usable in documentation. |
@cznic The algorithm may look complex, because it is generic and handles all kinds of transitions, including when a time is observed more than twice, or the clock jumps by more than an hour, etc.. I strongly believe any documentation for any implementation that will handle such transitions will always be complicated as well. However, such transitions are very rare, and the behavior of my algorithm in the comparably simple daylight savings transition is easy to explain (as I have proven above). |
@cznic It seems you are claiming that when the complete behavior of some function is inevitably hard to explain, you would rather leave the behavior undocumented (amounting to undefined behavior) than explain it. |
@cznic Alternatively, according to your statements, |
Here is an example from the Python community, proving that I am not the only one fighting for a well-defined behavior: |
@cznic In fact, the C standard library does have such a function: If you search the web you'll find that many people question its specification, quarrel about the interpretation of the specification, and some call for disambiguating the standard. |
Here is a restatement of the same algorithm I proposed above, in simpler terms. Hopefully, it will prove that the proposed behavior is not all that complicated to explain. Note: I am borrowing the terms "gap" and "fold" from here:
Mind that if you accept that a given time may only be affected by a single gap or a single fold, the explanation above becomes even simpler. Again: Please mind that the algorithm only states how the given time is to be interpreted. If one wishes, one can add:
|
Another possible solution, which might be more easily digestible:
Where "possible interpretations" are defined as follows:
|
DST is a very inconvenience in program. If If I think the requirement is very specific. If I need to do this, I will use some private methods in |
What version of Go are you using (
go version
)?go1.7.4 linux/amd64
Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH=""
GORACE=""
GOROOT="/usr/lib/go-1.7"
GOTOOLDIR="/usr/lib/go-1.7/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build628236315=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1
What did you do?
I called
time.Date
for March 11, 2018, 2:00 AM, and printed the resulting time.https://play.golang.org/p/N-XUwlGxpaF
What did you expect to see?
March 11, 2018, 2:00 AM is the moment when the clock in Vancouver changes to 3:00 AM due to switching from PST to PDT. So I would expect one of the following:
What did you see instead?
Instead,
time.Date
apparently interprets 2:00 in PDT, but prints 1:00 PST.The text was updated successfully, but these errors were encountered: