Exemplo n.º 1
0
        public static TimeZoneInfo ParseTimeZoneId(string id)
        {
            // See if explicit timezone offset
            if (id[0] == '-' || id[0] == '+')
            {
                TimeZoneTag tzt;
                if (TimeZoneTag.TryParse(id, out tzt))
                {
                    string idstr = $"(UTC{id})";
                    string name  = $"{idstr} Custom";
                    return(TimeZoneInfo.CreateCustomTimeZone(idstr, tzt.UtcOffset, name, name));
                }
            }

            // Check the abbreviations
            foreach (var pair in s_tzAbbreviations)
            {
                if (string.Equals(id, pair.Key, StringComparison.OrdinalIgnoreCase))
                {
                    try
                    {
                        var tz = TimeZoneInfo.FindSystemTimeZoneById(pair.Value);
                        if (tz != null)
                        {
                            return(tz);
                        }
                    }
                    catch
                    {
                        // Do nothing. FindTimeZoneById throws an exception if the value is not found (bad design).
                        // In that case we just skip the abbreviation.
                    }
                    break;
                }
            }

            // Look it up directly
            try
            {
                var tz = TimeZoneInfo.FindSystemTimeZoneById(id);
                if (tz != null)
                {
                    return(tz);
                }
            }
            catch
            {
                // Do nothing. FindTimeZoneById throws an exception if the value is not found.
            }
            return(null);
        }
Exemplo n.º 2
0
        protected virtual void LoadMetadata()
        {
            if (m_uri == null)
            {
                return;
            }

            try
            {
                using (var ps = WinShell.PropertyStore.Open(m_uri.LocalPath, false))
                {
                    // See if timezone and precision info are included
                    int         precision = 0;
                    TimeZoneTag tz        = null;
                    foreach (var pair in MetaTag.Extract(ps.GetValue(s_pkComment) as string))
                    {
                        if (pair.Key.Equals("datePrecision", StringComparison.OrdinalIgnoreCase))
                        {
                            if (!int.TryParse(pair.Value, out precision))
                            {
                                precision = 0;
                            }
                        }

                        if (pair.Key.Equals("timezone", StringComparison.OrdinalIgnoreCase))
                        {
                            if (!TimeZoneTag.TryParse(pair.Value, out tz))
                            {
                                tz = null;
                            }
                        }
                    }

                    // Get the date the photo or video was taken.
                    {
                        object date = ps.GetValue(s_pkDateTaken);
                        if (date == null)
                        {
                            date = ps.GetValue(s_pkDateEncoded);
                        }
                        if (date != null && date is DateTime)
                        {
                            var dt = new DateTag((DateTime)date, tz, precision);
                            dt = dt.ResolveTimeZone(TimeZoneInfo.Local);
                            var ci = CultureInfo.CurrentCulture;

                            // Use my preferred format if US English
                            string format = ci.Name.Equals(c_usCultureName, StringComparison.Ordinal)
                                ? c_usOverrideDateFormat
                                : ci.DateTimeFormat.FullDateTimePattern;

                            m_dateTaken = dt.ToString(format, ci);
                        }
                    }

                    var tags = new List <string>();

                    // Get title
                    string title = ps.GetValue(s_pkTitle) as string;
                    if (!string.IsNullOrEmpty(title))
                    {
                        tags.Add(title);
                    }

                    // Get keywords
                    var keywords = ps.GetValue(s_pkKeywords) as IEnumerable <string>;
                    if (keywords != null)
                    {
                        tags.AddRange(keywords);
                    }

                    if (tags.Count > 0)
                    {
                        Tags = tags;
                    }
                }
            }
            catch (Exception err)
            {
                Debug.WriteLine(err.ToString());
            }
        }