Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for time expressions ("from now", "in", "since", "ago") #816

Open
MrGrabazu opened this issue May 3, 2019 · 3 comments · May be fixed by #875
Open

Support for time expressions ("from now", "in", "since", "ago") #816

MrGrabazu opened this issue May 3, 2019 · 3 comments · May be fixed by #875

Comments

@MrGrabazu
Copy link

Hi,

Thank you for this great library ! Such a usefull tool !

I was thinking maybe it will be a great add to be able to choose between "since" and "ago" in order to express ifferents meanings for past expression and be able to choose between "from now" and "in" in order to express differents meanings for future expression.

  • since express that whatever it is it is what it is since X time (ex: since one year)
  • ago express that whatever it is it was what it was X time ago (ex: one year ago) (and may change since then)
  • from now express that whatever it is it will be what it is X time from now (ex: 1 hour from now)
  • in express that whatever it is it will be what it will be in X time (ex: in 1 hour) (and may be different before)

Today there is no options it's "ago" and that's all:
DateTime.UtcNow.AddHours(-30).Humanize() => "yesterday"
DateTime.UtcNow.AddHours(-2).Humanize() => "2 hours ago"

But maybe it can be more with a choice between "since" and "ago":
DateTime.UtcNow.AddHours(-30).Humanize(PastTimeExpression.Ago) => "yesterday"
DateTime.UtcNow.AddHours(-2).Humanize(PastTimeExpression.Ago) => "2 hours ago"
DateTime.UtcNow.AddHours(-30).Humanize(PastTimeExpression.Since) => "since yesterday"
DateTime.UtcNow.AddHours(-2).Humanize(PastTimeExpression.Since) => "since 2 hours"

Today there is no options it's "from now" and that's all:
DateTime.UtcNow.AddHours(30).Humanize() => "tomorrow"
DateTime.UtcNow.AddHours(2).Humanize() => "2 hours from now"

"tomorrow" is a specific case because in a way to express "from now" it should be "until tomorrow" because only "tomorrow" is more compatible with a "in" case

But maybe it can be more with a choice between "from now" and "in":
DateTime.UtcNow.AddHours(30).Humanize(FutureTimeExpression.FromNow) => "until tomorrow"
DateTime.UtcNow.AddHours(2).Humanize(FutureTimeExpression.FromNow) => "2 hours from now"
DateTime.UtcNow.AddHours(30).Humanize(FutureTimeExpression.In) => "tomorrow"
DateTime.UtcNow.AddHours(2).Humanize(FutureTimeExpression.In) => "in 2 hours"

Some examples :
"Your flight will take off in 2 hours"
"Our plane is flying since 4 hours"
"We will waiting our next flight 3 hours from now"
"We land 1 hour ago"

I think it's not easy to add it but it can be a great add to allow to express those differents meanings with time expressions.

Thank you

@luqp
Copy link

luqp commented Oct 31, 2019

Hi, good suggestion, but I found 2 problems:

First

I think we can not leave the user defines the time expression directly in the Humanize() method.
One case is that user the could define a future time expression while the DateTime value is in the past, like this:

DateTime.UtcNow.AddHours(-2).Humanize(FutureTimeExpression.FromNow) => "???"

My proposal is create a provider class (DateTimeExpressionProvider),
in which the user can overwrite only the time expression he wants:

    public class DateTimeExpressionProvider
    {
        public TimeExpressionFuture GetFutureTimeExpression() => throw null;

        public TimeExpressionPast GetPastTimeExpression() => throw null;
    }

    public class CustomTimeExpressionProvider : DateTimeExpressionProvider
    {
        public override TimeExpressionFuture GetFutureTimeExpression()
        {
            return TimeExpressionFuture.FromNow;
        }

        public override TimeExpressionPast GetPastTimeExpression()
        {
            return TimeExpressionPast.Ago;
        }
    }

Then pass it to Humanize() who will decide which method to call.

    var customTimeExpressionProvider = new CustomTimeExpressionProvider()
    DateTime.UtcNow.AddHours(-2).Humanize(customTimeExpressionProvider) =>2 hours ago”

customTimeExpressionProvider is an optional parameter.

Second

I researched and I think since 2 hours is grammatically incorrect.
Looks like since is used to refer to the starting point of actions or situations, which means specific dates.

    “since 9 o’clock”

And the DateTime.Humanize() does not result on specific dates but on a period of time, which could be adapted to use for and in time expressions, eg:

    “for 2 hours” or “in 2 hours”

For these reasons, I think TimeExpressionFuture and TimeExpressionPast must have predefined values as you said.

I would like to open a discussion about the use of since and other possible time expressions.
In the meanwhile, I prepared a pull request that cover all this.

Pull request

@MrGrabazu
Copy link
Author

It would be perfect to do that! Thanks a lot!

@graphicsxp
Copy link

Just came across this. When this feature is implemented, would I be able to call the static method :

DateHumanizeExtensions.Humanize(myDate, DateTime.Now);

and be able to have future value result as "in 2 hours" instead of "2 hours from now" ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants