TimeZoneCode to TimeZoneInfo

瘦欲@ 提交于 2019-12-01 00:28:38

Here is the code that I use to get a user's TimeZoneInfo. It retrieves the TimeZoneDefinition from CRM based on the name, but I believe you can look it up by the Bias matching the UTC offset from your excel file.

public static TimeZoneInfo GetUserTimeZone(IOrganizationService service, Guid userId)
{
    int timeZoneCode = 35; //default timezone to eastern just incase one doesnt exists for user
    var userSettings = service.Retrieve(UserSettings.EntityLogicalName, userId, new ColumnSet("timezonecode")).ToEntity<UserSettings>();

    if ((userSettings != null) && (userSettings.TimeZoneCode != null))
    {
        timeZoneCode = userSettings.TimeZoneCode.Value;
    }

    return GetTimeZone(service, timeZoneCode);
}

public static TimeZoneInfo GetTimeZone(IOrganizationService service, int crmTimeZoneCode)
{
    var qe = new QueryExpression(TimeZoneDefinition.EntityLogicalName);
    qe.ColumnSet = new ColumnSet("standardname");
    qe.Criteria.AddCondition("timezonecode", ConditionOperator.Equal, crmTimeZoneCode);
    return TimeZoneInfo.FindSystemTimeZoneById(service.RetrieveMultiple(qe).Entities.First().ToEntity<TimeZoneDefinition>().StandardName);
}

Edit - Bias is null

This could just be our on prem version of CRM, but this is what it is currently populated in CRM for any time zone that is -5, -6,-7, or -8.

.

This would make the bias lookup null & void.

On a side note, 99% of our users are on Eastern Time, but we have a few in California, and I haven't heard of any issues as of yet. But now I'm wondering if we bothered to test this before and after DST...

In case you should need a list of all CRM timezones and their relative SQL identifier: We wrote a little program to loop over all the options in CRM and output the name, semicolon, sql code.

