Skip to content
This repository has been archived by the owner on Nov 3, 2018. It is now read-only.

Commit

Permalink
Merge pull request #35 from mj1856/tzaware-methods
Browse files Browse the repository at this point in the history
Add tz-aware add/subtract methods on DateTimeOffset
  • Loading branch information
mattjohnsonpint committed Jul 2, 2015
2 parents 21f6b4f + 5408413 commit 4828c56
Show file tree
Hide file tree
Showing 7 changed files with 506 additions and 0 deletions.
139 changes: 139 additions & 0 deletions System.DateAndTime.Tests/DateTimeOffsetTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
using Xunit;

namespace System.DateAndTime.Tests
{
public class DateTimeOffsetTests
{
[Fact]
public void CanAddYearsAcrossDstTransition()
{
var tz = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
var dto = new DateTimeOffset(2014, 3, 9, 0, 0, 0, TimeSpan.FromHours(-8));
var result = dto.AddYears(1, tz);

var expected = new DateTimeOffset(2015, 3, 9, 0, 0, 0, TimeSpan.FromHours(-7));
Assert.Equal(expected, result);
Assert.Equal(expected.Offset, result.Offset);
}

[Fact]
public void CanAddMonthsAcrossDstTransition()
{
var tz = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
var dto = new DateTimeOffset(2015, 2, 9, 0, 0, 0, TimeSpan.FromHours(-8));
var result = dto.AddMonths(1, tz);

var expected = new DateTimeOffset(2015, 3, 9, 0, 0, 0, TimeSpan.FromHours(-7));
Assert.Equal(expected, result);
Assert.Equal(expected.Offset, result.Offset);
}

[Fact]
public void CanAddDaysAcrossDstTransition()
{
var tz = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
var dto = new DateTimeOffset(2015, 3, 8, 0, 0, 0, TimeSpan.FromHours(-8));
var result = dto.AddDays(1, tz);

var expected = new DateTimeOffset(2015, 3, 9, 0, 0, 0, TimeSpan.FromHours(-7));
Assert.Equal(expected, result);
Assert.Equal(expected.Offset, result.Offset);
}

[Fact]
public void CanAddDaysAcrossDstTransition_LandInGap()
{
var tz = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
var dto = new DateTimeOffset(2015, 3, 7, 2, 30, 0, TimeSpan.FromHours(-8));
var result = dto.AddDays(1, tz);

var expected = new DateTimeOffset(2015, 3, 8, 3, 30, 0, TimeSpan.FromHours(-7));
Assert.Equal(expected, result);
Assert.Equal(expected.Offset, result.Offset);
}

[Fact]
public void CanAddDaysAcrossDstTransition_LandInOverlap()
{
var tz = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
var dto = new DateTimeOffset(2015, 10, 31, 1, 30, 0, TimeSpan.FromHours(-7));
var result = dto.AddDays(1, tz);

var expected = new DateTimeOffset(2015, 11, 1, 1, 30, 0, TimeSpan.FromHours(-7));
Assert.Equal(expected, result);
Assert.Equal(expected.Offset, result.Offset);
}

[Fact]
public void CanAddDaysAcrossDstTransition_StartWithMismatchedOffset()
{
var tz = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
var dto = new DateTimeOffset(2015, 3, 8, 4, 0, 0, TimeSpan.FromHours(-4));
var result = dto.AddDays(1, tz);

var expected = new DateTimeOffset(2015, 3, 9, 0, 0, 0, TimeSpan.FromHours(-7));
Assert.Equal(expected, result);
Assert.Equal(expected.Offset, result.Offset);
}

[Fact]
public void CanAddHoursAcrossDstTransition()
{
var tz = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
var dto = new DateTimeOffset(2015, 3, 8, 1, 0, 0, TimeSpan.FromHours(-8));
var result = dto.AddHours(1, tz);

var expected = new DateTimeOffset(2015, 3, 8, 3, 0, 0, TimeSpan.FromHours(-7));
Assert.Equal(expected, result);
Assert.Equal(expected.Offset, result.Offset);
}

[Fact]
public void CanAddMinutesAcrossDstTransition()
{
var tz = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
var dto = new DateTimeOffset(2015, 3, 8, 1, 59, 0, TimeSpan.FromHours(-8));
var result = dto.AddMinutes(1, tz);

var expected = new DateTimeOffset(2015, 3, 8, 3, 0, 0, TimeSpan.FromHours(-7));
Assert.Equal(expected, result);
Assert.Equal(expected.Offset, result.Offset);
}

[Fact]
public void CanAddSecondsAcrossDstTransition()
{
var tz = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
var dto = new DateTimeOffset(2015, 3, 8, 1, 59, 59, TimeSpan.FromHours(-8));
var result = dto.AddSeconds(1, tz);

var expected = new DateTimeOffset(2015, 3, 8, 3, 0, 0, TimeSpan.FromHours(-7));
Assert.Equal(expected, result);
Assert.Equal(expected.Offset, result.Offset);
}

[Fact]
public void CanAddMillisecondsAcrossDstTransition()
{
var tz = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
var dto = new DateTimeOffset(2015, 3, 8, 1, 59, 59, 999, TimeSpan.FromHours(-8));
var result = dto.AddMilliseconds(1, tz);

var expected = new DateTimeOffset(2015, 3, 8, 3, 0, 0, TimeSpan.FromHours(-7));
Assert.Equal(expected, result);
Assert.Equal(expected.Offset, result.Offset);
}

[Fact]
public void CanAddTicksAcrossDstTransition()
{
var tz = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
var dto = new DateTimeOffset(2015, 3, 8, 1, 59, 59, 999, TimeSpan.FromHours(-8)).AddTicks(9999);
var result = dto.AddTicks(1, tz);

var expected = new DateTimeOffset(2015, 3, 8, 3, 0, 0, TimeSpan.FromHours(-7));
Assert.Equal(expected, result);
Assert.Equal(expected.Offset, result.Offset);
}
}
}
145 changes: 145 additions & 0 deletions System.DateAndTime.Tests/DateTimeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,150 @@ public void CanAssignDateTimeTimeOfDayToTimeOfDay()
{
TimeOfDay time = DateTime.Now.TimeOfDay;
}

