This repository has been archived by the owner on Nov 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from mj1856/tzaware-methods
Add tz-aware add/subtract methods on DateTimeOffset
- Loading branch information
Showing
7 changed files
with
506 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.