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

Datetime Test #27

Open
aranwe opened this issue Apr 22, 2020 · 2 comments
Open

Datetime Test #27

aranwe opened this issue Apr 22, 2020 · 2 comments

Comments

@aranwe
Copy link

aranwe commented Apr 22, 2020

Hi,

Tests fail when User have different timezone to GMT.
Log:

RTest.toTest    System.AssertException: Assertion Failed: Expected: 2017-12-31 23:00:00, Actual: 2018-01-01 00:00:00
in classes/RTest.cls:396:1

This is in my opinion false positive and Rtest.cls:393 should be:

--- Datetime mDatetime = Datetime.newInstance(2018, 1, 1);
+++ Datetime mDatetime = Datetime.newInstanceGmt(2018, 1, 1);

There should probably be method R.toDatetimeGmt(); to avoid confusion on timezone R uses internally.

What do you think?

@liumiaowilson
Copy link
Contributor

Thank you for pointing out the mistake.

After checking the code, I found that the wrong conversion of Date -> Datetime actually happens in this snippet of ToDatetimeFunc:

            else if(arg instanceof Datetime) {
                return (Datetime)arg;
            }
            else if(arg instanceof Date) {
                return getDatetime((Date)arg);
            }

This issue is fixed after I changed the code to this:

            else if(arg instanceof Date) {
                return getDatetime((Date)arg);
            }
            else if(arg instanceof Datetime) {
                return (Datetime)arg;
            }

The reason is tricky. In Apex, Date instanceof Datetime returns true. So when converting to Datetime from Date, the original code directly coerces the Date to Datetime, which caused the previous issue.

You can try the latest code and check it.

@aranwe
Copy link
Author

aranwe commented Sep 29, 2021

Btw: sorry I haven't replied -> yes, this does fix the issue for me :)

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

2 participants