123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- #region License, Terms and Conditions
- #endregion
- namespace Jayrock
- {
- #region Imports
- using System;
- using System.Globalization;
- #endregion
-
-
-
-
- public sealed class InternetDate
- {
- private static readonly string[] _formats =
- {
- "dd MMM yyyy HH':'mm",
- "dd MMM yyyy HH':'mm':'ss",
- "ddd, dd MMM yyyy HH':'mm",
- "ddd, dd MMM yyyy HH':'mm':'ss",
- };
- public static DateTime Parse(string input)
- {
- if (input == null)
- throw new ArgumentNullException("input");
- if (input.Length < _formats[0].Length)
- throw new ArgumentException("input");
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- int zzz;
- int zoneSpaceIndex = input.LastIndexOf(' ');
- if (zoneSpaceIndex <= 0)
- throw new FormatException();
- string zone = input.Substring(zoneSpaceIndex + 1);
- if (zone.Length == 0)
- throw new FormatException("Missing time zone.");
- switch (zone)
- {
-
-
-
- case "UT" :
- case "GMT" : zzz = +0000; break;
-
-
-
-
- case "EDT" : zzz = -0400; break;
- case "EST" :
- case "CDT" : zzz = -0500; break;
- case "CST" :
- case "MDT" : zzz = -0600; break;
- case "MST" :
- case "PDT" : zzz = -0700; break;
- case "PST" : zzz = -0800; break;
-
-
-
- default :
- {
- if (zone.Length < 4)
- throw new FormatException("Length of local differential component must be at least 4 characters (HHMM).");
- try
- {
- zzz = int.Parse(zone, NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture);
- }
- catch (FormatException e)
- {
- throw new FormatException("Invalid local differential.", e);
- }
- break;
- }
- }
-
-
-
-
-
- input = input.Substring(0, zoneSpaceIndex).TrimEnd();
- DateTime time = DateTime.ParseExact(input, _formats, CultureInfo.InvariantCulture, DateTimeStyles.AllowInnerWhite);
-
-
-
-
- TimeSpan offset = new TimeSpan(zzz / 100, zzz % 100, 0);
- return time.Subtract(offset).ToLocalTime();
- }
- private InternetDate()
- {
- throw new NotSupportedException();
- }
- }
- }
|