示例#1
0
        private DateTime WindowsTimeZone(EventDateTime time)
        {
            DateTime theDate = time.DateTime ?? DateTime.Parse(time.Date);

            if (time.TimeZone == null)
            {
                return(theDate);
            }

            LocalDateTime local     = new LocalDateTime(theDate.Year, theDate.Month, theDate.Day, theDate.Hour, theDate.Minute);
            DateTimeZone  zone      = DateTimeZoneProviders.Tzdb[TimezoneDB.FixAlexa(time.TimeZone)];
            ZonedDateTime zonedTime = local.InZoneLeniently(zone);
            DateTime      zonedUTC  = zonedTime.ToDateTimeUtc();

            log.Fine("IANA Timezone \"" + time.TimeZone + "\" mapped to \"" + zone.Id.ToString() + "\" with a UTC of " + zonedUTC.ToString("dd/MM/yyyy HH:mm:ss"));
            return(zonedUTC);
        }
        private Microsoft.Office.Interop.Outlook.TimeZone WindowsTimeZone(string ianaZoneId)
        {
            ianaZoneId = TimezoneDB.FixAlexa(ianaZoneId);

            Microsoft.Office.Interop.Outlook.TimeZones tzs = oApp.TimeZones;
            var utcZones = new[] { "Etc/UTC", "Etc/UCT", "UTC", "Etc/GMT" };

            if (utcZones.Contains(ianaZoneId, StringComparer.OrdinalIgnoreCase))
            {
                log.Fine("Timezone \"" + ianaZoneId + "\" mapped to \"UTC\"");
                return(tzs["UTC"]);
            }

            NodaTime.TimeZones.TzdbDateTimeZoneSource tzDBsource = TimezoneDB.Instance.Source;

            // resolve any link, since the CLDR doesn't necessarily use canonical IDs
            var links = tzDBsource.CanonicalIdMap
                        .Where(x => x.Value.Equals(ianaZoneId, StringComparison.OrdinalIgnoreCase))
                        .Select(x => x.Key);

            // resolve canonical zones, and include original zone as well
            var possibleZones = tzDBsource.CanonicalIdMap.ContainsKey(ianaZoneId)
                ? links.Concat(new[] { tzDBsource.CanonicalIdMap[ianaZoneId], ianaZoneId })
                : links;

            // map the windows zone
            var mappings = tzDBsource.WindowsMapping.MapZones;
            var item     = mappings.FirstOrDefault(x => x.TzdbIds.Any(possibleZones.Contains));

            if (item == null)
            {
                throw new System.ApplicationException("Timezone \"" + ianaZoneId + "\" has no mapping.");
            }
            log.Fine("Timezone \"" + ianaZoneId + "\" mapped to \"" + item.WindowsId + "\"");

            return(tzs[item.WindowsId]);
        }