protected virtual string OnFormatSpecifier(DateTimeSyntax syntax) { var specifier = new StringBuilder(); specifier.Append(syntax.HasFlag(DateTimeSyntax.WithDayName) ? "ddd, " : string.Empty); specifier.Append(syntax.HasFlag(DateTimeSyntax.TwoDigitDay) ? "dd" : "d"); specifier.Append(" MMM"); specifier.Append(syntax.HasFlag(DateTimeSyntax.FourDigitYear) ? " yyyy" : " yy"); if (syntax.HasFlag(DateTimeSyntax.UseAmPm)) { if (syntax.HasFlag(DateTimeSyntax.TwoDigitTime)) { specifier.Append(syntax.HasFlag(DateTimeSyntax.WithSeconds) ? " hh:mm:ss tt" : " hh:mm tt"); } else { specifier.Append(syntax.HasFlag(DateTimeSyntax.WithSeconds) ? " h:m:s tt" : " h:m tt"); } } else { if (syntax.HasFlag(DateTimeSyntax.TwoDigitTime)) { specifier.Append(syntax.HasFlag(DateTimeSyntax.WithSeconds) ? " HH:mm:ss" : " HH:mm"); } else { specifier.Append(syntax.HasFlag(DateTimeSyntax.WithSeconds) ? " H:m:s" : " H:m"); } } return(specifier.ToString()); }
/// <summary> /// Formats a date time offset according to the RFC 822 syntax specified /// in the syntax parameter. /// </summary> /// <param name="syntax">The syntax that is ued to format the date time.</param> /// <returns>An RFC 822 formatted date time.</returns> public string ToString(DateTimeSyntax syntax) { var format = new DateTimeFormat(syntax).FormatSpecifier; string result; if (syntax.HasFlag(DateTimeSyntax.NumericTimeZone)) { result = Instant.DateTime.ToString(format, CultureInfo.InvariantCulture); string timeZone = Instant.Offset.Ticks >= 0 ? "+" : "-"; timeZone += Instant.Offset.ToString("hhmm"); result += " " + timeZone; } else { result = Instant.UtcDateTime.ToString(format, CultureInfo.InvariantCulture); result += " UT"; } return(result); }
/// <summary> /// Converts the RFC 822 string representation of date and time to a /// System.DateTime and saves it together with the time zone offset /// in the Instant property. /// </summary> /// <param name="input">A date and time formatted according to RFC 822 /// syntax.</param> /// <param name="syntax">The syntax rules that should be used when /// interpreting the input.</param> public DateTimeRfc822(string input, DateTimeSyntax syntax) { ITimeZoneMapper timeZoneMapper; if (syntax.HasFlag(DateTimeSyntax.NumericTimeZone)) { timeZoneMapper = new NumericTimeZoneMapper(); } else { timeZoneMapper = new TimeZoneTable(); } var timeZoneMap = timeZoneMapper.Map(input.Substring(input.LastIndexOf(' ') + 1)); var src = input.TrimEnd(timeZoneMap.Identifier.ToCharArray()).Trim(); var format = new DateTimeFormat(syntax); var dateTime = DateTime.ParseExact(src, format.FormatSpecifier, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind | DateTimeStyles.AllowWhiteSpaces); Instant = new DateTimeOffset(dateTime, timeZoneMap.Offset); }