+ Start a Discussion
Nicholas PalattaoNicholas Palattao 

Timezone.getOffset returning inconsistent values

I have a trigger which uses UserInfo.getTimeZone() in order to obtain the GMT offset for the current user's timezone.
This offset is then compared to a timezone selected from a picklist field in order to convert a Date/Time field.

For example, if a user is in Los Angeles (GMT -08:00), and they select New York in this timezone picklist (GMT -05:00), the value of the Date/Time picklist field will be adjusted by 3 hours. 
The problem I recently discovered is that sometimes, Timezone.getOffset returns -25200000 milliseconds (-7 hours) instead of the expected -28800000 (-8 hours). This happens using the same user on different records without changing the user timezone at all. 

Because of this, many of our records have fields which are an hour off. Daylight savings time in the US does not go into effect until mid March (2017), so I don't understand how the getOffset function can be returning -7 hours so randomly.
It is highly inconsistent and I have not been able to find anything in common between records using -7 versus those using the correct number of -8. 
Any ideas or help would be greatly appreciated here.
Eric PepinEric Pepin
I believe that Salesforce adjusts the DateTime value by the offset for the DateTime value itself, not the DateTime at which you are querying it. Salesforce stores all dates in UTC and then converts them based on the user's locale when it is viewed in the UI. If you look at a DateTime value of March 1st through the UI, it will show the offset from GMT to Standard time, because March 1st is a time of year when Daylight Savings is not observed. Conversely, if you view a DateTime value of May 1st through the UI, Salesforce will offset GMT to Daylight Savings, because May 1st is a time of year when DST IS observed. It does not take into account the DateTime at which you queried the data, but looks at the date value itself to determine the offset.