DDay.iCal.Serialization.iCalendar.DateTimeSerializer.Deserialize C# (CSharp) Method

Deserialize() public method

public Deserialize ( TextReader tr ) : object
tr TextReader
return object
        public override object Deserialize(TextReader tr)
        {
            string value = tr.ReadToEnd();

            IDateTime dt = CreateAndAssociate() as IDateTime;
            if (dt != null)
            {
                // Decode the value as necessary
                value = Decode(dt, value);

                Match match = Regex.Match(value, fullPattern, RegexOptions.IgnoreCase);
                if (!match.Success)
                    match = Regex.Match(value, dateOnlyPattern, RegexOptions.IgnoreCase);

                if (!match.Success)
                    return null;
                else
                {
                    DateTime now = DateTime.Now;

                    int year = now.Year;
                    int month = now.Month;
                    int date = now.Day;
                    int hour = 0;
                    int minute = 0;
                    int second = 0;

                    if (match.Groups[1].Success)
                    {
                        dt.HasDate = true;
                        dt.HasTime = false;                
                        year = Convert.ToInt32(match.Groups[2].Value);
                        month = Convert.ToInt32(match.Groups[3].Value);
                        date = Convert.ToInt32(match.Groups[4].Value);
                    }
                    if (match.Groups.Count >= 6 && match.Groups[5].Success)
                    {
                        // If 'VALUE=DATE' is present, then
                        // let's force dt.HasTime to false.
                        // NOTE: Fixes bug #3534283 - DateTimeSerializer bug on deserializing dates without time
                        if (dt.Parameters.ContainsKey("VALUE"))
                        {
                            var valueType = dt.Parameters.Get("VALUE");
                            if (valueType != "DATE")
                                dt.HasTime = true;
                        }
                        else
                        {
                            dt.HasTime = true;
                        }

                        if (dt.HasTime)
                        {
                            hour = Convert.ToInt32(match.Groups[6].Value);
                            minute = Convert.ToInt32(match.Groups[7].Value);
                            second = Convert.ToInt32(match.Groups[8].Value);
                        }
                    }

                    if (match.Groups[9].Success)
                        dt.IsUniversalTime = true;

                    dt.Value = CoerceDateTime(year, month, date, hour, minute, second, DateTimeKind.Utc);
                    return dt;
                }
            }

            return null;
        }

Usage Example

Example #1
0
 public iCalDateTime(string value)
 {
     DateTimeSerializer serializer = new DateTimeSerializer();
     CopyFrom(serializer.Deserialize(new StringReader(value)) as ICopyable);
 }