@force2be Time / Timezone conversions


/ Published in: Other
Save to your folder(s)



Copy this code and paste it in your HTML
  1. //http://www.force2b.net/index.php/2010/08/date-time-and-timezone-handling-in-apex/
  2.  
  3. /* **********************************************************************************************
  4. * TimeConversions Class
  5. * Created by: Michael Smith/Force2b, 04/06/2010
  6. *
  7. ************************************************************************************************ */
  8. global class TimeConversions {
  9.  
  10. /* -------------------------------------------------------------------------------------
  11. * Returns an Integer of the Timezone Offset from Eastern Time for the
  12. * currently logged in user
  13. *
  14. * This is used to convert the String DateTime (that is in Eastern Time) into a
  15. * DateTime value in SalesForce. The default behavior of SFC converts the string into
  16. * a local datetime value, but we need to get into Eastern Time.
  17. * ------------------------------------------------------------------------------------- */
  18. public Integer getCurrentUserTZOffsetFromEastern() {
  19.  
  20. Map<String, Integer[]> tzSIDKeys = getTZSidKeys();
  21.  
  22. User user = [SELECT ID, TimeZoneSidKey FROM User WHERE ID = :UserInfo.getUserId() LIMIT 1];
  23.  
  24. Date[] dstDatesNow = getDSTDates(System.Today().year());
  25. Integer UsersTZOffset = 0;
  26.  
  27. if (tzSIDKeys.get(user.TimeZoneSidKey) != null) {
  28. // Get the base timezone offset from GMT for the user
  29. if (System.Today() >= dstDatesNow[0] && System.Today() <= dstDatesNow[1]) UsersTZOffset = tzSIDKeys.get(user.TimeZoneSidKey)[1];
  30. else UsersTZOffset = tzSIDKeys.get(user.TimeZoneSidKey)[0];
  31. system.debug(LoggingLevel.Error, 'Base TimeZone for Current User=' + user.TimeZoneSidKey + '/' + UsersTZOffset );
  32.  
  33. // Now make it a timezone offset from EASTERN time
  34. Integer EasternTZOffset = 0;
  35. if (System.Today() >= dstDatesNow[0] && System.Today() <= dstDatesNow[1]) EasternTZOffset = tzSIDKeys.get('America/New_York')[1];
  36. else EasternTZOffset = tzSIDKeys.get('America/New_York')[0];
  37. UsersTZOffset = Math.abs(EasternTZOffset) - Math.abs(UsersTZOffset);
  38. system.debug(LoggingLevel.Error, 'TimeZone Offset to Eastern Time=' + UsersTZOffset );
  39. }
  40. return UsersTZOffset ;
  41. }
  42.  
  43. /* -------------------------------------------------------------------------------------
  44. * Returns a String Collection of the Timezone Codes based on the Timezone Offset Passed
  45. * for the date passed.
  46. *
  47. * Based on a table from: http://en.wikipedia.org/wiki/Zone.tab
  48. *
  49. * getTimeZoneCode[0] = Display Text (ex: EDT)
  50. * getTimeZoneCode[1] = DateTime.Format() parameter (ex: America/New_York)
  51. * ------------------------------------------------------------------------------------- */
  52. public string[] getTimeZoneCode(Integer tzOffset, Date theDate, Boolean isDSTObserved) {
  53. Date[] dstDates = getDSTDates(theDate.year()); // [0]=startDate, [1]=endDate
  54. boolean isDSTOn = (theDate >= dstDates[0] && theDate <= dstDates[1]);
  55.  
  56. if (tzOffset == 0) return new String[]{' GMT', 'Europe/London' };
  57. else if (tzOffset == 4) return new String[]{' AST (UTC-04)', 'America/Puerto_Rico' };
  58. else if (tzOffset == 5 && isDSTOn && isDSTObserved) return new String[]{' EDT (UTC-04)', 'America/New_York' };
  59. else if (tzOffset == 5) return new String[]{' EST (UTC-05)', 'America/New_York' };
  60. else if (tzOffset == 6 && isDSTOn && isDSTObserved) return new String[]{' CDT (UTC-05)', 'America/Chicago' };
  61. else if (tzOffset == 6) return new String[]{' CST (UTC-06)', 'America/Chicago' };
  62. else if (tzOffset == 7 && !isDSTObserved) return new String[]{' MST (UTC-07)', 'America/Phoenix' };
  63. else if (tzOffset == 7 && isDSTOn && isDSTObserved) return new String[]{' MDT (UTC-06)', 'America/Denver' };
  64. else if (tzOffset == 7) return new String[]{' MST (UTC-07)', 'America/Denver' };
  65. else if (tzOffset == 8 && isDSTOn && isDSTObserved) return new String[]{' PDT (UTC-07)', 'America/Los_Angeles' };
  66. else if (tzOffset == 8) return new String[]{' PST (UTC-08)', 'America/Los_Angeles' };
  67. else if (tzOffset == 9 && isDSTOn && isDSTObserved) return new String[]{' AKDT (UTC-08)', 'America/Anchorage' };
  68. else if (tzOffset == 9) return new String[]{' AKST (UTC-09)', 'America/Anchorage' };
  69. else if (tzOffset == 10 && !isDSTObserved) return new String[]{' HST (UTC-10)', 'Pacific/Honolulu' };
  70. else if (tzOffset == 10 && isDSTOn && isDSTObserved) return new String[]{' HDT (UTC-09)', 'America/Adak' };
  71. else if (tzOffset == 10) return new String[]{' HST (UTC-10)', 'America/Adak' };
  72. else if (tzOffset == 11) return new String[]{' HST (UTC-10)', 'Pacific/Pago_Pago' };
  73. else return new String[]{' UTC-' + tzOffset, 'GMT' };
  74. }
  75.  
  76. /* -------------------------------------------------------------------------------------
  77. * Returns a date Collection of Start/End dates for US Daylight Saving Time
  78. * for the specified year.
  79. *
  80. * Based on code from: http://www.webexhibits.org/daylightsaving/b2.html
  81. * ------------------------------------------------------------------------------------- */
  82. public Date[] getDSTDates(Integer theYear) {
  83. Long thisYear;
  84. Long AprilDate;
  85. Long OctoberDate;
  86. Long MarchDate;
  87. Long NovemberDate;
  88. Long longSeven = 7;
  89. thisYear = Math.round(theYear);
  90.  
  91. AprilDate = Math.mod(2+6 * thisYear - Math.floor(thisYear / 4).longValue(), longSeven) + 1;
  92. OctoberDate= Math.mod(31-( Math.floor(thisYear * 5 / 4).longValue() + 1), longSeven);
  93.  
  94. MarchDate = 14 - Math.mod(Math.floor(1 + thisYear * 5 / 4).LongValue(), longSeven);
  95. NovemberDate = 7 - Math.mod(Math.floor (1 + thisYear * 5 / 4).LongValue(), longSeven);
  96.  
  97. string startDate = (thisYear > 2006 ? ('03/'+MarchDate) : ('04/'+AprilDate)) + '/' + thisYear;
  98. string endDate = (thisYear > 2006 ? ('11/'+NovemberDate):('10/'+OctoberDate))+ '/' + thisYear;
  99.  
  100. Date[] rtnDates = new List<Date>();
  101. rtnDates.add(Date.parse(startDate));
  102. rtnDates.add(Date.parse(endDate));
  103. return rtnDates;
  104. }
  105.  
  106. public Map<String, Integer[]> getTZSidKeys() {
  107. Map<String, Integer[]> tzSIDKeys = new Map<String, Integer[]>();
  108. tzSIDKeys.put('America/Adak', new Integer[]{-10, -9});
  109. tzSIDKeys.put('America/Anchorage', new Integer[]{-9, -8});
  110. tzSIDKeys.put('America/Chicago', new Integer[]{-6, -5});
  111. tzSIDKeys.put('America/Denver', new Integer[]{-7, -6});
  112. tzSIDKeys.put('America/Detroit', new Integer[]{-5, -4});
  113. tzSIDKeys.put('America/Halifax', new Integer[]{-4, -3});
  114. tzSIDKeys.put('America/Indianapolis', new Integer[]{-5, -4});
  115. tzSIDKeys.put('America/Los_Angeles', new Integer[]{-8, -7});
  116. tzSIDKeys.put('America/Montreal', new Integer[]{-5, -4});
  117. tzSIDKeys.put('America/New_York', new Integer[]{-5, -4});
  118. tzSIDKeys.put('America/Panama', new Integer[]{-5, -5});
  119. tzSIDKeys.put('America/Phoenix', new Integer[]{-7, -7});
  120. tzSIDKeys.put('America/Puerto_Rico', new Integer[]{-4, -4});
  121. tzSIDKeys.put('America/Toronto', new Integer[]{-5, -4});
  122. tzSIDKeys.put('America/Vancouver', new Integer[]{-8, -7});
  123. tzSIDKeys.put('Europe/London', new Integer[]{0, 1});
  124. tzSIDKeys.put('Pacific/Honolulu', new Integer[]{-10, -10});
  125. tzSIDKeys.put('Pacific/Pago_Pago', new Integer[]{-11, -11});
  126.  
  127. return tzSIDKeys;
  128. }
  129. }
  130.  

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.