Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# EditorConfig is awesome: http://EditorConfig.org

# top-most EditorConfig file
root = true

# Global settings
[*]
end_of_line = crlf
insert_final_newline = true
trim_trailing_whitespace = true

# Xml project files
[*.{csproj,vbproj,vcxproj,vcxproj.filters,proj,projitems,shproj}]
indent_style = space
indent_size = 2

# Xml config files
[*.{props,targets,ruleset,config,nuspec,resx,vsixmanifest,vsct}]
indent_style = space
tab_width = 2

[*.{md,json}]
indent_style = space
tab_width = 4

[*.cs]
indent_style = space
tab_width = 4
1 change: 1 addition & 0 deletions FluentAssertions.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ VisualStudioVersion = 15.0.26730.3
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{E5A0B454-22D4-4694-99FF-D6A8B7DE7DA3}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
Tests\Default.testsettings = Tests\Default.testsettings
Src\FluentAssertions.nuspec = Src\FluentAssertions.nuspec
Src\JetBrainsAnnotations.cs = Src\JetBrainsAnnotations.cs
Expand Down
96 changes: 91 additions & 5 deletions Src/FluentAssertions/FluentDateTimeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,18 +141,62 @@ public static DateTime At(this DateTime date, TimeSpan time)
/// Returns a new <see cref="DateTime"/> value for the specified <paramref name="date"/> and time with the specified
/// <paramref name="hours"/>, <paramref name="minutes"/> and optionally <paramref name="seconds"/>.
/// </summary>
public static DateTime At(this DateTime date, int hours, int minutes, int seconds = 0, int milliseconds = 0)
public static DateTime At(this DateTime date, int hours, int minutes, int seconds = 0, int milliseconds = 0, int microseconds = 0, int nanoseconds = 0)
{
return new DateTime(date.Year, date.Month, date.Day, hours, minutes, seconds, milliseconds);
if (microseconds < 0 || microseconds > 999)
{
throw new ArgumentOutOfRangeException(nameof(microseconds), "Valid values are between 0 and 999");
}

if (nanoseconds < 0 || nanoseconds > 999)
{
throw new ArgumentOutOfRangeException(nameof(nanoseconds), "Valid values are between 0 and 999");
}

var value = new DateTime(date.Year, date.Month, date.Day, hours, minutes, seconds, milliseconds);

if (microseconds != 0)
{
value += microseconds.Microseconds();
}

if (nanoseconds != 0)
{
value += nanoseconds.Nanoseconds();
}

return value;
}

/// <summary>
/// Returns a new <see cref="DateTimeOffset"/> value for the specified <paramref name="date"/> and time with the specified
/// <paramref name="hours"/>, <paramref name="minutes"/> and optionally <paramref name="seconds"/>.
/// </summary>
public static DateTimeOffset At(this DateTimeOffset date, int hours, int minutes, int seconds = 0, int milliseconds = 0)
public static DateTimeOffset At(this DateTimeOffset date, int hours, int minutes, int seconds = 0, int milliseconds = 0, int microseconds = 0, int nanoseconds = 0)
{
return new DateTimeOffset(date.Year, date.Month, date.Day, hours, minutes, seconds, milliseconds, date.Offset);
if (microseconds < 0 || microseconds > 999)
{
throw new ArgumentOutOfRangeException(nameof(microseconds), "Valid values are between 0 and 999");
}

if (nanoseconds < 0 || nanoseconds > 999)
{
throw new ArgumentOutOfRangeException(nameof(nanoseconds), "Valid values are between 0 and 999");
}

var value = new DateTimeOffset(date.Year, date.Month, date.Day, hours, minutes, seconds, milliseconds, date.Offset);

if (microseconds != 0)
{
value += microseconds.Microseconds();
}

if (nanoseconds != 0)
{
value += nanoseconds.Nanoseconds();
}

return value;
}

