0

I am using the below code in the trigger to calculate due time:

                Time TIME_6_PM = Time.newInstance(18, 0, 0, 0);
                ol.Transcript_Due__c = Datetime.newInstanceGmt(ol.Transcript_Due__c.dateGMT(), TIME_6_PM);
                System.debug('TranscriptDue is: ' + ol.Transcript_Due__c);

The code is for our UK users so our time zone is set to be London/EU, and it is GMT time zone. The debug info shows correctly the due time is: 2018-03-27 18:00:00 .

However, when it comes to the SObject field, it suddenly becomes 27/03/2018 19:00.

I have noticed there is a day saving time happening in Europe at 25/03, and this issue happens exactly after that point. But I don't see much connection between.

If I run the same logic in a dummy Visualforce page it displays 18:00 properly without any issue which makes me believe it is not a timezone issue. So what is going wrong here?

1
  • I believe the system corresponds local time, in this case GMT, to UTC. This means that from 25/03 it will still be trying to assign the time as 6pm UTC, which will actually be 7pm GMT.
    – CaptainHaZ
    Commented Mar 16, 2018 at 8:44

1 Answer 1

3

Some fun timezone facts: London's timezone is not really GMT. It's BST in the summer and GMT the rest of the year. GMT is the same as UTC. You can blame Scottish farmers for this, if you like.

So, what you're building when you construct the Time object is 18.00 GMT/UTC. When that's displayed via a London user's locale, it will be 19.00 on dates after we enter BST.

When you print the DateTime via System.debug() it is showing you the value in GMT/UTC.

When you view it via a sObject field, it is showing in the London locale.

When you output a DateTime via a Visualforce, there are many ways to get unexpected locale/formats. So, you might have fallen foul of one of those. See Formatting Date with outputField The short answer is to use .format() in Apex, which does respect timezones.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .