/// <summary> /// Initializes a new instance of <see cref="ZonedDateTime"/> from individual date time component values /// </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="zone"></param> public ZonedDateTime(int year, int month, int day, int hour, int minute, int second, int nanosecond, Zone zone) { Throw.ArgumentOutOfRangeException.IfValueNotBetween(year, TemporalHelpers.MinYear, TemporalHelpers.MaxYear, nameof(year)); Throw.ArgumentOutOfRangeException.IfValueNotBetween(month, TemporalHelpers.MinMonth, TemporalHelpers.MaxMonth, nameof(month)); Throw.ArgumentOutOfRangeException.IfValueNotBetween(day, TemporalHelpers.MinDay, TemporalHelpers.MaxDayOfMonth(year, month), nameof(day)); Throw.ArgumentOutOfRangeException.IfValueNotBetween(hour, TemporalHelpers.MinHour, TemporalHelpers.MaxHour, nameof(hour)); Throw.ArgumentOutOfRangeException.IfValueNotBetween(minute, TemporalHelpers.MinMinute, TemporalHelpers.MaxMinute, nameof(minute)); Throw.ArgumentOutOfRangeException.IfValueNotBetween(second, TemporalHelpers.MinSecond, TemporalHelpers.MaxSecond, nameof(second)); Throw.ArgumentOutOfRangeException.IfValueNotBetween(nanosecond, TemporalHelpers.MinNanosecond, TemporalHelpers.MaxNanosecond, nameof(nanosecond)); Throw.ArgumentNullException.IfNull(zone, nameof(zone)); Year = year; Month = month; Day = day; Hour = hour; Minute = minute; Second = second; Nanosecond = nanosecond; Zone = zone; }
/// <summary> /// Converts this time value to a <see cref="TimeSpan"/> instance. /// </summary> /// <value>Equivalent <see cref="TimeSpan"/> value</value> /// <exception cref="ValueTruncationException">If a truncation occurs during conversion</exception> public TimeSpan ToTimeSpan() { TemporalHelpers.AssertNoTruncation(this, nameof(TimeSpan)); return(new TimeSpan(0, Hour, Minute, Second).Add( TimeSpan.FromTicks(TemporalHelpers.ExtractTicksFromNanosecond(Nanosecond)))); }
public object Read(IPackStreamReader reader, byte signature, long size) { PackStream.EnsureStructSize("LocalTime", StructSize, size); var nanosOfDay = reader.ReadLong(); return(TemporalHelpers.NanoOfDayToTime(nanosOfDay)); }
public void ShouldCreateDateTimeWithRawValues() { var dateTime = new DateTime(1947, 12, 17, 23, 49, 54).AddTicks(1927945); var cypherDateTime = new CypherDateTime(TemporalHelpers.SecondsSinceEpoch(dateTime.Ticks), TemporalHelpers.NanosOfSecond(dateTime.Ticks)); cypherDateTime.ToDateTime().Should().Be(dateTime); }
public object Read(IPackStreamReader reader, byte signature, long size) { PackStream.EnsureStructSize("Date", StructSize, size); var epochDays = reader.ReadLong(); return(TemporalHelpers.EpochDaysToDate(epochDays)); }
/// <summary> /// Converts this date value to a <see cref="DateTime"/> instance. /// </summary> /// <value>Equivalent <see cref="DateTime"/> value</value> /// <exception cref="ValueOverflowException">If the value cannot be represented with DateTime</exception> /// <exception cref="ValueTruncationException">If a truncation occurs during conversion</exception> /// <returns>A <see cref="DateTime"/> instance.</returns> public DateTime ToDateTime() { TemporalHelpers.AssertNoTruncation(this, nameof(System.DateTime)); TemporalHelpers.AssertNoOverflow(this, nameof(System.DateTime)); return(new DateTime(Year, Month, Day, Hour, Minute, Second).AddTicks( TemporalHelpers.ExtractTicksFromNanosecond(Nanosecond))); }
public void FormatTimer() { Assert.AreEqual("30s", TemporalHelpers.FormatTimer(30), "FormatTimer() 30 seconds"); Assert.AreEqual("1m : 30s", TemporalHelpers.FormatTimer(90), "FormatTimer() 90 seconds"); Assert.AreEqual("1h : 0m : 0s", TemporalHelpers.FormatTimer(3600), "FormatTimer() 3600 seconds"); Assert.AreEqual("1d : 1h : 0m : 0s", TemporalHelpers.FormatTimer((3600 * 24) + 3600), "FormatTimer() (3600 * 24) + 3600 seconds"); Assert.AreEqual("5d : 1h : 0m : 0s", TemporalHelpers.FormatTimer((3600 * 24 * 5) + 3600), "FormatTimer() (3600 * 24 * 5) + 3600 seconds"); Assert.AreEqual("20d : 5h : 10m : 15s", TemporalHelpers.FormatTimer((3600 * 24 * 20) + (3600 * 5) + (60 * 10) + 15), "FormatTimer() (3600 * 24 * 20) + (3600 * 5) + (60 * 10) + 15 seconds"); }
public object Deserialize(IPackStreamReader reader, byte signature, long size) { PackStream.EnsureStructSize("LocalDateTime", StructSize, size); var epochSeconds = reader.ReadLong(); var nanosOfSecond = reader.ReadInteger(); return(TemporalHelpers.EpochSecondsAndNanoToDateTime(epochSeconds, nanosOfSecond)); }
public object Deserialize(IPackStreamReader reader, byte signature, long size) { PackStream.EnsureStructSize("Time", StructSize, size); var nanosOfDay = reader.ReadLong(); var offsetSeconds = reader.ReadInteger(); return(new OffsetTime(TemporalHelpers.NanoOfDayToTime(nanosOfDay), offsetSeconds)); }
public void ShouldCreateDateWithRawValues() { var dateTime = new DateTime(1947, 12, 17, 23, 49, 54).AddTicks(1927945); var cypherDateTime = new CypherDateTimeWithZoneId(TemporalHelpers.SecondsSinceEpoch(dateTime.Ticks), TemporalHelpers.NanosOfSecond(dateTime.Ticks), "Europe/Rome"); cypherDateTime.DateTime.Should().Be(dateTime); cypherDateTime.Offset.Should().Be(TimeSpan.FromHours(1)); cypherDateTime.ZoneId.Should().Be("Europe/Rome"); }
/// <summary> /// Converts this instance to an equivalent <see cref="DateTimeOffset"/> value /// </summary> /// <returns>Equivalent <see cref="DateTimeOffset"/> value</returns> /// <exception cref="ValueOverflowException">If the value cannot be represented with DateTimeOffset</exception> /// <exception cref="ValueTruncationException">If a truncation occurs during conversion</exception> public DateTimeOffset ToDateTimeOffset() { // we first get DateTime instance to force Truncation / Overflow checks var dateTime = DateTime; var offset = Offset; TemporalHelpers.AssertNoTruncation(offset, nameof(DateTimeOffset)); TemporalHelpers.AssertNoOverflow(offset, nameof(DateTimeOffset)); return(new DateTimeOffset(dateTime, offset)); }
public void StopWatch() { StopWatch stopwatch = new StopWatch(); stopwatch.Start(); TemporalHelpers.PauseExecution(2000); stopwatch.Stop(); Assert.AreEqual(2, stopwatch.GetSeconds <int>(), "GetSeconds()"); stopwatch.StopDate = stopwatch.StartDate.AddSeconds(1); Assert.AreEqual("00:00:01", stopwatch.GetTimeSpan().ToString(@"hh\:mm\:ss"), "GetTimeSpan() 1 second"); stopwatch.StopDate = stopwatch.StartDate.AddDays(1); Assert.AreEqual("1.00:00:00", stopwatch.GetTimeSpan().ToString(@"d\.hh\:mm\:ss"), "GetTimeSpan() 1 day"); stopwatch.StopDate = stopwatch.StartDate.AddDays(3).AddHours(5).AddMinutes(7).AddSeconds(15); Assert.AreEqual("3.05:07:15", stopwatch.GetTimeSpan().ToString(@"d\.hh\:mm\:ss"), "GetTimeSpan() complex"); stopwatch.StopDate = stopwatch.StartDate.AddDays(3).AddHours(5).AddMinutes(7).AddSeconds(15).AddMilliseconds(999); Assert.AreEqual("3.05:07:15.999", stopwatch.GetTimeSpan().ToString(@"d\.hh\:mm\:ss\.fff"), "GetTimeSpan() complex 2"); }
public object Deserialize(IPackStreamReader reader, byte signature, long size) { PackStream.EnsureStructSize($"ZonedDateTime[{(char) signature}]", StructSize, size); var epochSecondsUtc = reader.ReadLong(); var nanosOfSecond = reader.ReadInteger(); switch (signature) { case StructTypeWithId: return(new ZonedDateTime( TemporalHelpers.EpochSecondsAndNanoToDateTime(epochSecondsUtc, nanosOfSecond), Zone.Of(reader.ReadString()))); case StructTypeWithOffset: return(new ZonedDateTime( TemporalHelpers.EpochSecondsAndNanoToDateTime(epochSecondsUtc, nanosOfSecond), Zone.Of(reader.ReadInteger()))); default: throw new ProtocolException( $"Unsupported struct signature {signature} passed to {nameof(ZonedDateTimeSerializer)}!"); } }
/// <summary> /// Converts the value of the current <see cref="Duration"/> object to its equivalent string representation. /// </summary> /// <returns>String representation of this Point.</returns> public override string ToString() { return(TemporalHelpers.ToIsoDurationString(Months, Days, Seconds, Nanos)); }
/// <summary> /// Initializes a new instance of <see cref="LocalDateTime"/> from given <see cref="System.DateTime"/> value. /// </summary> /// /// <remarks> /// The value of <see cref="DateTime.Kind"/> has no effect. Date and time component values will be used without any /// explicit conversions (i.e. we treat <see cref="DateTime.Kind"/> as if <see cref="DateTimeKind.Local"/>). /// </remarks> /// <param name="dateTime"></param> public LocalDateTime(DateTime dateTime) : this(dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, dateTime.Second, TemporalHelpers.ExtractNanosecondFromTicks(dateTime.Ticks)) { }
/// <summary> /// Converts the value of the current <see cref="LocalDateTime"/> object to its equivalent string representation. /// </summary> /// <returns>String representation of this Point.</returns> public override string ToString() { return ($"{TemporalHelpers.ToIsoDateString(Year, Month, Day)}T{TemporalHelpers.ToIsoTimeString(Hour, Minute, Second, Nanosecond)}"); }
/// <summary> /// Initializes a new instance of <see cref="LocalDate"/> from individual date component values /// </summary> /// <param name="year"></param> /// <param name="month"></param> /// <param name="day"></param> public LocalDate(int year, int month, int day) { Throw.ArgumentOutOfRangeException.IfValueNotBetween(year, TemporalHelpers.MinYear, TemporalHelpers.MaxYear, nameof(year)); Throw.ArgumentOutOfRangeException.IfValueNotBetween(month, TemporalHelpers.MinMonth, TemporalHelpers.MaxMonth, nameof(month)); Throw.ArgumentOutOfRangeException.IfValueNotBetween(day, TemporalHelpers.MinDay, TemporalHelpers.MaxDayOfMonth(year, month), nameof(day)); Year = year; Month = month; Day = day; }
/// <summary> /// Converts the value of the current <see cref="LocalDate"/> object to its equivalent string representation. /// </summary> /// <returns>String representation of this Point.</returns> public override string ToString() { return(TemporalHelpers.ToIsoDateString(Year, Month, Day)); }
/// <summary> /// Converts this date value to a <see cref="DateTime"/> instance. /// </summary> /// <value>Equivalent <see cref="DateTime"/> value</value> /// <exception cref="ValueOverflowException">If the value cannot be represented with DateTime</exception> /// <returns>A <see cref="DateTime"/> instance.</returns> public DateTime ToDateTime() { TemporalHelpers.AssertNoOverflow(this, nameof(System.DateTime)); return(new DateTime(Year, Month, Day)); }
internal override int OffsetSecondsAt(DateTime dateTime) { return((int)TemporalHelpers.GetTimeZoneInfo(Id).GetUtcOffset(dateTime).TotalSeconds); }
/// <summary> /// Converts the value of the current <see cref="ZoneOffset"/> object to its equivalent string representation. /// </summary> /// <returns>String representation of this Point.</returns> public override string ToString() { return(TemporalHelpers.ToIsoTimeZoneOffset(OffsetSeconds)); }
/// <summary> /// Initializes a new instance of <see cref="ZonedDateTime"/> from given <see cref="DateTime"/> value. /// </summary> /// <param name="dateTime"></param> /// <param name="zoneId"></param> public ZonedDateTime(DateTime dateTime, string zoneId) : this(dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, dateTime.Second, TemporalHelpers.ExtractNanosecondFromTicks(dateTime.Ticks), Zone.Of(zoneId)) { }
/// <summary> /// Initializes a new instance of <see cref="ZonedDateTime"/> from given <see cref="DateTime"/> value. /// </summary> /// <param name="dateTime"></param> /// <param name="offsetSeconds"></param> public ZonedDateTime(DateTime dateTime, int offsetSeconds) : this(dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, dateTime.Second, TemporalHelpers.ExtractNanosecondFromTicks(dateTime.Ticks), Zone.Of(offsetSeconds)) { }
/// <summary> /// Initializes a new instance of <see cref="CypherTimeWithOffset"/> from individual time components /// </summary> /// <param name="hour"></param> /// <param name="minute"></param> /// <param name="second"></param> /// <param name="nanoOfSecond"></param> /// <param name="offsetSeconds"></param> public CypherTimeWithOffset(int hour, int minute, int second, int nanoOfSecond, int offsetSeconds) : this(TemporalHelpers.NanosOf(hour, minute, second, nanoOfSecond), offsetSeconds) { }
/// <summary> /// Gets a <see cref="TimeSpan"/> copy of this time value. /// </summary> /// <returns>Equivalent <see cref="TimeSpan"/> value</returns> /// <exception cref="TruncationException">If a truncation occurs during conversion</exception> public TimeSpan ToTimeSpan() { return(TemporalHelpers.TimeOf(NanosecondsOfDay, true)); }
/// <summary> /// Converts the value of the current <see cref="OffsetTime"/> object to its equivalent string representation. /// </summary> /// <returns>String representation of this Point.</returns> public override string ToString() { return(TemporalHelpers.ToIsoTimeString(Hour, Minute, Second, Nanosecond) + TemporalHelpers.ToIsoTimeZoneOffset(OffsetSeconds)); }
/// <summary> /// Converts the value of the current <see cref="ZoneId"/> object to its equivalent string representation. /// </summary> /// <returns>String representation of this Point.</returns> public override string ToString() { return(TemporalHelpers.ToIsoTimeZoneId(Id)); }
/// <summary> /// Initializes a new instance of <see cref="OffsetTime"/> from given <see cref="TimeSpan"/> value /// </summary> /// <param name="time"></param> /// <param name="offsetSeconds"></param> private OffsetTime(TimeSpan time, int offsetSeconds) : this(time.Hours, time.Minutes, time.Seconds, TemporalHelpers.ExtractNanosecondFromTicks(time.Ticks), offsetSeconds) { }
/// <summary> /// Initializes a new instance of <see cref="LocalTime"/> from given <see cref="TimeSpan"/> value /// </summary> /// <param name="time"></param> public LocalTime(TimeSpan time) : this(time.Hours, time.Minutes, time.Seconds, TemporalHelpers.ExtractNanosecondFromTicks(time.Ticks)) { }
/// <summary> /// Gets a <see cref="DateTime"/> copy of this date value. /// </summary> /// <returns>Equivalent <see cref="DateTime"/> value</returns> public DateTime ToDateTime() { return(TemporalHelpers.DateOf(EpochDays)); }