views:

210

answers:

3

I am trying to parse a french date to a DateTime object with no luck so far. Is there a way? to do that.

String foo = "mar, 20 avr 2010 09:00:00 -0500";

I've Already tried parsing with a different culture and changing the culture of the thread.

System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-CA",true);
CultureInfo culture = new CultureInfo("fr-CA",true);
DateTime.Parse(foo,culture,DateTimeStyles.AdjustToUniversal);
+1  A: 

The closest I think you're going to get is

DateTime.ParseExact("mar., 01 juin 2010 12:11:53 -04:00", "ddd, dd MMM yyyy hh:mm:ss zzz", culture); // extra period after "mar"
// or
DateTime.ParseExact("mardi, 01 juin 2010 12:12:33 -04:00", "dddd, dd MMM yyyy hh:mm:sszzz", culture ); // full day name

Why DateTime.Parse doesn't work:

The documentation for DateTime.Parse says that

The s parameter must contain the representation of a date and time in one of the formats returned by the DateTimeFormatInfo.GetAllDateTimePatterns() method of the current culture.

On my computer, using this code, I get the following formats. It looks like your pattern isn't in the list.

    CultureInfo culture = new CultureInfo("fr-CA", true);

    foreach( string dateTimePattern in culture.DateTimeFormat.GetAllDateTimePatterns())
    {
        Debug.WriteLine(dateTimePattern);
    }
  • yyyy-MM-dd
  • yy-MM-dd
  • dd-MM-yy
  • yy MM dd
  • dd/MM/yy
  • d MMMM yyyy
  • d MMM yyyy
  • d MMMM yyyy HH:mm
  • d MMMM yyyy H:mm
  • d MMMM yyyy H' h 'mm
  • d MMM yyyy HH:mm
  • d MMM yyyy H:mm
  • d MMM yyyy H' h 'mm
  • d MMMM yyyy HH:mm:ss
  • d MMMM yyyy H:mm:ss
  • d MMMM yyyy H' h 'mm
  • d MMMM yyyy H:mm
  • d MMM yyyy HH:mm:ss
  • d MMM yyyy H:mm:ss
  • d MMM yyyy H' h 'mm
  • d MMM yyyy H:mm
  • yyyy-MM-dd HH:mm
  • yyyy-MM-dd H:mm
  • yyyy-MM-dd H' h 'mm
  • yy-MM-dd HH:mm
  • yy-MM-dd H:mm
  • yy-MM-dd H' h 'mm
  • dd-MM-yy HH:mm
  • dd-MM-yy H:mm
  • dd-MM-yy H' h 'mm
  • yy MM dd HH:mm
  • yy MM dd H:mm
  • yy MM dd H' h 'mm
  • dd/MM/yy HH:mm
  • dd/MM/yy H:mm
  • dd/MM/yy H' h 'mm
  • yyyy-MM-dd HH:mm:ss
  • yyyy-MM-dd H:mm:ss
  • yyyy-MM-dd H' h 'mm
  • yyyy-MM-dd H:mm
  • yy-MM-dd HH:mm:ss
  • yy-MM-dd H:mm:ss
  • yy-MM-dd H' h 'mm
  • yy-MM-dd H:mm
  • dd-MM-yy HH:mm:ss
  • dd-MM-yy H:mm:ss
  • dd-MM-yy H' h 'mm
  • dd-MM-yy H:mm
  • yy MM dd HH:mm:ss
  • yy MM dd H:mm:ss
  • yy MM dd H' h 'mm
  • yy MM dd H:mm
  • dd/MM/yy HH:mm:ss
  • dd/MM/yy H:mm:ss
  • dd/MM/yy H' h 'mm
  • dd/MM/yy H:mm
  • d MMMM
  • d MMMM
  • yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK
  • yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK
  • ddd, dd MMM yyyy HH':'mm':'ss 'GMT'
  • ddd, dd MMM yyyy HH':'mm':'ss 'GMT'
  • yyyy'-'MM'-'dd'T'HH':'mm':'ss
  • HH:mm
  • H:mm
  • H' h 'mm
  • HH:mm:ss
  • H:mm:ss
  • H' h 'mm
  • H:mm
  • yyyy'-'MM'-'dd HH':'mm':'ss'Z'
  • d MMMM yyyy HH:mm:ss
  • d MMMM yyyy H:mm:ss
  • d MMMM yyyy H' h 'mm
  • d MMMM yyyy H:mm
  • d MMM yyyy HH:mm:ss
  • d MMM yyyy H:mm:ss
  • d MMM yyyy H' h 'mm
  • d MMM yyyy H:mm
  • MMMM, yyyy
  • MMMM, yyyy
Greg
+3  A: 

You can only parse (with Parse or ParseExact) what you can create when formatting a DateTime.

The closest custom format specifier to your example input is probably something like this:

ddd, dd MMM yyyy HH':'mm':'ss zzz

Code:

CultureInfo culture = new CultureInfo("fr-CA", true);

var f = new DateTimeOffset(2010, 04, 20, 09, 00, 00, TimeSpan.FromHours(-5))
            .ToString("ddd, dd MMM yyyy HH':'mm':'ss zzz", culture);

This produces the following result:

"mar., 20 avr. 2010 09:00:00 -05:00"

As you can see, the short day and short month specifier (ddd and MMM) add a . after the name, and the time-zone specifier (zzz) inserts a :.

I believe it's not possible to trick ToString into generating the desired output, and thereby also not to parse the result with ParseExact. I guess you have to parse the string yourself using plain old string manipulation.

dtb
Just curious, what does the quote character do in the format string?
Greg
@Greg: Single quotes designate a string literal within the format specifier. You can use it, for example, to literally insert a `:` character into the output instead of the culture-dependent time separator (which the `:` format specifier stands for).
dtb
Cool, did not know that, thanks.
Greg
I am getting this date from an RSSFeed pubDate element it looks like the person making the feeds didn't properly abbreviate it. I guess I'll have to add the period using good old fashioned string manipulation. Thanks for the information!
Edward
@Edward: According to the RSS specification, the pubDate is not supposed to be localized ("All date-times in RSS conform to the Date and Time Specification of RFC 822, ..."). Tell the person making the feeds to fix it, don't start working around buggy RSS feeds.
dtb
+1  A: 

The day name and month name are not properly abbreviated, they need a period. If you can massage the string then you could make it work:

using System;
using System.Globalization;

class Program {
    static void Main(string[] args) {
        String foo = "mar., 20 avr. 2010 09:00:00 -0500";
        var cvt = CultureInfo.GetCultureInfo("fr-CA").DateTimeFormat;
        var dt = DateTimeOffset.Parse(foo, cvt, DateTimeStyles.RoundtripKind);
        Console.WriteLine(dt);
        Console.ReadLine();
    }
}
Hans Passant