public void IConvertible() { var date = new FudgeDate(19870304); Assert.Equal(TypeCode.Object, date.GetTypeCode()); Assert.Throws<InvalidCastException>(() => date.ToBoolean(null)); Assert.Throws<InvalidCastException>(() => date.ToByte(null)); Assert.Throws<InvalidCastException>(() => date.ToChar(null)); Assert.Equal(new DateTime (1987, 3, 4), date.ToDateTime(null)); Assert.Throws<InvalidCastException>(() => date.ToDecimal(null)); Assert.Throws<InvalidCastException>(() => date.ToDouble(null)); Assert.Throws<InvalidCastException>(() => date.ToInt16(null)); Assert.Throws<InvalidCastException>(() => date.ToInt32(null)); Assert.Throws<InvalidCastException>(() => date.ToInt64(null)); Assert.Throws<InvalidCastException>(() => date.ToSByte(null)); Assert.Throws<InvalidCastException>(() => date.ToSingle(null)); Assert.Equal("1987-03-04", date.ToString(null)); Assert.Throws<InvalidCastException>(() => date.ToUInt16(null)); Assert.Throws<InvalidCastException>(() => date.ToUInt32(null)); Assert.Throws<InvalidCastException>(() => date.ToUInt64(null)); Assert.Equal(new DateTime(1987, 3, 4), date.ToType(typeof(DateTime), null)); Assert.Equal(new DateTimeOffset(1987, 3, 4, 0, 0, 0, TimeSpan.Zero), date.ToType(typeof(DateTimeOffset), null)); Assert.Equal(new FudgeDateTime(1987, 3, 4, 0, 0, 0, 0, FudgeDateTimePrecision.Day), date.ToType(typeof(FudgeDateTime), null)); Assert.Equal("1987-03-04", date.ToType(typeof(string), null)); Assert.Throws<InvalidCastException>(() => date.ToType(typeof(int), null)); }
public void IConvertible() { var date = new FudgeDate(19870304); Assert.Equal(TypeCode.Object, date.GetTypeCode()); Assert.Throws <InvalidCastException>(() => date.ToBoolean(null)); Assert.Throws <InvalidCastException>(() => date.ToByte(null)); Assert.Throws <InvalidCastException>(() => date.ToChar(null)); Assert.Equal(new DateTime(1987, 3, 4), date.ToDateTime(null)); Assert.Throws <InvalidCastException>(() => date.ToDecimal(null)); Assert.Throws <InvalidCastException>(() => date.ToDouble(null)); Assert.Throws <InvalidCastException>(() => date.ToInt16(null)); Assert.Throws <InvalidCastException>(() => date.ToInt32(null)); Assert.Throws <InvalidCastException>(() => date.ToInt64(null)); Assert.Throws <InvalidCastException>(() => date.ToSByte(null)); Assert.Throws <InvalidCastException>(() => date.ToSingle(null)); Assert.Equal("1987-03-04", date.ToString(null)); Assert.Throws <InvalidCastException>(() => date.ToUInt16(null)); Assert.Throws <InvalidCastException>(() => date.ToUInt32(null)); Assert.Throws <InvalidCastException>(() => date.ToUInt64(null)); Assert.Equal(new DateTime(1987, 3, 4), date.ToType(typeof(DateTime), null)); Assert.Equal(new DateTimeOffset(1987, 3, 4, 0, 0, 0, TimeSpan.Zero), date.ToType(typeof(DateTimeOffset), null)); Assert.Equal(new FudgeDateTime(1987, 3, 4, 0, 0, 0, 0, FudgeDateTimePrecision.Day), date.ToType(typeof(FudgeDateTime), null)); Assert.Equal("1987-03-04", date.ToType(typeof(string), null)); Assert.Throws <InvalidCastException>(() => date.ToType(typeof(int), null)); }
public void RoundTrip() { var d = new FudgeDate(1999, 12, 10); var msg1 = new FudgeMsg(context, new Field("d", d)); var bytes = msg1.ToByteArray(); var msg2 = context.Deserialize(bytes).Message; Assert.Equal("1999-12-10", msg2.GetValue <FudgeDate>("d").ToString()); }
public void RoundTrip() { var d = new FudgeDate(1999, 12, 10); var msg1 = new FudgeMsg(context, new Field("d", d)); var bytes = msg1.ToByteArray(); var msg2 = context.Deserialize(bytes).Message; Assert.Equal("1999-12-10", msg2.GetValue<FudgeDate>("d").ToString()); }
public void ToDateTime() { var dateTime = new FudgeDate(20030104).ToDateTime(); Assert.Equal("2003-01-04T00:00:00.0000000", dateTime.ToString("o")); Assert.Equal(DateTimeKind.Unspecified, dateTime.Kind); // Check rolls to next valid day var dateTime2 = new FudgeDate(20030230).ToDateTime(); Assert.Equal("2003-03-01T00:00:00.0000000", dateTime2.ToString("o")); }
/// <summary> /// Constructs a new <c>FudgeDateTime</c> based on a .net <see cref="DateTime"/>, specifying the precision. /// </summary> /// <param name="dateTime"></param> /// <param name="precision"></param> public FudgeDateTime(DateTime dateTime, FudgeDateTimePrecision precision) { this.date = new FudgeDate(dateTime); if (precision > FudgeDateTimePrecision.Day) { this.time = new FudgeTime(dateTime, precision); } else { this.time = FudgeTime.Midnight; } this.precision = precision; }
public void YearMonthDay() { var date = new FudgeDate(19701211); Assert.Equal(1970, date.Year); Assert.Equal(12, date.Month); Assert.Equal(11, date.Day); var date2 = new FudgeDate(-12340102); Assert.Equal(-1234, date2.Year); Assert.Equal(1, date2.Month); Assert.Equal(2, date2.Day); }
/// <summary> /// Constructs a new <c>FudgeDateTime</c> based on a .net <see cref="DateTimeOffset"/>, specifying the precision. /// </summary> /// <param name="dateTimeOffset"><see cref="DateTimeOffset"/> to use.</param> /// <param name="precision"></param> /// <remarks>The offset must be on a 15-minute boundary and within the +/- 30 hour range allowed by <c>FudgeDateTime</c>.</remarks> public FudgeDateTime(DateTimeOffset dateTimeOffset, FudgeDateTimePrecision precision) { this.date = new FudgeDate(dateTimeOffset.Date); if (precision > FudgeDateTimePrecision.Day) { int seconds = (int)(dateTimeOffset.TimeOfDay.Ticks / FudgeTime.TicksPerSecond); int nanos = (int)(dateTimeOffset.TimeOfDay.Ticks % FudgeTime.TicksPerSecond); int offset = (int)(dateTimeOffset.Offset.Ticks / FudgeTime.TicksPerSecond / 60); this.time = new FudgeTime(precision, seconds, nanos, offset); } else { // Time and offset are irrelevant this.time = FudgeTime.Midnight; } this.precision = precision; }
/// <summary> /// Constructs a new <c>FudgeDateTime</c> using a <see cref="FudgeDate"/> and <see cref="FudgeTime"/>, specifying the precision. /// </summary> /// <param name="date">Date part of the datetime.</param> /// <param name="time">Time part of the datetime, may be <c>null</c>.</param> /// <param name="precision">Precision to use.</param> public FudgeDateTime(FudgeDate date, FudgeTime time, FudgeDateTimePrecision precision) { this.date = date; if (time == null) { this.time = FudgeTime.Midnight; } else { this.time = time; } this.precision = precision; }
/// <summary> /// Constructs a new <c>FudgeDateTime</c> using a <see cref="FudgeDate"/> and <see cref="FudgeTime"/>, taking the /// precision from the <see cref="FudgeTime"/>. /// </summary> /// <param name="date">Date part of the datetime.</param> /// <param name="time">Time part of the datetime, may be <c>null</c>.</param> public FudgeDateTime(FudgeDate date, FudgeTime time) : this(date, time, time == null ? FudgeDateTimePrecision.Day : time.Precision) { }
/// <summary> /// Constructs a <c>FudgeDateTime</c> from component values with timezone information. /// </summary> /// <param name="year"></param> /// <param name="month"></param> /// <param name="day"></param> /// <param name="hour"></param> /// <param name="minute"></param> /// <param name="second"></param> /// <param name="nanosecond"></param> /// <param name="timeZoneOffset">Timezone offset from UTC, in minutes, must be multiple of 15</param> /// <param name="precision"></param> public FudgeDateTime(int year, int month, int day, int hour, int minute, int second, int nanosecond, int timeZoneOffset, FudgeDateTimePrecision precision) { this.date = new FudgeDate(year, month, day); if (precision > FudgeDateTimePrecision.Day) { this.time = new FudgeTime(hour, minute, second, nanosecond, timeZoneOffset, precision); } else { this.time = FudgeTime.Midnight; } this.precision = precision; }