/// <summary>
Expand Down Expand Up @@ -190,5 +234,47 @@ public static DateTime After(this TimeSpan timeDifference, DateTime sourceDateTi
{
return sourceDateTime.Add(timeDifference);
}

/// <summary>
/// Gets the nanoseconds component of the date represented by the current <see cref="DateTime" /> structure.
/// </summary>
public static int Nanosecond(this DateTime self)
{
return self.Ticks.Ticks().Nanosecond();
}

/// <summary>
/// Returns a new <see cref="DateTime" /> that adds the specified number of nanoseconds to the value of this instance.
/// </summary>
public static DateTime AddNanoseconds(this DateTime self, long nanoseconds)
{
if (nanoseconds == 0)
{
return self;
}

return self + nanoseconds.Nanoseconds();
}

/// <summary>
/// Gets the microseconds component of the date represented by the current <see cref="DateTime" /> structure.
/// </summary>
public static int Microsecond(this DateTime self)
{
return self.Ticks.Ticks().Microsecond();
}

/// <summary>
/// Returns a new <see cref="DateTime" /> that adds the specified number of microseconds to the value of this instance.
/// </summary>
public static DateTime AddMicroseconds(this DateTime self, long microseconds)
{
if (microseconds == 0)
{
return self;
}

return self + microseconds.Microseconds();
}
}
}
}
30 changes: 27 additions & 3 deletions Src/FluentAssertions/Formatting/DateTimeOffsetValueFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,22 @@ public string Format(object value, FormattingContext context, FormatChild format

if (HasTime(dateTime))
{
string format = HasMilliSeconds(dateTime) ? "HH:mm:ss.fff" : "HH:mm:ss";
fragments.Add(dateTime.ToString(format));
if (HasNanoSeconds(dateTime))
{
fragments.Add(dateTime.ToString("HH:mm:ss.fffffff"));
}
else if (HasMicroSeconds(dateTime))
{
fragments.Add(dateTime.ToString("HH:mm:ss.ffffff"));
}
else if(HasMilliSeconds(dateTime))
{
fragments.Add(dateTime.ToString("HH:mm:ss.fff"));
}
else
{
fragments.Add(dateTime.ToString("HH:mm:ss"));
}
}

if (dateTime.Offset > TimeSpan.Zero)
Expand Down Expand Up @@ -86,5 +100,15 @@ private static bool HasMilliSeconds(DateTimeOffset dateTime)
{
return (dateTime.Millisecond > 0);
}

private static bool HasMicroSeconds(DateTimeOffset dateTime)
{
return (dateTime.Ticks % TimeSpan.FromMilliseconds(1).Ticks) > 0;
}

private static bool HasNanoSeconds(DateTimeOffset dateTime)
{
return (dateTime.Ticks % (TimeSpan.FromMilliseconds(1).Ticks / 1000)) > 0;
}
}
}
}
9 changes: 5 additions & 4 deletions Src/FluentAssertions/Formatting/TimeSpanValueFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,11 @@ private static IEnumerable<string> GetNonZeroFragments(TimeSpan timeSpan)

private static void AddMicrosecondsIfNotZero(TimeSpan timeSpan, List<string> fragments)
{
if (timeSpan.Ticks > 0 && timeSpan.Ticks < TimeSpan.TicksPerMillisecond)
var ticks = timeSpan.Ticks % TimeSpan.TicksPerMillisecond;
if (ticks > 0)
{
var microSeconds = timeSpan.Ticks / (double)TimeSpan.TicksPerMillisecond * 1000;
fragments.Add(microSeconds.ToString("0.0") + "us");
var microSeconds = ticks / (double)TimeSpan.TicksPerMillisecond * 1000;
fragments.Add(microSeconds.ToString("0.0") + "µs");
}
}

Expand Down Expand Up @@ -125,4 +126,4 @@ private static string[] AllButLastFragment(IEnumerable<string> fragments)
return fragments.Take(fragments.Count() - 1).ToArray();
}
}
}
}
87 changes: 85 additions & 2 deletions Src/FluentAssertions/TimeSpanConversionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using FluentAssertions.Common;

