@@ -55,6 +55,11 @@ public static DateTime FromiCalendar(string iCalendar)
5555 time = result ; // localtime
5656 }
5757
58+ if ( time . Length < 15 )
59+ {
60+ return DateTime . MaxValue ;
61+ }
62+
5863 // We now have the time string to parse, and we'll adjust
5964 // to UTC or timezone after parsing
6065 string year = time . Substring ( 0 , 4 ) ;
@@ -65,43 +70,56 @@ public static DateTime FromiCalendar(string iCalendar)
6570 string second = time . Substring ( 13 , 2 ) ;
6671
6772 // Check if any of the date time parts is non-numeric
68- if ( ! IsNumeric ( year ) )
73+ if ( ! int . TryParse ( year , out int intYear ) ||
74+ intYear < DateTime . MinValue . Year ||
75+ intYear > DateTime . MaxValue . Year )
6976 {
7077 return DateTime . MaxValue ;
7178 }
72- else if ( ! IsNumeric ( month ) )
79+ if ( ! int . TryParse ( month , out int intMonth ) ||
80+ intMonth < DateTime . MinValue . Month ||
81+ intMonth > DateTime . MaxValue . Month )
7382 {
7483 return DateTime . MaxValue ;
7584 }
76- else if ( ! IsNumeric ( day ) )
85+ if ( ! int . TryParse ( day , out int intDay ) ||
86+ intDay < DateTime . MinValue . Day ||
87+ intDay > DateTime . MaxValue . Day ||
88+ intDay > DateTime . DaysInMonth ( intYear , intMonth ) )
7789 {
7890 return DateTime . MaxValue ;
7991 }
80- else if ( ! IsNumeric ( hour ) )
92+ if ( ! int . TryParse ( hour , out int intHour ) ||
93+ intHour < DateTime . MinValue . Hour ||
94+ intHour > DateTime . MaxValue . Hour )
8195 {
8296 return DateTime . MaxValue ;
8397 }
84- else if ( ! IsNumeric ( minute ) )
98+ if ( ! int . TryParse ( minute , out int intMinute ) ||
99+ intMinute < DateTime . MinValue . Minute ||
100+ intMinute > DateTime . MaxValue . Minute )
85101 {
86102 return DateTime . MaxValue ;
87103 }
88- else if ( ! IsNumeric ( second ) )
104+ if ( ! int . TryParse ( second , out int intSecond ) ||
105+ intSecond < DateTime . MinValue . Second ||
106+ intSecond > DateTime . MaxValue . Second )
89107 {
90108 return DateTime . MaxValue ;
91109 }
92110
93111 DateTime dt = new (
94- Convert . ToInt32 ( year ) ,
95- Convert . ToInt32 ( month ) ,
96- Convert . ToInt32 ( day ) ,
97- Convert . ToInt32 ( hour ) ,
98- Convert . ToInt32 ( minute ) ,
99- Convert . ToInt32 ( second ) ) ;
112+ intYear ,
113+ intMonth ,
114+ intDay ,
115+ intHour ,
116+ intMinute ,
117+ intSecond ) ;
100118
101119 if ( utc )
102120 {
103121 // Convert the Kind to DateTimeKind.Utc
104- dt = new DateTime ( 0 , DateTimeKind . Utc ) . AddTicks ( dt . Ticks ) ;
122+ dt = new DateTime ( dt . Ticks , DateTimeKind . Utc ) ;
105123 }
106124 else if ( tzid )
107125 {
0 commit comments