Skip to content

Commit

Permalink
Merge pull request #9 from lpreiner/datetimeoffset
Browse files Browse the repository at this point in the history
Add support for DateTimeOffset
  • Loading branch information
supersonicclay authored Mar 19, 2021
2 parents 16a2af0 + 1fd5300 commit 1ec02b3
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 17 deletions.
83 changes: 66 additions & 17 deletions CSharpDate.Test/DateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,32 @@ public void CanConstruct()
Assert.Equal(635007168000000000, d.Ticks);
}

[Fact]
public void CanConstructFromDateTime()
{
Date d1 = new Date(new DateTime(2001, 2, 3, 4, 5, 6, 7).AddTicks(8));
Date d2 = new Date(2001, 2, 3);
Assert.Equal(d1, d2);
Assert.True(d1.Equals(d2));
}

[Fact]
public void ToDateRemovesAllTimePortion()
{
Date d1 = new DateTime(2001, 2, 3, 4, 5, 6, 7).AddTicks(8).ToDate();
Date d2 = new Date(2001, 2, 3);
Assert.Equal(d1, d2);
Assert.True(d1.Equals(d2));
}
[Fact]
public void CanConstructFromDateTime()
{
Date d1 = new Date(new DateTime(2001, 2, 3, 4, 5, 6, 7).AddTicks(8));
Date d2 = new Date(2001, 2, 3);
Assert.Equal(d1, d2);
Assert.True(d1.Equals(d2));
}

[Fact]
public void CanConstructFromDateTimeOffset()
{
Date d1 = new Date(new DateTimeOffset(2001, 2, 3, 4, 5, 6, 7, TimeSpan.Zero).AddTicks(8));
Date d2 = new Date(2001, 2, 3);
Assert.Equal(d1, d2);
Assert.True(d1.Equals(d2));
}

[Fact]
public void ToDateRemovesAllTimePortion()
{
Date d1 = new DateTime(2001, 2, 3, 4, 5, 6, 7).AddTicks(8).ToDate();
Date d2 = new Date(2001, 2, 3);
Assert.Equal(d1, d2);
Assert.True(d1.Equals(d2));
}

[Fact]
public void CanGetDateFromDateTime()
Expand All @@ -76,6 +85,17 @@ public void CanGetDateFromDateTime()
Assert.Equal(dt.Day, d.Day);
}

[Fact]
public void CanGetDateFromDateTimeOffset()
{
DateTimeOffset dto = new DateTimeOffset(2013, 4, 5, 6, 7, 8, 9, TimeSpan.Zero);
Date d = dto.ToDate();

Assert.Equal(dto.Year, d.Year);
Assert.Equal(dto.Month, d.Month);
Assert.Equal(dto.Day, d.Day);
}

[Fact]
public void CanAddYears()
{
Expand Down Expand Up @@ -220,6 +240,35 @@ public void CanExplicitCastToDateTime()
Assert.Equal(dt.Day, d.Day);
}

[Fact]
public void CanImplicitCastToDateTimeOffset()
{
Date d = new Date(2013, 4, 5);
DateTimeOffset dt = d;

Assert.Equal(d.Year, dt.Year);
Assert.Equal(d.Month, dt.Month);
Assert.Equal(d.Day, dt.Day);
Assert.Equal(d.DayOfWeek, dt.DayOfWeek);
Assert.Equal(d.DayOfYear, dt.DayOfYear);
Assert.Equal(0, dt.Hour);
Assert.Equal(0, dt.Minute);
Assert.Equal(0, dt.Second);
Assert.Equal(0, dt.Millisecond);
}

[Fact]
public void CanExplicitCastToDateTimeOffset()
{
DateTime dt = new DateTime(2000, 1, 2, 3, 4, 5);
DateTimeOffset dto = new DateTimeOffset(dt, TimeSpan.FromMinutes(1));
Date d = (Date)dto;

Assert.Equal(dto.Year, d.Year);
Assert.Equal(dto.Month, d.Month);
Assert.Equal(dto.Day, d.Day);
}

[Fact]
public void CanToShortString()
{
Expand Down
22 changes: 22 additions & 0 deletions CSharpDate/Date.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public Date(DateTime dateTime)
this._dt = dateTime.AddTicks(-dateTime.Ticks % TimeSpan.TicksPerDay);
}

public Date(DateTimeOffset dateTimeOffset)
:this(dateTimeOffset.DateTime)
{ }

private Date(SerializationInfo info, StreamingContext context)
{
this._dt = DateTime.FromFileTime(info.GetInt64("ticks"));
Expand Down Expand Up @@ -106,6 +110,16 @@ public static explicit operator Date(DateTime d)
return new Date(d);
}

public static implicit operator DateTimeOffset(Date d)
{
return d._dt;
}

public static explicit operator Date(DateTimeOffset d)
{
return new Date(d);
}

public int Day
{
get
Expand Down Expand Up @@ -342,4 +356,12 @@ public static Date ToDate(this DateTime dt)
return new Date(dt);
}
}

public static class DateTimeOffsetExtensions
{
public static Date ToDate(this DateTimeOffset dto)
{
return new Date(dto);
}
}
}

0 comments on commit 1ec02b3

Please sign in to comment.