5

Q: Has Salesforce broken away from datetime standards or did datetime standards change or get looser?

I ran this script in Execute Anon:

TimeZone tz = 
  UserInfo.getTimeZone();
System.debug(
  'Display name: ' + 
  tz.getDisplayName());
System.debug(
  'ID: ' + 
  tz.getID());
Datetime zGMT = Datetime.Now();
date dT = zGMT.date();
time tM = zGMT.time();
System.Debug('zGMT: '+zGMT);
System.Debug('zGMT.format : '+zGMT.format());
System.Debug('dT : '+dt);
System.Debug('tM : '+tM);

I got this result:

  1. 22:15:14:006 USER_DEBUG [3]|DEBUG|Display name: New Zealand Standard Time
  2. 22:15:14:006 USER_DEBUG [6]|DEBUG|ID: Pacific/Auckland
  3. 22:15:14:007 USER_DEBUG [12]|DEBUG|zGMT : 2017-01-15 09:15:14
  4. 22:15:14:007 USER_DEBUG [13]|DEBUG|zGMT.format : 15/01/2017 10:15 PM
  5. 22:15:14:007 USER_DEBUG [14]|DEBUG|dT : 2017-01-15 00:00:00
  6. 22:15:14:007 USER_DEBUG [15]|DEBUG|tM : 22:15:14.174Z

All of the results returned by Salesforce are correct except line 6.

If the timestamp is going to represent a timezone ALPHA then it should be an "M" for "Mike" or X for Xray as that is the TimeZone I am in, not sure which due to daylight savings time but definately not Z for Zulu or GMT

EXTRA- I remember about 6 years ago I could insert dates with this format and change the ALPHA at the end to represent my timezone and salesforce would adjust it to GMT for me. 2017-01-15T22:15:14.174M

Any ideas. I really feel like this is a bug.

4
  • You only have 5 system.debug calls in your snippet.
    – Adrian Larson
    Jan 15, 2017 at 14:45
  • Yes, Sorry I updated my run when I realised I missed line debug 3, but forgot to update the snippet. Cheers Jan 15, 2017 at 15:46
  • 1
    Can you elaborate on how you used 2017-01-15T22:15:14.174M?
    – Adrian Larson
    Jan 15, 2017 at 16:06
  • Back in the day I used the timezone ALPHA equivalent on dates. I only ever used in dataloader. To data migrate local times into GMT. I only used it when formatting my data via excel. Because A. excel was dodgey and messed with dates, and B. Dataloader often had issues. on different versions. I did not use for very long because within a year of using it it broke and I stopped trying. - But Dates have always been touchy, in my experience. This latest issue just another example (luckiy not a big deal as it turns out). Jan 15, 2017 at 18:52

1 Answer 1

4

The Date and Time objects are timezone agnostic; they neither carry nor honor timezone information. You can prove this with a simple script:

System.debug(
    DateTime.newInstance(
        Date.today(), 
        Time.newInstance(0, 0, 0, 0)));
System.debug(
    DateTime.newInstanceGMT(
        Date.today(), 
        Time.newInstance(0, 0, 0, 0)));

In my org, where I'm currently GMT-06:00, I get the following values:

11:04:18.1 (13606970)|USER_DEBUG|[1]|DEBUG|2017-01-15 06:00:00
11:04:18.1 (13646506)|USER_DEBUG|[5]|DEBUG|2017-01-15 00:00:00

As you can see, the first value shows that six hours were added, because I'm in GMT-06:00, and the default debug method outputs GMT all the time, while the second one shows midnight GMT.

You might say that there's a bug with the the system renders the output, because Time is timezone agnostic, but that's the extent of the damage. If you use a Time with newInstance, the time will be appropriate for your configured timezone, while if you use newInstanceGMT, it will be appropriate for the GMT timezone.

Similarly, if you use DateTime.time to get the Time fragment, the Time will be the appropriate hours-minutes-seconds-milliseconds for your configured time zone, while if you use DateTime.timeGMT to get the Time fragment, the Time will be appropriate for the GMT timezone. As a similar demonstration to above:

System.debug(
    DateTime.newInstance(
        Date.today(), 
        Time.newInstance(0, 0, 0, 0)).time());
System.debug(
    DateTime.newInstance(
        Date.today(), 
        Time.newInstance(0, 0, 0, 0)).timeGMT());

Outputs the following values:

11:14:27.1 (1758565)|USER_DEBUG|[1]|DEBUG|00:00:00.000Z
11:14:27.1 (1791286)|USER_DEBUG|[5]|DEBUG|06:00:00.000Z

As you can see, despite both Time values outputting "Z", the hours, minutes, and seconds are appropriate for the timezone. The first one shows midnight in my configured timezone, while the second shows what time it would be when it is midnight in my timezone in GMT. There's no inconsistency with the logic except for the displayed Z, which shouldn't be there.

2
  • Great Answer Thank You. That makes a lot of sense. But it was confusing me. Not enough knowledge can be dangerous :) I am planning on writing a better Page/Document for DateTime Instructions and Use Cases. I find Salesforce documentation easily misunderstood. Prior to doing that, is here (Stack Exchange) and good place to have a dialogue with people to ensure I get the facts correct, while I write it. Alternately I could Write it in a wiki and post it back here as a reference for feedback??? Jan 15, 2017 at 18:38
  • @NZDev A wiki would be awesome. More time than I can manage on my own, but I'd be willing to contribute.
    – sfdcfox
    Jan 15, 2017 at 18:50

You must log in to answer this question.

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