9

suppose i am a call center employee with IST time zone and want to fix a meeting with a US customer. I enquire about his time zone and time at which he will be available. Now i want to convert the time given by customer to my Timezones time. How can i do this in salesforce??

2
  • 1
    timeanddate.com/worldclock I would use this website and convert and if we want this functionality inside salesforce then we may need some custom code .Quick solution will be to keep this as short link and convert time zone – Mohith Shrivastava Feb 15 '13 at 14:26
  • Interesting is the above website gives API and hence probably i am thinking of building a Visualforce Page and provide this ability.something to work on and also exploring App exchange to see if we have something for conversion wont be bad idea – Mohith Shrivastava Feb 15 '13 at 14:41
13

The Timezone class is new in Spring 13 and includes the getOffset() method to get

"the time zone offset, in milliseconds, of the specified date to the GMT time zone."

It should be possible to use this information to do the timezone conversion.

You would need a list of time zone ids. According to the docs these values match those in the Java TimeZone class. E.g. "America/Los_Angeles". Here is a good list of TimeZoneSidKeys - https://docs.google.com/spreadsheet/ccc?key=0Ar3Bg2QuV-_IdFNnbDdmOHpyV0NxQmhTUE5lN0hVLWc&hl=en#gid=0

Alternatively, you can see the valid values under the User Locale Settings > Time Zone.

Salesforce User Locale Settings - Time Zone


This is a bit rough, but it should give you a start. Here I convert a Date and Time from the users PST TimeZone to mine - NZDT.

// This is the Date and Time in the users TimeZone
string customerDateTimeString = '2013-02-17 17:35:00';
DateTime customerDateTime = DateTime.valueofGmt(customerDateTimeString);
string customerTimeZoneSidId = 'America/Los_Angeles';

TimeZone customerTimeZone = TimeZone.getTimeZone(customerTimeZoneSidId);
System.assertEquals('Pacific Standard Time',  customerTimeZone.getDisplayName());

integer offsetToCustomersTimeZone = customerTimeZone.getOffset(customerDateTime);
System.debug('GMT Offset: ' + offsetToCustomersTimeZone + ' (milliseconds) to PST');

// For the given Date I expect PST to be GMT - 8 hours
System.assertEquals(-8, offsetToCustomersTimeZone / (1000 * 60 *60));

// Here you might like to explicitly use 'Asia/Colombo' to get IST in your code.
TimeZone tz = UserInfo.getTimeZone();
// During daylight saving time for the Pacific/Auckland time zone
integer offsetToUserTimeZone = tz.getOffset(customerDateTime);
System.debug('GMT Offset: ' + offsetToUserTimeZone + ' (milliseconds) to NZDT');

// Expect NZDT to be GMT + 13 hours
System.assertEquals(13, offsetToUserTimeZone / (1000 * 60 *60));

// Figure out correct to go from Customers DateTime to GMT and then from GMT to Users TimeZone
integer correction = offsetToUserTimeZone - offsetToCustomersTimeZone;
System.debug('correction: ' + correction);

// Note: Potential issues for TimeZone differences less than a minute
DateTime correctedDateTime = customerDateTime.addMinutes(correction / (1000 * 60));
System.debug('correctedDateTime: ' + correctedDateTime);
// In the users Pacific/Auckland timezone the time should be moved forward 21 hours
System.assertEquals(correctedDateTime, DateTime.valueofGmt('2013-02-18 14:35:00'));

Things might get a bit tricky around transitions to and from daylight savings in various regions. TimeZone.getOffset() will correct for this, but it would be worth creating some test cases for the boundary conditions.

Obligatory bad guy: This operation is sheer foolishness, and it's not happening on my watch!  Mainly because I can't figure out how to adjust the time. Source xkcd

0

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.