Time difference calculation bug

Roger Riggs roger.riggs at oracle.com
Wed May 3 19:58:02 UTC 2023


Hi,

The seconds and nano-seconds are computed separately.
The representation of Instant holds seconds and nanoseconds separately.

The computation is performed on the nano-seconds of the Instant and 
truncated to milliseconds.
The value of 0.000999 becomes 0 when truncated to MILLIS.

Regards, Roger

On 5/3/23 12:54 PM, SHARPE, Stuart (Commercial & Institutional CDIO) wrote:
>
> Hello,
>
> I believe I have discovered a bug in java.time. I've searched Jira and 
> couldn't find any existing similar issue.
>
> Consider the following code:
>
>     var t1 = Instant.parse("2023-05-03T10:00:00.000999Z");
>
>     var t2 = Instant.parse("2023-05-03T10:00:01.000Z");
>
>     var t3 = Instant.parse("2023-05-03T10:00:01.001Z");
>
>     System.out.println(MILLIS.between(t1, t2));
>
> System.out.println(MILLIS.between(t1, t3));
>
> This prints:
>
>     1000
>
>     1000
>
> Given that t3 is exactly one millisecond later than t2, it does not 
> seem reasonable that they can both be 1000 milliseconds later than t1.
>
> The JavaDoc for between() states "The calculation returns a whole 
> number, representing the number of complete units between the two 
> temporals."
>
> Assuming I have understood this correctly, I think MILLIS.between(t1, 
> t3) is correct but MILLIS.between(t1, t2) should return 999.
>
> Regards
>
> Stuart Sharpe
>
>
> This communication and any attachments are confidential and intended 
> solely for the addressee. If you are not the intended recipient please 
> advise us immediately and delete it. Unless specifically stated in the 
> message or otherwise indicated, you may not duplicate, redistribute or 
> forward this message and any attachments are not intended for 
> distribution to, or use by any person or entity in any jurisdiction or 
> country where such distribution or use would be contrary to local law 
> or regulation. NatWest Markets Plc, NatWest Markets N.V., NatWest 
> Markets Securities Japan Limited and/or NatWest Markets Securities 
> Inc. (collectively "NatWest Markets") accepts no responsibility for 
> any changes made to this message after it was sent.
> This communication, where prepared by the sales and trading desk or 
> desk strategists, may be marketing material, desk strategy and/or 
> trader commentary. It is not a product of the research department. 
> This material may constitute an invitation to consider entering into a 
> derivatives transaction under U.S. CFTC Regulations sections 1.71 and 
> 23.605, where applicable, but is not a binding offer to buy/sell any 
> financial instrument. The views of the author may differ from others 
> at NatWest Markets.
> Unless otherwise specifically indicated, the contents of this 
> communication and its attachments are for information purposes only 
> and should not be regarded as an offer or solicitation to buy or sell 
> a product or service, confirmation of any transaction, a valuation, 
> indicative price or an official statement. Trading desks may have a 
> position or interest that is inconsistent with any views expressed in 
> this message. In evaluating the information contained in this message, 
> you should know that it could have been previously provided to other 
> clients and/or internal NatWest Markets personnel, who could have 
> already acted on it.
> NatWest Markets cannot provide absolute assurances that all electronic 
> communications (sent or received) are secure, error free, not 
> corrupted, incomplete or virus free and/or that they will not be lost, 
> mis-delivered, destroyed, delayed or intercepted/decrypted by others. 
> Therefore NatWest Markets disclaims all liability with regards to 
> electronic communications (and the contents therein) if they are 
> corrupted, lost destroyed, delayed, incomplete, mis-delivered, 
> intercepted, decrypted or otherwise misappropriated by others.
> Any electronic communication that is conducted within or through 
> NatWest Markets systems will be subject to being archived, monitored 
> and produced to regulators and in litigation in accordance with 
> NatWest Markets’ policy and local laws, rules and regulations. Unless 
> expressly prohibited by local law, electronic communications may be 
> archived in countries other than the country in which you are located, 
> and may be treated in accordance with the laws and regulations of the 
> country of each individual included in the entire chain.
> Copyright © NatWest Markets Plc. All rights reserved. See 
> https://www.nwm.com/disclaimer for further risk disclosure (the agency 
> arrangements referred to in the further risk disclosure between 
> NatWest Markets Plc and NatWest Markets N.V. are not applicable to 
> branches of NatWest Markets N.V.).
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/core-libs-dev/attachments/20230503/4c076a91/attachment-0001.htm>


More information about the core-libs-dev mailing list