Skip to content

Commit

Permalink
Merge pull request #6 from bolorundurowb/feature/add-second-tranche-o…
Browse files Browse the repository at this point in the history
…f-methods

feature/add calendar time
  • Loading branch information
Bolorunduro Winner-Timothy B committed Apr 21, 2019
2 parents f746647 + eecb6d9 commit 3b39b6b
Show file tree
Hide file tree
Showing 10 changed files with 450 additions and 266 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![Build Status](https://travis-ci.org/bolorundurowb/moment.net.svg?branch=master)](https://travis-ci.org/bolorundurowb/moment.net) [![Coverage Status](https://coveralls.io/repos/github/bolorundurowb/moment.net/badge.svg)](https://coveralls.io/github/bolorundurowb/moment.net) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)


This library aims to port as many bits of functionality from moment.js as is necessary. A few have been ported thus far `FromNow`, `From`, `ToNow`, `To` and `StartOf`.
This library aims to port as many bits of functionality from moment.js as is necessary. A few have been ported thus far `FromNow`, `From`, `ToNow`, `To`, `StartOf` and `CalendarTime``.

## Usages

Expand Down Expand Up @@ -44,3 +44,15 @@ var startOfWeek = date.StartOf(DateTimeAnchor.Week); //27/04/2008 00:00:00" (pr
var startOfMonth = date.StartOf(DateTimeAnchor.Month); //01/05/2008 00:00:00"
var startOfYear = date.StartOf(DateTimeAnchor.Year); //01/01/2008 00:00:00"
```

- CalendarTime
Calendar Time supports creating formats for displaying the return string. The format works in the standard DateTime format string manner

```csharp
var startDateTime = new DateTime(2012, 12, 12);
var sameDay = new DateTime(2012, 12, 12, 12, 0, 0);
var endDateTime = new DateTime(2012, 12, 13);
var calendarTime = startDateTime.CalendarTime(endDateTime); // Tomorrow at 00:00 AM
calendarTime = endDateTime.CalendarTime(startDateTime); // Yesterday at 00:00 AM
calendarTime = startDateTime.CalendarTime(sameDay); // Today at 12:00 PM
```
6 changes: 1 addition & 5 deletions src/DateTimeAnchor.cs → src/Enums/DateTimeAnchor.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace moment.net
namespace moment.net.Enums
{
public enum DateTimeAnchor
{
Expand Down
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;
}
}
}
55 changes: 53 additions & 2 deletions src/RelativeTime.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using moment.net.Models;
using System.Globalization;
using moment.net.Enums;

namespace moment.net
{
Expand Down Expand Up @@ -60,7 +62,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 @@ -99,6 +101,56 @@ public static string To(this DateTime This, DateTime dateTime)
return ParseFromFutureTimeSpan(endDate - startDate);
}

/// <summary>
/// Get the calendar time description from this DateTime instance to the current time
/// </summary>
/// <param name="This">The date instance which to compare with the current date</param>
/// <param name="formats">An object describing how the output string should be displayed</param>
/// <returns></returns>
public static string CalendarTime(this DateTime This, CalendarTimeFormats formats = null)
{
return CalendarTime(This, DateTime.Now, formats);
}

/// <summary>
/// Get the calendar time description from this DateTime instance to a specified DateTime instance
/// </summary>
/// <param name="This">The date instance which to start comparison from</param>
/// <param name="dateTime">The date instance to compare to</param>
/// <param name="formats">An object describing how the output string should be displayed</param>
/// <returns></returns>
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 Expand Up @@ -178,7 +230,6 @@ private static string ParseTimeDifference(TimeSpan timeSpan)
"in The time span sent could not be parsed.");
}


/// <summary>
/// Returns the first day of the week for the given date and culture info
/// The returned first day of the week will vary based on the supplied culture info
Expand Down
8 changes: 4 additions & 4 deletions src/moment.net.csproj
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard1.1;netstandard1.3;netstandard1.6;netstandard2.0</TargetFrameworks>
<PackageVersion>1.0.0-alpha</PackageVersion>
<TargetFrameworks>netstandard1.1;netstandard2.0</TargetFrameworks>
<PackageVersion>1.0.0</PackageVersion>
<Title>moment.net</Title>
<Authors>bolorundurowb</Authors>
<Authors>bolorundurowb YemiKudaisi</Authors>
<Description>A library aimed at adding in some relevanyt fundtionality as seen in moment.js</Description>
<Copyright>(c) 2019</Copyright>
<PackageProjectUrl>https://github.com/bolorundurowb/moment.net</PackageProjectUrl>
<PackageLicenseUrl>https://github.com/bolorundurowb/moment.net/blob/master/LICENSE</PackageLicenseUrl>
<RepositoryUrl>https://github.com/bolorundurowb/moment.net</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>moment datetime relative time</PackageTags>
<PackageReleaseNotes>First pre release</PackageReleaseNotes>
<PackageReleaseNotes>First full release.</PackageReleaseNotes>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
</PropertyGroup>

Expand Down
55 changes: 55 additions & 0 deletions tests/CalendarTime.Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using moment.net.Models;
using NUnit.Framework;
using Shouldly;

namespace moment.net.Tests
{
public class CalendarTimeTests
{
[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 3b39b6b

Please sign in to comment.