Skip to content

Commit

Permalink
Merge 0c16236 into c8a3dd9
Browse files Browse the repository at this point in the history
  • Loading branch information
Bolorunduro Winner-Timothy B committed Apr 21, 2019
2 parents c8a3dd9 + 0c16236 commit d23046c
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 1 deletion.
51 changes: 51 additions & 0 deletions src/Models/CalendarTimeFormats.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
namespace moment.net.Models
{
public class CalendarTimeFormats
{
public string SameDay { get; }

public string NextDay { get; }

public string NextWeek { get; }

public string LastDay { get; }

public string LastWeek { get; }

public string EverythingElse { get; }

/// <summary>
/// Default constructor, sets the time formats to the default
/// </summary>
public CalendarTimeFormats()
{
const string baseSuffix = " 'at' hh:mm tt";
SameDay = "'Today'" + baseSuffix;
NextDay = "'Tomorrow'" + baseSuffix;
NextWeek = "dddd" + baseSuffix;
LastDay = "'Yesterday'" + baseSuffix;
LastWeek = "'Last' dddd" + baseSuffix;
EverythingElse = "MM/dd/yyyy";
}

/// <summary>
/// Overload constructor, allows for setting the format expected for each calendar time group
/// </summary>
/// <param name="sameDay">Format for dates that fall on the same day</param>
/// <param name="nextDay">Format for dates that fall on the next day</param>
/// <param name="nextWeek">Format for dates that fall in the next week</param>
/// <param name="lastDay">Format for dates that fall on the day before</param>
/// <param name="lastWeek">Format for dates that fall in the preceding week</param>
/// <param name="everythingElse">Format for dates that do not fall into the predefined categories</param>
public CalendarTimeFormats(string sameDay, string nextDay, string nextWeek, string lastDay, string lastWeek,
string everythingElse)
{
SameDay = sameDay;
NextDay = nextDay;
NextWeek = nextWeek;
LastDay = lastDay;
LastWeek = lastWeek;
EverythingElse = everythingElse;
}
}
}
40 changes: 39 additions & 1 deletion src/RelativeTime.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using moment.net.Models;

namespace moment.net
{
Expand All @@ -18,7 +19,7 @@ public static string FromNow(this DateTime This)
? ParseFromPastTimeSpan(DateTime.UtcNow - This)
: ParseFromPastTimeSpan(DateTime.Now - This);
}

/// <summary>
/// Get the relative time from a given date time to another date time instance
/// </summary>
Expand Down Expand Up @@ -57,6 +58,43 @@ public static string To(this DateTime This, DateTime dateTime)
return ParseFromFutureTimeSpan(endDate - startDate);
}

public static string CalendarTime(this DateTime This, CalendarTimeFormats formats = null)
{
return CalendarTime(This, DateTime.UtcNow, formats);
}

public static string CalendarTime(this DateTime This, DateTime dateTime,
CalendarTimeFormats formats = null)
{
formats = formats ?? new CalendarTimeFormats();
var startDate = This.Kind == DateTimeKind.Local ? This : This.ToLocalTime();
var endDate = dateTime.Kind == DateTimeKind.Local ? dateTime : dateTime.ToLocalTime();
var timeDiff = endDate - startDate;

if (startDate.Date == endDate.Date)
{
return endDate.ToString(formats.SameDay);
}

if (startDate.AddDays(1).Date == endDate.Date)
{
return endDate.ToString(formats.NextDay);
}
if (startDate.AddDays(-1).Date == endDate.Date)
{
return endDate.ToString(formats.LastDay);
}
if (timeDiff.TotalDays > 1 && timeDiff.TotalDays < 7)
{
return endDate.ToString(formats.NextWeek);
}
if (timeDiff.TotalDays >= -6 && timeDiff.TotalDays < -1)
{
return endDate.ToString(formats.LastWeek);
}
return endDate.ToString(formats.EverythingElse);
}

private static string ParseFromPastTimeSpan(TimeSpan timeSpan)
{
return $"{ParseTimeDifference(timeSpan)} ago";
Expand Down
46 changes: 46 additions & 0 deletions tests/RelativeTime.Tests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using moment.net.Models;
using NUnit.Framework;
using Shouldly;

Expand Down Expand Up @@ -205,5 +206,50 @@ public void ToSpecifiedDateTest()

twoThousandAndTwelve.To(twoThousandAndEighteen).ShouldBe("in 6 years");
}

[Test]
public void CalendarTimeSameDay()
{
var today = DateTime.Now.Date.AddHours(2);
today.CalendarTime().ShouldStartWith("Today at ");
}

[Test]
public void CalendarTimeFromYesterday()
{
var yesterday = DateTime.Now.AddDays(-1);
yesterday.CalendarTime().ShouldStartWith("Tomorrow at ");
}

[Test]
public void CalendarTimeFromTomorrow()
{
var tomorrow = DateTime.Now.AddDays(1);
tomorrow.CalendarTime().ShouldStartWith("Yesterday at ");
}

[Test]
public void CalendarTimeFromTwoFixedDates()
{
var initialDate = new DateTime(2012,12,12);
var nextDate = new DateTime(2012,12,18);
initialDate.CalendarTime(nextDate).ShouldStartWith(nextDate.ToString("dddd 'at' "));
}

[Test]
public void CalendarTimeToTwoFixedDates()
{
var earlierDate = new DateTime(2012,12,12);
var laterDate = new DateTime(2012,12,18);
laterDate.CalendarTime(earlierDate).ShouldStartWith(earlierDate.ToString("'Last' dddd 'at' "));
}

[Test]
public void CalendarTimeForEcessiveTimeSpanWithSpecifiedFormat()
{
var initialDate = new DateTime(2012,12,12);
var nextDate = new DateTime(2018,12,12);
initialDate.CalendarTime(nextDate, new CalendarTimeFormats("", "", "", "", "", "dd/MM/yyyy")).ShouldBe(nextDate.ToString("dd/MM/yyyy"));
}
}
}

0 comments on commit d23046c

Please sign in to comment.