[Fact]
public void CanAddYearsAcrossDstTransition()
{
var tz = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
var dt = new DateTime(2014, 3, 9, 0, 0, 0);
var result = dt.AddYears(1, tz);

var expected = new DateTimeOffset(2015, 3, 9, 0, 0, 0, TimeSpan.FromHours(-7));
Assert.Equal(expected, result);
Assert.Equal(expected.Offset, result.Offset);
}

[Fact]
public void CanAddMonthsAcrossDstTransition()
{
var tz = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
var dt = new DateTime(2015, 2, 9, 0, 0, 0);
var result = dt.AddMonths(1, tz);

var expected = new DateTimeOffset(2015, 3, 9, 0, 0, 0, TimeSpan.FromHours(-7));
Assert.Equal(expected, result);
Assert.Equal(expected.Offset, result.Offset);
}

[Fact]
public void CanAddDaysAcrossDstTransition()
{
var tz = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
var dt = new DateTime(2015, 3, 8, 0, 0, 0);
var result = dt.AddDays(1, tz);

var expected = new DateTimeOffset(2015, 3, 9, 0, 0, 0, TimeSpan.FromHours(-7));
Assert.Equal(expected, result);
Assert.Equal(expected.Offset, result.Offset);
}

[Fact]
public void CanAddDaysAcrossDstTransition_LandInGap()
{
var tz = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
var dt = new DateTime(2015, 3, 7, 2, 30, 0);
var result = dt.AddDays(1, tz);

var expected = new DateTimeOffset(2015, 3, 8, 3, 30, 0, TimeSpan.FromHours(-7));
Assert.Equal(expected, result);
Assert.Equal(expected.Offset, result.Offset);
}