(GMT+10:00) Canberra, Melbourne, Sydney (Commonwealth Games 2006); 256 
(GMT-04:00) Georgetown, La Paz, Manaus, San Juan; 55 
(GMT) Greenwich Mean Time : Dublin, Edinburgh, Lisbon, London; 85 
(GMT+06:00) Ekaterinburg; 180 
(GMT+03:00) Baghdad; 158 
(GMT+06:00) Dhaka; 196 
(GMT-06:00) Guadalajara, Mexico City, Monterrey; 29 
(GMT+04:00) Abu Dhabi, Muscat; 165 
(GMT+02:00) Beirut; 131 
(GMT) Coordinated Universal Time; 92 
(GMT+10:00) Canberra, Melbourne, Sydney; 255 
(GMT+10:00) Brisbane; 260 
(GMT-01:00) Cape Verde Is.; 83 
(GMT+01:00) Brussels, Copenhagen, Madrid, Paris; 105 
(GMT+12:00) Auckland, Wellington; 290 
(GMT+2:00) Nicosia; 115 
(GMT+02:00) Athens, Bucharest; 130 
(GMT+13:00) Nukualofa; 300 
(GMT+10:00) Hobart; 265 
(GMT+04:00) Port Louis; 172 
(GMT+07:00) Novosibirsk; 201 
(GMT-03:30) Newfoundland; 60 
(GMT+03:30) Tehran; 160 
(GMT+08:00) Beijing, Chongqing, Hong Kong, Urumqi; 210 
(GMT+02:00) Amman; 129 
(GMT-06:00) Central America; 33 
(GMT+08:00) Krasnoyarsk; 207 
(GMT-11:00) Coordinated Universal Time-11; 6 
(GMT+01:00) Sarajevo, Skopje, Warsaw, Zagreb; 100 
(GMT-05:00) Bogota, Lima, Quito; 45 
(GMT+10:00) Yakutsk; 240 
(GMT+04:00) Yerevan; 170 
(GMT+09:00) Osaka, Sapporo, Tokyo; 235 
(GMT+10:00) Guam, Port Moresby; 275 
(GMT+12:00) Fiji; 285 
(GMT+04:30) Kabul; 175 
(GMT-05:00) Eastern Time (US & Canada); 35 
(GMT+11:00) Vladivostok; 270 
(GMT-01:00) Azores; 80 
(GMT+02:00) Jerusalem; 135 
(GMT+01:00) Belgrade, Bratislava, Budapest, Ljubljana, Prague; 95 
(GMT+07:00) Bangkok, Hanoi, Jakarta; 205 
(GMT-03:00) Buenos Aires; 69 
(GMT+09:00) Irkutsk; 227 
(GMT+12:00) Magadan; 281 
(GMT-03:00) Cayenne, Fortaleza; 70 
(GMT-07:00) Chihuahua, La Paz, Mazatlan; 12 
(GMT+02:00) Cairo; 120 
(GMT-08:00) Baja California; 5 
(GMT+12:00) Petropavlovsk-Kamchatsky - Old; 295 
(GMT+05:00) Tashkent; 185 
(GMT+03:00) Nairobi; 155 
(GMT-04:00) Atlantic Time (Canada); 50 
(GMT-12:00) International Date Line West; 0 
(GMT+03:00) Kaliningrad, Minsk; 159 
(GMT-02:00) Coordinated Universal Time-02; 76 
(GMT+02:00) Istanbul; 134 
(GMT+08:00) Perth; 225 
(GMT+13:00) Samoa; 1 
(GMT) Casablanca; 84 
(GMT+06:00) Astana; 195 
(GMT+09:30) Darwin; 245 
(GMT-04:30) Caracas; 47 
(GMT-10:00) Hawaii; 2 
(GMT+09:00) Seoul; 230 
(GMT+08:00) Kuala Lumpur, Singapore; 215 
(GMT-06:00) Guadalajara, Mexico City, Monterrey - Old; 30 
(GMT+06:30) Yangon (Rangoon); 203 
(GMT+04:00) Moscow, St. Petersburg, Volgograd; 145 
(GMT+02:00) Damascus; 133 
(GMT-07:00) Arizona; 15 
(GMT+04:00) Tbilisi; 173 
(GMT-09:00) Alaska; 3 
(GMT-03:00) Brasilia; 65 
(GMT+01:00) Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna; 110 
(GMT-07:00) Chihuahua, La Paz, Mazatlan - Old; 13 
(GMT+08:00) Ulaanbaatar; 228 
(GMT-03:00) Greenland; 73 
(GMT+09:30) Adelaide; 250 
(GMT-02:00) Mid-Atlantic; 75 
(GMT+12:00) Coordinated Universal Time+12; 284 
(GMT+02:00) Helsinki, Kyiv, Riga, Sofia, Tallinn, Vilnius; 125 
(GMT+02:00) Harare, Pretoria; 140 
(GMT-05:00) Indiana (East); 40 
(GMT+01:00) West Central Africa; 113 
(GMT-08:00) Pacific Time (US & Canada); 4 
(GMT+05:00) Islamabad, Karachi; 184 
(GMT+11:00) Solomon Is., New Caledonia; 280 
(GMT+03:00) Kuwait, Riyadh; 150 
(GMT+08:00) Taipei; 220 
(GMT-03:00) Montevideo; 74 
(GMT+09:30) Adelaide (Commonwealth Games 2006); 251 
(GMT+10:00) Hobart (Commonwealth Games 2006); 266 
(GMT+05:30) Sri Jayawardenepura; 200 
(GMT+04:00) Baku; 169 
(GMT+05:30) Chennai, Kolkata, Mumbai, New Delhi; 190 
(GMT-06:00) Central Time (US & Canada); 20 
(GMT-07:00) Mountain Time (US & Canada); 10 
(GMT-06:00) Saskatchewan; 25 
(GMT-04:00) Cuiaba; 58 
(GMT-04:00) Asuncion; 59 
(GMT+01:00) Windhoek; 141 
(GMT-04:00) Santiago; 56 
(GMT+05:45) Kathmandu; 193 
Bahia Standard Time; 71 
(GMT) Monrovia, Reykjavik; 90 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!