0

I'm running this code to get a DateTime and change the time of it to a preset one

DateTime  dt = Datetime.newInstanceGMT(2021, 11, 8, 12, 0, 0);
Time t = Time.newInstance(8, 0, 0, 0);

System.debug('dateTime '+dt );   
System.debug('date '+dt.dateGMT());      
System.debug('time '+t);           
System.debug('new time '+DateTime.newInstance(dt.dateGMT(), t));           

When i run this on the console it takes the right time zone (-6) and returns this

dateTime 2021-11-08 12:00:00            
date 2021-11-08 00:00:00                    
time 08:00:00.000Z                
new time 2021-11-08 14:00:00   

but when i run it in the trigger it takes a time zone of -8

dateTime 2021-11-08 12:00:00                  
date 2021-11-08 00:00:00                       
time 08:00:00.000Z                     
new time 2021-11-08 16:00:00   

Expected return 11/08/2021, 8:00 AM          
actual return 11/08/2021, 10:00 AM

Is this an error of configuration?
how can i set the right time from my trigger?

Solutions
So to summarize i got 2 issues here, one was my server and my client running on two different time zones, and the other was the way i was setting the date times the solution was to offset the time my self and set the date and new instance using GMT functions

5
  • 2
    Is the trigger also running in your user context? Nov 9, 2021 at 3:31
  • In addition are you aware that System.debug shows date/time values in UTC (what Salesforce call GMT) when not specifically output in a given time zone?
    – Phil W
    Nov 9, 2021 at 5:45
  • @NagendraSingh no, the trigger is running on the system context, the console is running on my user context
    – EDD rico
    Nov 9, 2021 at 17:42
  • @PhilW i'm not sure what you mean, here i'm getting two different data UTC and GMT and System.debug is just showing the right data, UTC is shifted as it shoud and GMT is shown raw as it should
    – EDD rico
    Nov 9, 2021 at 17:42
  • The code is constructing both Datetime values in user timezone. System.debug always shows Datetimes in UTC, never in user timezone. That's all I'm saying.
    – Phil W
    Nov 9, 2021 at 18:01

1 Answer 1

1

see below for the specific answer to the question in comments

Time is really complex. You need to use newInstanceGMT in the creation of your second time, or make sure all your dates and times are in 'local' time, which can be confusing since local time changes for users... so to make it easier, always use local or GMT, but never cross the streams... Don't even get me started about trigger contexts...

I also want to point out you created another potential issue by specifying dateGMT with your local date creation, which could lead to the issues you see below:

DateTime  dt = Datetime.newInstanceGMT(2021, 11, 8, 4, 0, 0);
Time t = Time.newInstance(8, 0, 0, 0);

System.debug('dateTime '+dt );   
System.debug('date '+dt.dateGMT());      
System.debug('time '+t);       
System.debug('new time '+DateTime.newInstance(dt.dateGMT(), t));  
System.debug('new time '+DateTime.newInstance(dt.date(), t));  
System.debug('new time '+DateTime.newInstanceGMT(dt.dateGMT(), t)); 
System.debug('new time '+DateTime.newInstanceGMT(dt.date(), t)); 

here is the resultant logs:

05:42:53.16 (17457343)|USER_DEBUG|[4]|DEBUG|dateTime 2021-11-08 04:00:00
05:42:53.16 (17524443)|USER_DEBUG|[5]|DEBUG|date 2021-11-08 00:00:00
05:42:53.16 (17584575)|USER_DEBUG|[6]|DEBUG|time 08:00:00.000Z
05:42:53.16 (17669288)|USER_DEBUG|[7]|DEBUG|new time 2021-11-08 15:00:00
05:42:53.16 (17728841)|USER_DEBUG|[8]|DEBUG|new time 2021-11-07 15:00:00 //local date is different from GMT date!
05:42:53.16 (17786827)|USER_DEBUG|[9]|DEBUG|new time 2021-11-08 08:00:00 //what you want!!!
05:42:53.16 (17833628)|USER_DEBUG|[10]|DEBUG|new time 2021-11-07 08:00:00

There is a lot going on here, but again... either use local or GMT. and I would always construct my newInstances of DateTime using integers... the Time t is introducing yet another Zulu time that is further screwing things up.

As a fun aside: Greenwich Mean Time was designated as the "reference time", having an offset of zero hours. The phonetic radio alphabet for Z is ZULU- so when one says "ZULU time", it's short for "zero-offset time", meaning Greenwich Mean Time, or more properly, UTC - Coordinated Universal Time.

Specific question from comments
How would you set DateTime correctly in a local timezone?

First of all, I would try to use. Get the specific

DateTime  dt = Datetime.newInstance(2021, 11, 8, 4, 0, 0);

This will almost always give you what you want.

If you need to follow the format you were suggesting above, you can use the following

DateTime  dt = Datetime.newInstanceGMT(2021, 11, 8, 4, 0, 0);
Time t = Time.newInstance(8, 0, 0, 0);
TimeZone tz = UserInfo.getTimeZone(); // you can also specify a timezone, see the APEX TimeZone class
DateTime dt2 = Datetime.newInstanceGMT(dt.dateGMT(), t - tz.getOffset(dt)); 

this returns:

|DEBUG|dateTime 2021-11-08 11:00:00 //-7 == 4:00 in GMT
|DEBUG|time 08:00:00.000Z //GMT
|DEBUG|new time dt2 2021-11-08 15:00:00 //!!!! 15 is -7, or 14:00 in -6

Hope this helps!

5
  • thanks, but this DateTime.newInstanceGMT(dt.dateGMT(), t)); will set the time as GMT(which i want) but the time is still in local timezone 2021-11-08 08:00:00 this should be stored as 2021-11-08 14:00:00 Since my local time is -6
    – EDD rico
    Nov 9, 2021 at 17:51
  • so there is your answer! 8:00:00Z is not 14:00:00 -6, it equals 2:00:00 local time. I'll work the right thing into my answer.
    – dbwood3
    Nov 9, 2021 at 18:53
  • there is one tiny, tiny hole in my code. if the date-time you are setting is in the 2 to 3 am on fall back or spring forward day, you may get 'different results'. If this slight issue in the middle of the night on two days in the year is an issue for you, you should test away and please post what happens... best example... on Sunday morning for those of us that fell back, there were 2 2:15AMs. with two different offsets... I'm not sure how SF handles that... and now I must get back to work!
    – dbwood3
    Nov 9, 2021 at 19:51
  • Yes, i got the same idea of set the offset my self, and it's working fine, i'll check those corner cases you talked about, thank you
    – EDD rico
    Nov 9, 2021 at 20:04
  • you're welcome! glad to help...
    – dbwood3
    Nov 9, 2021 at 20:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.