[Fact]
public void CanAddDaysAcrossDstTransition_LandInOverlap()
{
var tz = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
var dt = new DateTime(2015, 10, 31, 1, 30, 0);
var result = dt.AddDays(1, tz);

var expected = new DateTimeOffset(2015, 11, 1, 1, 30, 0, TimeSpan.FromHours(-7));
Assert.Equal(expected, result);
Assert.Equal(expected.Offset, result.Offset);
}

[Fact]
public void CanAddDaysAcrossDstTransition_StartWithMismatchedKind()
{
var tz = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
var dt = new DateTime(2015, 3, 8, 8, 0, 0, DateTimeKind.Utc);
var result = dt.AddDays(1, tz);

var expected = new DateTimeOffset(2015, 3, 9, 0, 0, 0, TimeSpan.FromHours(-7));
Assert.Equal(expected, result);
Assert.Equal(expected.Offset, result.Offset);
}


[Fact]
public void CanAddHoursAcrossDstTransition()
{
var tz = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
var dt = new DateTime(2015, 3, 8, 1, 0, 0);
var result = dt.AddHours(1, tz);

var expected = new DateTimeOffset(2015, 3, 8, 3, 0, 0, TimeSpan.FromHours(-7));
Assert.Equal(expected, result);
Assert.Equal(expected.Offset, result.Offset);
}

[Fact]
public void CanAddHoursAcrossDstTransition_StartWithMismatchedKind()
{
var tz = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
var dt = new DateTime(2015, 3, 8, 9, 0, 0, DateTimeKind.Utc);
var result = dt.AddHours(1, tz);

var expected = new DateTimeOffset(2015, 3, 8, 3, 0, 0, TimeSpan.FromHours(-7));
Assert.Equal(expected, result);
Assert.Equal(expected.Offset, result.Offset);
}

[Fact]
public void CanAddMinutesAcrossDstTransition()
{
var tz = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
var dt = new DateTime(2015, 3, 8, 1, 59, 0);
var result = dt.AddMinutes(1, tz);

var expected = new DateTimeOffset(2015, 3, 8, 3, 0, 0, TimeSpan.FromHours(-7));
Assert.Equal(expected, result);
Assert.Equal(expected.Offset, result.Offset);
}

[Fact]
public void CanAddSecondsAcrossDstTransition()
{
var tz = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
var dt = new DateTime(2015, 3, 8, 1, 59, 59);
var result = dt.AddSeconds(1, tz);

var expected = new DateTimeOffset(2015, 3, 8, 3, 0, 0, TimeSpan.FromHours(-7));
Assert.Equal(expected, result);
Assert.Equal(expected.Offset, result.Offset);
}

[Fact]
public void CanAddMillisecondsAcrossDstTransition()
{
var tz = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
var dt = new DateTime(2015, 3, 8, 1, 59, 59, 999);
var result = dt.AddMilliseconds(1, tz);

var expected = new DateTimeOffset(2015, 3, 8, 3, 0, 0, TimeSpan.FromHours(-7));
Assert.Equal(expected, result);
Assert.Equal(expected.Offset, result.Offset);
}

[Fact]
public void CanAddTicksAcrossDstTransition()
{
var tz = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
var dt = new DateTime(2015, 3, 8, 1, 59, 59, 999).AddTicks(9999);
var result = dt.AddTicks(1, tz);

var expected = new DateTimeOffset(2015, 3, 8, 3, 0, 0, TimeSpan.FromHours(-7));
Assert.Equal(expected, result);
Assert.Equal(expected.Offset, result.Offset);
}
}
}
1 change: 1 addition & 0 deletions System.DateAndTime.Tests/System.DateAndTime.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
<Compile Include="DateFormattingTests.cs" />
<Compile Include="DateMathTests.cs" />
<Compile Include="DateMiscTests.cs" />
<Compile Include="DateTimeOffsetTests.cs" />
<Compile Include="TimeOfDayComparisonTests.cs" />
<Compile Include="TimeOfDayConstructionTests.cs" />
<Compile Include="TimeOfDayMathTests.cs" />
Expand Down
Loading

0 comments on commit 4828c56

Please sign in to comment.