namespace FluentAssertions
{
Expand All @@ -22,6 +21,16 @@ namespace FluentAssertions
/// <seealso cref="FluentDateTimeExtensions"/>
public static class TimeSpanConversionExtensions
{
/// <summary>
/// Represents the number of ticks that are in 1 microsecond.
/// </summary>
public const Int64 TicksPerMicrosecond = TimeSpan.TicksPerMillisecond / 1000;

/// <summary>
/// Represents the number of ticks that are in 1 nanosecond.
/// </summary>
public const Double TicksPerNanosecond = TicksPerMicrosecond / 1000d;

/// <summary>
/// Returns a <see cref="TimeSpan" /> based on a number of ticks.
/// </summary>
Expand All @@ -38,6 +47,80 @@ public static TimeSpan Ticks(this long ticks)
return TimeSpan.FromTicks(ticks);
}

/// <summary>
/// Gets the nanoseconds component of the time interval represented by the current <see cref="TimeSpan" /> structure.
/// </summary>
public static int Nanosecond(this TimeSpan self)
{
return (int)((self.Ticks % TicksPerMicrosecond) * (1d / TicksPerNanosecond));
}

/// <summary>
/// Returns a <see cref="TimeSpan" /> based on a number of nanoseconds.
/// </summary>
/// <remarks>
/// .NET's smallest resolutions is 100 nanoseconds. Any nanoseconds passed in
/// lower than .NET's resolution will be rounded using the default rounding
/// algorithm in Math.Round().
/// </remarks>
public static TimeSpan Nanoseconds(this int nanoseconds)
{
return ((long)Math.Round(nanoseconds * TicksPerNanosecond)).Ticks();
}

/// <summary>
/// Returns a <see cref="TimeSpan" /> based on a number of nanoseconds.
/// </summary>
/// <remarks>
/// .NET's smallest resolutions is 100 nanoseconds. Any nanoseconds passed in
/// lower than .NET's resolution will be rounded using the default rounding
/// algorithm in Math.Round().
/// </remarks>
public static TimeSpan Nanoseconds(this long nanoseconds)
{
return ((long)Math.Round(nanoseconds * TicksPerNanosecond)).Ticks();
}

/// <summary>
/// Gets the value of the current <see cref="TimeSpan" /> structure expressed in whole and fractional nanoseconds.
/// </summary>
public static double TotalNanoseconds(this TimeSpan self)
{
return self.Ticks * (1d / TicksPerNanosecond);
}

/// <summary>
/// Gets the microseconds component of the time interval represented by the current <see cref="TimeSpan" /> structure.
/// </summary>
public static int Microsecond(this TimeSpan self)
{
return (int)((self.Ticks % TimeSpan.TicksPerMillisecond) * (1d / TicksPerMicrosecond));
}

/// <summary>
/// Returns a <see cref="TimeSpan" /> based on a number of microseconds.
/// </summary>
public static TimeSpan Microseconds(this int microseconds)
{
return (microseconds * TicksPerMicrosecond).Ticks();
}

/// <summary>
/// Returns a <see cref="TimeSpan" /> based on a number of microseconds.
/// </summary>
public static TimeSpan Microseconds(this long microseconds)
{
return (microseconds * TicksPerMicrosecond).Ticks();
}

/// <summary>
/// Gets the value of the current <see cref="TimeSpan" /> structure expressed in whole and fractional microseconds.
/// </summary>
public static double TotalMicroseconds(this TimeSpan self)
{
return self.Ticks * (1d / TicksPerMicrosecond);
}

/// <summary>
/// Returns a <see cref="TimeSpan" /> based on a number of milliseconds.
/// </summary>
Expand Down Expand Up @@ -165,4 +248,4 @@ public static TimeSpan And(this TimeSpan sourceTime, TimeSpan offset)
return sourceTime.Add(offset);
}
}
}
}
4 changes: 2 additions & 2 deletions Tests/Shared.Specs/DateTimeAssertionSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,7 @@ public void When_asserting_subject_datetime_is_not_close_to_the_maximum_datetime
// Assert
//-----------------------------------------------------------------------------------------------------------
act.Should().Throw<XunitException>()
.WithMessage("Expected date and time to not be within 0.100s from <9999-12-31 23:59:59.999>, but found <9999-12-31 23:59:59.949>.");
.WithMessage("Expected date and time to not be within 0.100s from <9999-12-31 23:59:59.9999999>, but found <9999-12-31 23:59:59.9499999>.");
}

#endregion
Expand Down Expand Up @@ -2604,4 +2604,4 @@ public void When_a_value_is_one_of_the_specified_values_it_should_succeed_when_d

#endregion
}
}
}
4 changes: 2 additions & 2 deletions Tests/Shared.Specs/DateTimeOffsetAssertionSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,7 @@ public void When_asserting_subject_datetimeoffset_is_not_close_to_the_maximum_da
// Assert
//-----------------------------------------------------------------------------------------------------------
act.Should().Throw<XunitException>()
.WithMessage("Expected date and time to not be within 0.100s from <9999-12-31 23:59:59.999>, but found <9999-12-31 23:59:59.949>.");
.WithMessage("Expected date and time to not be within 0.100s from <9999-12-31 23:59:59.9999999>, but found <9999-12-31 23:59:59.9499999>.");
}
#endregion

Expand Down Expand Up @@ -2664,4 +2664,4 @@ public void When_a_value_is_one_of_the_specified_values_it_should_succeed_when_d

#endregion
}
}
}
Loading