public void TryToDateTimeLongTest() { DateTime dt; Assert.True(ParseDateTime.TryToDateTime(20180628L, 121532L, out dt)); Assert.Equal(new DateTime(2018, 06, 28, 12, 15, 32), dt); // While the 35th is not a valid day of the month, so false from failed new DateTime. Assert.False(ParseDateTime.TryToDateTime(20180635L, 121532L, out dt)); }
public void TryToNullableDateTimeTest() { DateTime? dt; Assert.True(ParseDateTime.TryToDateTime("2018-01-01", out dt)); Assert.Equal(new DateTime(2018, 1, 1), dt); Assert.True(ParseDateTime.TryToDateTime("5", out dt)); Assert.Equal(5, (int)DateTime.Now.Subtract((DateTime)dt).TotalDays); // Don't need to test all of the other format variations because any // non-null string will use the non-nullable TryToDateTime(). Assert.False(ParseDateTime.TryToDateTime("blah", out dt)); Assert.Equal(null, dt); }
public void TryToDateTimeTest() { DateTime dt; Assert.True(ParseDateTime.TryToDateTime("2018-01-01", out dt)); Assert.Equal(new DateTime(2018, 1, 1), dt); Assert.True(ParseDateTime.TryToDateTime("01-Jan-2018", out dt)); Assert.Equal(new DateTime(2018, 1, 1), dt); // Testing the relative datetime is tricky because a new DateTime will have a different number of // milliseconds. Zeroing that out would be easy but then there would be the occasional fail when // the two DateTimes cross a second boundary during testing. Assert.True(ParseDateTime.TryToDateTime("5", out dt)); Assert.Equal(5, (int)DateTime.Now.Subtract(dt).TotalDays); // This subtraction will always be slightly more than 5. Assert.False(ParseDateTime.TryToDateTime("blah", out dt)); Assert.Equal(default(DateTime), dt); }
public override int getRss(HttpResponse res) { var items = xEle.Descendants(nodeName); string error = ""; int counter = 0; DateTime?maxdate; int cantTitle = 0; maxdate = null; foreach (var item in items) { DateTime pData = DateTime.Now; try { pData = Convert.ToDateTime(item.Element("pubDate").Value); } catch (Exception ex) { pData = new ParseDateTime(item.Element("pubDate").Value).getDateTime(); } DataRow drNew = new dsNewsReader().Tables["News"].NewRow(); Uri myUri = new Uri(item.Element("guid").Value); string host = myUri.Host; string strChannel = myUri.Segments[1].Replace("/", ""); if (strChannel == "economia") { var c = "econo"; } if (strChannel == "deportes-2") { var c = "DEPOR"; strChannel = "Deportes"; } if ((strChannel == "Política" || strChannel == "politica" || strChannel == "Economía" || strChannel == "economia" || strChannel == "Deportes" || strChannel == "El Mundo" || strChannel == "Sociedad" || strChannel == "Policiales" || strChannel == "noticias")) { int vChannel = 0; // int cantTitle=0; DataTable dtChannel = pNew.News_GetChannelIdPorDescripcion(strChannel).Tables[0]; try{ vChannel = (int)dtChannel.Rows[0]["IdChannel"]; var vTitle = replaceCDATA(item.Element("title").Value); maxdate = pNew.getMaxPublishedDate(IdNewsPaper, vChannel); cantTitle = pNew.countByTitle(IdNewsPaper, vChannel, vTitle); drNew["IdNewsPaper"] = IdNewsPaper; drNew["IdChannel"] = vChannel; drNew["Title"] = vTitle; drNew["Link"] = item.Element("guid").Value; drNew["Description"] = RemoveHTML(replaceCDATA(item.Element("description").ToString())); drNew["PublishDate"] = pData; drNew["Author"] = ""; } catch (Exception ex) { // throw ex; error = error + strChannel + ex.Message.ToString(); } if (maxdate == null || pData > maxdate || cantTitle == 0) { counter++; pNew.newsInsert(drNew); } } } if (error != "") { throw new Exception(error); } return(counter); }
public override int getRss(HttpResponse res) { IEnumerable <XElement> items; DateTime?maxdate; int clarinCounter = 0; try { items = xEle.Descendants(nodeName); } catch (Exception ex) { throw new Exception("<h4> xEle.Descendants(nodeName)" + ex.ToString() + " - " + this.url + "</h4>"); } try { maxdate = pNew.getMaxPublishedDate(IdNewsPaper, IdChannel); } catch (Exception ex) { throw new Exception("<h3> getMaxPublishedDate(IdNewsPaper=" + IdNewsPaper.ToString() + ", IdChannel=" + IdChannel.ToString() + " --- " + ex.ToString() + "</h3>"); } foreach (var item in items) { string itemDescription = HttpUtility.HtmlDecode(item.Element("description").ToString()); var iDescription = new HtmlDocument(); iDescription.LoadHtml(itemDescription); DateTime pData = DateTime.Now; try{ pData = Convert.ToDateTime(item.Element("pubDate").Value); }catch (Exception ex) { pData = new ParseDateTime(item.Element("pubDate").Value).getDateTime(); } iDescription.DocumentNode.Descendants() .Where(n => n.Name == "img" || n.Name == "br" || n.Name == "a") .ToList() .ForEach(n => n.Remove()); DataRow drNew = new dsNewsReader().Tables["News"].NewRow(); try{ // res.Write("<p style=margin:0;padding:0;>" + item.Element("guid").Value + "</p>"); drNew["IdNewsPaper"] = IdNewsPaper; drNew["IdChannel"] = IdChannel; drNew["Title"] = item.Element("title").Value; drNew["Description"] = iDescription.DocumentNode.InnerText.Replace("<![CDATA[", "").Replace("]]>", ""); drNew["PublishDate"] = pData; drNew["Link"] = item.Element("guid").Value; drNew["Author"] = ""; }catch (Exception ex) { throw new Exception("<h4> drNew: " + " --- " + ex.ToString() + "</h3>"); } //if (item.Element("enclosure") != null && item.Element("enclosure").HasAttributes){ // item.Element("enclosure").Attribute("url").Value; //} try { if (maxdate == null || pData > maxdate) { clarinCounter++; pNew.newsInsert(drNew); } } catch (Exception ex) { throw new Exception("<h3> pNew.newsInsert(drNew);" + ex.ToString() + "</h3>"); } } return(clarinCounter); }