I am getting mail archives with dates like this in it.
Wed, 17 Dec 1997 13:36:23 +2
Mon, 16 Jun 1997 15:41:52 EST
Tue, 15 Jul 1997 14:37:00 EDT
Tue, 5 Aug 1997 08:37:56 PST
Tue, 5 Aug 1997 15:46:16 PDT
Thu, 5 Mar 1998 08:44:19 MET
Mon, 8 Nov 1999 17:49:25 GMT
Thu, 24 Feb 94 20:06:06 MST
Mon, 19 Dec 2005 14:17:06 CST
Thu, 14 Sep 95 19:15 CDT
Sat, 22 Feb 1997 05:16:55 UT
Mon, 8 Jul 1996 15:48:54 GMT-5
Mon, 25 Nov 1996 17:10:28 WET
Mon, 6 Jan 1997 23:43:48 UT
Fri, 13 Jun 1997 16:44:03 -0400
Ask is to convert this time into UTC. This is how I am trying to do this.
static void Main(string[] args)
{
var possibleValues = new string[]
{
"Mon, 29 Sep 2014 08:33:35 +0200"
, "Fri, 29 Jun 2001 07:53:01 -0700"
,"Fri, 26 Sep 2014 15:57:04 +0000"
,"Wed, 17 Dec 1997 13:36:23 +2"
, "Fri, 13 Jun 1997 16:44:03 -0400"
, "Mon, 16 Jun 1997 15:41:52 EST"
, "Tue, 15 Jul 1997 14:37:00 EDT"
, "Tue, 5 Aug 1997 08:37:56 PST"
, "Tue, 5 Aug 1997 15:46:16 PDT"
, "Thu, 5 Mar 1998 08:44:19 MET"
, "Mon, 8 Nov 1999 17:49:25 GMT"
, "Thu, 24 Feb 94 20:06:06 MST"
, "Mon, 19 Dec 2005 14:17:06 CST"
, "Thu, 14 Sep 95 19:15:00 CDT"
, "Sat, 22 Feb 1997 05:16:55 UT"
, "Mon, 8 Jul 1996 15:48:54 GMT-5"
, "Mon, 25 Nov 1996 17:10:28 WET"
, "Mon, 6 Jan 1997 23:43:48 UT"
};
foreach (var item in possibleValues)
{
var dateParts = item.Split(' ');
var lastItem = dateParts[dateParts.Length - 1];
if (lastItem.StartsWith("+") || lastItem.StartsWith("-"))
{
try
{
DateTimeOffset offset = DateTimeOffset.Parse(item, CultureInfo.InvariantCulture);
Debug.WriteLine("Input: {0}, UTC Time: {1}", item, offset.UtcDateTime);
}
catch (Exception exc)
{
Debug.WriteLine("Failed - {0}, Error Message: {1}", item, exc.Message);
}
}
else
{
//Sometimes year is a two digit number and sometimes it is 4 digit number.
string dateFormat = string.Format("ddd, {0} MMM {1} {2}:mm:ss {3}", new string('d', dateParts[1].Length), new string('y', dateParts[3].Length), int.Parse(dateParts[4].Substring(0, 2)) > 12 ? "HH" : "hh", lastItem);
try
{
DateTimeOffset offset = DateTimeOffset.ParseExact(item, dateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None);
Debug.WriteLine("Input: {0}, UTC Time: {1}", item, offset.UtcDateTime);
}
catch (Exception exc)
{
Debug.WriteLine("Failed - {0}, DateFormat Tried: {1}, Error Message: {2}", item, dateFormat, exc.Message);
}
}
}
}
I am not able figure out how to handle all the cases. I am open to use Noda time too.
I have gone thru many links from SO and Google to find this answer but wasn't able implement any answer from those links. In case if you know the similar question then please let me know.
I have already gone thru below links.
Convert.ToDateTime Method
Converting between types
daylight-saving-time-and-time-zone-best-practices
SO Tags timezone
Coding Best Practices Using DateTime in the .NET Framework
conversion-of-a-utc-date-time-string-in-c-sharp