public void TestToStringCore_TimestampMax_LowerS() { var value = new Timestamp.Value( 292277026596, 12, 4, 15, 30, 7, 999999999 ); Assert.That( TimestampStringConverter.ToString( "s", CultureInfo.InvariantCulture, ref value ), Is.EqualTo("292277026596-12-04T15:30:07Z") ); }
public void TestToStringCore_Zero_LowerS() { var value = new Timestamp.Value( 0, 1, 1, 0, 0, 0, 0 ); Assert.That( TimestampStringConverter.ToString( "s", CultureInfo.InvariantCulture, ref value ), Is.EqualTo("0000-01-01T00:00:00Z") ); }
public void TestToStringCore_TimestampMin_LowerS() { var value = new Timestamp.Value( -292277022657, 1, 27, 8, 29, 52, 0 ); Assert.That( TimestampStringConverter.ToString( "s", CultureInfo.InvariantCulture, ref value ), Is.EqualTo("-292277022657-01-27T08:29:52Z") ); }
public void TestToStringCore_YearMinus10000_LowerS() { var value = new Timestamp.Value( -10000, 10, 10, 10, 10, 10, 123456789 ); Assert.That( TimestampStringConverter.ToString( "s", CultureInfo.InvariantCulture, ref value ), Is.EqualTo("-10000-10-10T10:10:10Z") ); }
public void TestToStringCore_Year10000_UpperO() { var value = new Timestamp.Value( 10000, 10, 10, 10, 10, 10, 123456789 ); Assert.That( TimestampStringConverter.ToString( "O", CultureInfo.InvariantCulture, ref value ), Is.EqualTo("10000-10-10T10:10:10.123456789Z") ); }
public void TestToStringCore_YearMinus1000_UpperO() { var value = new Timestamp.Value( -1000, 1, 1, 0, 0, 0, 0 ); Assert.That( TimestampStringConverter.ToString( "O", CultureInfo.InvariantCulture, ref value ), Is.EqualTo("-1000-01-01T00:00:00.000000000Z") ); }
public static string ToString(string format, IFormatProvider formatProvider, ref Timestamp.Value value) { switch (format ?? DefaultFormat) { case "o": case "O": { // round-trip return(ToIso8601String(formatProvider, /* containsNanoseconds */ true, ref value)); } case "s": { // sortable(ISO-8601) return(ToIso8601String(formatProvider, /* containsNanoseconds */ false, ref value)); } default: { throw new ArgumentException("The specified format is not supported.", "format"); } } }
private static string ToIso8601String(IFormatProvider formatProvider, bool containsNanosecons, ref Timestamp.Value value) { var numberFormat = NumberFormatInfo.GetInstance(formatProvider); // Most cases are yyyy-MM-ddTHH:mm:ss[.fffffffff]Z -- 50 or 60 chars. var buffer = new StringBuilder(49 + (containsNanosecons ? 11 : 1)); buffer.Append(value.Year.ToString("0000", formatProvider)); buffer.Append(DateDelimiter); buffer.Append(value.Month.ToString("00", formatProvider)); buffer.Append(DateDelimiter); buffer.Append(value.Day.ToString("00", formatProvider)); buffer.Append(DateTimeDelimiter); buffer.Append(value.Hour.ToString("00", formatProvider)); buffer.Append(TimeDelimiter); buffer.Append(value.Minute.ToString("00", formatProvider)); buffer.Append(TimeDelimiter); buffer.Append(value.Second.ToString("00", formatProvider)); if (containsNanosecons) { buffer.Append(SubsecondDelimiter); buffer.Append(value.Nanoseconds.ToString("000000000", formatProvider)); } buffer.Append(UtcSign); return(buffer.ToString()); }
// Currently, custom format and normal date time format except 'o' or 'O' 's' are NOT supported. public static TimestampParseResult TryParseExact(string input, string format, IFormatProvider formatProvider, DateTimeStyles styles, out Timestamp result) { if (format != "o" && format != "O" && format != "s") { result = default(Timestamp); return(TimestampParseResult.UnsupportedFormat); } var numberFormat = NumberFormatInfo.GetInstance(formatProvider); var position = 0; if (!ParseWhitespace(input, ref position, (styles & DateTimeStyles.AllowLeadingWhite) != 0, /* isTrailing */ false)) { result = default(Timestamp); return(TimestampParseResult.LeadingWhitespaceNotAllowed); } long year; if (!ParseYear(input, ref position, numberFormat, out year)) { result = default(Timestamp); return(TimestampParseResult.InvalidYear); } if (!ParseDelimiter(input, ref position, DateDelimiter)) { result = default(Timestamp); return(TimestampParseResult.InvalidYearMonthDeilimiter); } var isLeapYear = Timestamp.IsLeapYearInternal(year); int month; if (!ParseDigitRange(input, 2, ref position, 1, 12, out month)) { result = default(Timestamp); return(TimestampParseResult.InvalidMonth); } if (!ParseDelimiter(input, ref position, DateDelimiter)) { result = default(Timestamp); return(TimestampParseResult.InvalidMonthDayDelimiter); } int day; if (!ParseDay(input, ref position, month, isLeapYear, out day)) { result = default(Timestamp); return(TimestampParseResult.InvalidDay); } if (!ParseDelimiter(input, ref position, DateTimeDelimiter)) { result = default(Timestamp); return(TimestampParseResult.InvalidDateTimeDelimiter); } int hour; if (!ParseDigitRange(input, 2, ref position, 0, 23, out hour)) { result = default(Timestamp); return(TimestampParseResult.InvalidHour); } if (!ParseDelimiter(input, ref position, TimeDelimiter)) { result = default(Timestamp); return(TimestampParseResult.InvalidHourMinuteDelimiter); } int minute; if (!ParseDigitRange(input, 2, ref position, 0, 59, out minute)) { result = default(Timestamp); return(TimestampParseResult.InvalidMinute); } if (!ParseDelimiter(input, ref position, TimeDelimiter)) { result = default(Timestamp); return(TimestampParseResult.InvalidMinuteSecondDelimiter); } int second; if (!ParseDigitRange(input, 2, ref position, 0, 59, out second)) { result = default(Timestamp); return(TimestampParseResult.InvalidSecond); } var nanosecond = 0; if (format != "s") { // "o" or "O" if (!ParseDelimiter(input, ref position, SubsecondDelimiter)) { result = default(Timestamp); return(TimestampParseResult.InvalidSubsecondDelimiter); } if (!ParseDigitRange(input, 9, ref position, 0, 999999999, out nanosecond)) { result = default(Timestamp); return(TimestampParseResult.InvalidNanoSecond); } } if (!ParseDelimiter(input, ref position, UtcSign)) { result = default(Timestamp); return(TimestampParseResult.MissingUtcSign); } if (!ParseWhitespace(input, ref position, (styles & DateTimeStyles.AllowTrailingWhite) != 0, /* isTrailing */ true)) { result = default(Timestamp); return(TimestampParseResult.TrailingWhitespaceNotAllowed); } if (position != input.Length) { result = default(Timestamp); return(TimestampParseResult.ExtraCharactors); } var components = new Timestamp.Value(); components.Year = year; components.Month = month; components.Day = day; components.Hour = hour; components.Minute = minute; components.Second = second; components.Nanoseconds = unchecked (( uint )nanosecond); try { result = Timestamp.FromComponents(ref components, isLeapYear); } catch (OverflowException) { result = default(Timestamp); return(TimestampParseResult.YearOutOfRange); } return(TimestampParseResult.Success); }