I'm using Telligent's RSS.Net fork to parse an XML Stream. The stream is well-formed and most of the desired elements are being parsed, but for some reason the pubData element isn't.
Inspecting the feed contents shows the pubData element nicely displayed, no issues with the RSS or the element contents. But... As I said, no parsing, either. Inspecting in Visual Studio shows:
PubDate = {1/01/0001 12:00:00 AM}
Which is clearly not the case and suspiciously generic for an plain incorrect parse, so I suspect it's just failing outright and defaulting to the NYE culmination, 1 (Grab the nearest hot person, quick! *{Snog}*)
The feeds I'm trying to parse are Google News feeds, so I'd like to assume they're working correctly.
Aha!
On a hunch I checked through the RSSReader code, and found that the date parsing, when dealing with a date that doesn't parse cleanly (Say, GMT+Offset):
Mon, 02 Nov 2009 12:34:56 GTM+10:00
Will remove the last 5 characters, leaving:
Mon, 02 Nov 2009 12:34:56 GTM+
Which make DateTime.Parse very very sad.
So, I'm going to alter the library to deal with the GMT time offset.
Related
I am currently porting code from go to c# and came across this (simplified) piece of code.
I do know that it converts a given string 171228175744.085 using a given format 060102150405.
The official docs only have examples using common formats like 2017-Feb-1, not this format (a possible timestamp?)
I do know that this will result in the time beeing 2017-12-28 17:57:44.085 +0000 UTC, but I do not have a clue how, because I have no information what the string 171228175744.085 and layout represent. I do know that some of this information is GPS related. So, my question is: Does anyone know how to do this in c#?
package main
import (
"fmt"
"time"
)
func main() {
t, err := time.Parse("060102150405", "171228175744.085")
if err == nil{
fmt.Println(t)
}
}
The docs around time.Format explain what the format means.
Quoting:
Format returns a textual representation of the time value formatted
according to layout, which defines the format by showing how the
reference time, defined to be
Mon Jan 2 15:04:05 -0700 MST 2006
The format string in your example: 060102150405 tells the time parser to look for the following:
06: year
01: month
02: day of the month
15: hour of the day
04: minute
05: second
This is a convenient way of telling the parser how it should interpret each number. If you look carefully, you'll see that numbers are not reused so when you say 06, the parser matches it to 2006.
In C#, you can use datetime.ParseExact. Something along the lines of:
DateTime.ParseExact(dateString, "yyMMddhhmmss", some_provider);
(note: I have not tried the C# snippet above. You may need to adjust it)
I'm based in the UK (GMT+1 time at the moment).
If I run this:
> DateTime.UtcNow.ToString("R") // Or...
> DateTime.Now.ToUniversalTime().ToString("R")
"Mon, 06 Oct 2014 10:20:00 GMT"
Correct answer.
If I now run the same, without UTC DateTime conversion:
> DateTime.Now.ToString("R")
"Mon, 06 Oct 2014 11:20:00 GMT"
The time printed is correct, but the timezone is wrong. I would expect instead:
"Mon, 06 Oct 2014 11:20:00" // Or..
"Mon, 06 Oct 2014 11:20:00 BST"
Question: Is this behaviour by design? Can I get the same output as with the "R" format, but with the correct timezone indicator?
It's definitely not a bug, it's the documented behaviour:
The custom format string is "ddd, dd MMM yyyy HH':'mm':'ss 'GMT'". When this standard format specifier is used, the formatting or parsing operation always uses the invariant culture.
...
Although the RFC 1123 standard expresses a time as Coordinated Universal Time (UTC), the formatting operation does not modify the value of the DateTime object that is being formatted. Therefore, you must convert the DateTime value to UTC by calling the DateTime.ToUniversalTime method before you perform the formatting operation. In contrast, DateTimeOffset values perform this conversion automatically; there is no need to call the DateTimeOffset.ToUniversalTime method before the formatting operation.
As I noted in a comment on the question, 10:20 GMT is correct, assuming that you ran the code shortly before asking the question: 11:20 GMT has not occurred yet.
So basically, when you follow the guidance in the documentation and call ToUniversalTime, it does the right thing. When you don't, it gives a misleading value - that's unfortunate, but part of the broken design of DateTime IMO.
You should consider at least using DateTimeOffset, or potentially using my Noda Time project instead.
#"(Sun|Mon|Tue|Wed|Thu|Fri|Sat)\,((31(?!\ (Feb(ruary)?|Apr(il)?|June?|(Sep(?=\b|t)t?|Nov)(ember)?)))|((30|29)(?!\ Feb(ruary)?))|(29(?=\ Feb(ruary)?\ (((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)))))|(0?[1-9])|1\d|2[0-8])\ (Jan(uary)?|Feb(ruary)?|Mar(ch)?|Apr(il)?|Jul(y)?)|Aug(ust)?|Oct(ober)?|(Sep(?=\b|t)t?|Nov|Dec)(ember)?)\((1[6-9]|[2-9]\d)\d{2})\s(20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d"
Hello, I use it to check for datetime formated as
Thu, 10 Nov 2010 13:08:06
I have runtime exception stating that is an invalid regex.
You should use DateTime.TryParse(). It's more suited to your needs.
Since you've tagged this with PHP...
You could use strtotime(); to convert the string to a timestamp, then create the date required in the correct format, as follows:
date("D, j M Y H:i:s", strtotime("Thu, 10 Nov 2010 13:08:06"));
Then at least however it is written, it will be converted correctly (belongs as the date is (of course) valid.
Just an alternative to regex....
This compiles :
Regex regexObj = new Regex(#"(Sun|Mon|Tue|Wed|Thu|Fri|Sat),((31(?! (Feb(ruary)?|Apr(il)?|June?|(Sep(?=\b|t)t?|Nov)(ember)?)))|((30|29)(?! Feb(ruary)?))|(29(?= Feb(ruary)? (((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)))))|(0?[1-9])|1\d|2[0-8]) (Jan(uary)?|Feb(ruary)?|Mar(ch)?|Apr(il)?|Jul(y)?)|Aug(ust)?|Oct(ober)?|(Sep(?=\b|t)t?|Nov|Dec)(ember)?\((1[6-9]|[2-9]\d)\d{2}\s(20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d");
You have a couple of two many ). Other than that, good luck debugging this mess..
I create RSS items. I've a column called date_published, which is DateTime column in MS SQL database. e.g. it contains "2011-05-04 15:19:05.630"
Below is the C# code date I re-generate RSS acceptable date format (input database field).
Input:
Convert.ToDateTime(item.date_published).ToString("r")
Output:
Wed, 04 May 2011 15:19:05 GMT
So I looked with Notepad in the XML file and the result look above.
Problem:
When the users receive this item in Outlook RSS feedreader, it displays +2 hours ahead. So it displays in email outlook received date 2011-05-04 17:19:05
So I change my code from:
item.pubdate_published.Value.ToString("r")
To:
item.pubdate_published.Value.ToString("ddd dd MMM yyyy HH:mm:ss +2 G'M'T")
This gives me exact what I want like this : mer, 04 mai 2011 15:19:05 +2 GMT.
PROBLEM:
My RSS XMl file shows like that too (above), but my OUTLOOK email received date is 06/January/2011 09:32
So the only issue I can think is the language problem... Because the dates are generated in Dutch or French.
if this is the issue: How do I write the dates in English?
I found the problem... the datetime in RSS XML file must be in English. So we were generating the date sometimes Dutch and sometimes in French.
this is what I did:
item.pubdate_published.Value.ToString("r").replace("GMT",("+2 GMT");
the hours and the dates are correct now.
be careful doing like this : ToString("ddd dd MMM yyyy HH:mm:ss +2 G'M'T") will generate specific language and not always English. ("r") always generate in English.
So, i have this string "Date: Mon Jan 03 2011 19:29:44 GMT+0200", and when i use DateTime.Parse(date).ToString(); i'm getting "String was not recognized as a valid DateTime."
If i remove the '+0200' part it works ok, but ofcourse it doesn't show the correct local time.
What's wrong with that?
From the documentation, it seems that DateTime.Parse() only understands:
The GMT designator, used alone, e.g. Mon, Jan 03 2011 17:29:44 GMT, or
A time zone offset specified without the GMT designator, e.g. Mon, Jan 03 2011 19:29:44+02:00.
You might want to convert your date string to the second form.
It just means that the time zone offset isn't an expected part of the default format strings.
If you know what format you're expecting, I suggest you call DateTime.ParseExact (or DateTime.TryParseExact), specifying the format(s) to try. Look at the documentation for custom date/time format strings for more details.
You have two mistakes.
First - don`t use Parse method. More correct is TryParse.
Second - you will have globalisation issues, when you use Parse or TryParse without arguments.
For example, see this code:
DateTime.Parse( "01.02.2011" ); In the USA it is 2nd of January. In the Germany it is 1st of February.
So, I recomment you to use formats from this article.