/// <summary> /// Gets the Outlook AppointmentItem representation of this CalendarItem /// </summary> /// <param name="item">The AppointmentItem to edit</param> /// <returns>An Outlook AppointmentItem representation of this CalendarItem</returns> public AppointmentItem GetOutlookAppointment(AppointmentItem item) { try { item.Start = DateTime.Parse(Start); item.End = DateTime.Parse(End); item.Location = Location; item.Body = Body; item.Subject = Subject; item.AllDayEvent = IsAllDayEvent; Microsoft.Office.Interop.Outlook.TimeZone startTz = OutlookSync.Syncer.CurrentApplication.TimeZones[TimeZoneConverter.IanaToWindows(StartTimeZone)]; Microsoft.Office.Interop.Outlook.TimeZone endTz = OutlookSync.Syncer.CurrentApplication.TimeZones[TimeZoneConverter.IanaToWindows(EndTimeZone)]; item.StartTimeZone = startTz; item.EndTimeZone = endTz; if (!m_isUsingDefaultReminders) { item.ReminderMinutesBeforeStart = ReminderTime; item.ReminderSet = true; } if (Recurrence != null) { var pattern = item.GetRecurrencePattern(); Recurrence.GetOutlookPattern(ref pattern, IsAllDayEvent); } foreach (var attendee in Attendees) { var recipt = item.Recipients.Add(attendee.Name); bool result = recipt.Resolve(); if (!result) { ContactItem contact = new ContactItem(attendee.Name, attendee.Email); result = contact.CreateContact(); } result &= recipt.Resolve(); if (result) { recipt.AddressEntry.Address = attendee.Email; recipt.Type = attendee.Required ? (int)OlMeetingRecipientType.olRequired : (int)OlMeetingRecipientType.olOptional; } } return(item); } catch (COMException ex) { Log.Write(ex); MessageBox.Show( "CalendarItem: There has been an error when trying to create a outlook appointment item from a CalendarItem.", "Unknown Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } return(null); }
public void WindowsTimeZone_get(AppointmentItem ai, out String startTz, out String endTz) { Microsoft.Office.Interop.Outlook.TimeZone _startTz = null; Microsoft.Office.Interop.Outlook.TimeZone _endTz = null; try { _startTz = ai.StartTimeZone; _endTz = ai.EndTimeZone; startTz = _startTz.ID; endTz = _endTz.ID; } finally { _startTz = (Microsoft.Office.Interop.Outlook.TimeZone)OutlookOgcs.Calendar.ReleaseObject(_startTz); _endTz = (Microsoft.Office.Interop.Outlook.TimeZone)OutlookOgcs.Calendar.ReleaseObject(_endTz); } }
private TimeZoneInfo getWindowsTimezoneFromDescription(String tzDescription) { try { System.Collections.ObjectModel.ReadOnlyCollection <TimeZoneInfo> sysTZ = TimeZoneInfo.GetSystemTimeZones(); //First let's just search with what we've got TimeZoneInfo tzi = sysTZ.FirstOrDefault(t => t.DisplayName == tzDescription || t.StandardName == tzDescription || t.Id == tzDescription); if (tzi != null) { return(tzi); } log.Warn("Could not find timezone ID based on given description. Attempting some fuzzy logic..."); if (tzDescription.StartsWith("(GMT")) { log.Fine("Replace GMT with UTC"); String modTzDescription = tzDescription.Replace("(GMT", "(UTC"); tzi = sysTZ.FirstOrDefault(t => t.DisplayName == modTzDescription || t.StandardName == modTzDescription || t.Id == modTzDescription); if (tzi != null) { return(tzi); } log.Fine("Removing offset prefix"); modTzDescription = System.Text.RegularExpressions.Regex.Replace(modTzDescription, @"^\(UTC[+-]\d{1,2}:\d{0,2}\)\s+", "").Trim(); tzi = sysTZ.FirstOrDefault(t => t.StandardName == modTzDescription || t.Id == modTzDescription); if (tzi != null) { return(tzi); } } //Try searching just by timezone offset. This would at least get the right time for the appointment, eg if the tzDescription doesn't match //because they it is in a different language that the user's system data. Int16?offset = null; offset = TimezoneDB.GetTimezoneOffset(tzDescription); if (offset != null) { List <TimeZoneInfo> tzis = sysTZ.Where(t => t.BaseUtcOffset.Hours == offset).ToList(); if (tzis.Count == 0) { log.Warn("No timezone ID exists for organiser's GMT offset timezone " + tzDescription); } else if (tzis.Count == 1) { return(tzis.First()); } else { String tzCountry = tzDescription.Substring(tzDescription.LastIndexOf("/") + 1); if (string.IsNullOrEmpty(tzCountry)) { log.Warn("Could not determine country; and multiple timezones exist with same GMT offset of " + offset + ". Picking the first."); return(tzis.FirstOrDefault()); } else { List <TimeZoneInfo> countryTzis = tzis.Where(t => t.DisplayName.Contains(tzCountry)).ToList(); if (countryTzis.Count == 0) { log.Warn("Could not find timezone with GMT offset of " + offset + " for country " + tzCountry + ". Picking the first offset match regardless of country."); return(tzis.FirstOrDefault()); } else if (countryTzis.Count == 1) { return(countryTzis.First()); } else { log.Warn("Could not find unique timezone with GMT offset of " + offset + " for country " + tzCountry + ". Picking the first."); return(countryTzis.FirstOrDefault()); } } } } else { //Check if it's already an IANA value NodaTime.TimeZones.TzdbDateTimeZoneSource tzDBsource = TimezoneDB.Instance.Source; IEnumerable <NodaTime.TimeZones.TzdbZoneLocation> a = tzDBsource.ZoneLocations.Where(l => l.ZoneId == tzDescription); if (a.Count() >= 1) { log.Debug("It appears to be an IANA timezone already!"); Microsoft.Office.Interop.Outlook.TimeZone tz = WindowsTimeZone(tzDescription); if (tz != null) { return(TimeZoneInfo.FindSystemTimeZoneById(tz.ID)); } } } } catch (System.Exception ex) { log.Warn("Failed to get the organiser's timezone ID for " + tzDescription); OGCSexception.Analyse(ex); } return(null); }