Giter Site home page Giter Site logo

timeago's Introduction

timeago - A time formatting package

Install

go get github.com/xeonx/timeago

Docs

You can see the docs page here

Use

package main

import (
 "time"

 "github.com/xeonx/timeago"
)

func main() {
 t := time.Now().Add(42 * time.Second)

 // 's' will contain "less than a minute ago"
 s := timeago.English.Format(t)

 //...
}

Tests

go test is used for testing.

timeago's People

Contributors

aerialcombat avatar huacnlee avatar luisnquin avatar shazow avatar simonhege avatar tpisani avatar umutuluer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

timeago's Issues

Support plurality rules

This package should support more plurality rules. You can use plural package for that.

In some languages, it's not as easy as one or many, but there's also zero (if the language has different ending if it's exactly zero elements), few and many.

For example, Polish requires 4 pluralities: one, few, many and other:

  • 1 miesiąc (one)
  • 2 miesiące (few)
  • 5 miesięcy (many)
  • 1.5 miesiąca (other)

The plural package deals with all the hard stuff by having algorithms to check which plurality to use with a specific number (which for Polish is quite complicated: few is used for numbers ending with 2, 3, 4; unless it's between 12 and 14, etc.) so the only thing you have to do is use the package and pass the number to it and it'll come back with either zero, one, few, many or other

Arbitrary precision

Any interest in adding support for arbitrary precision?

I imagine something like,

func (cfg Config) FormatPrecise(duration time.Duration, precision time.Duration) string

FormatPrecise(60 * time.Second, time.Second) == "60 seconds"
FormatPrecise(60 * time.Second, time.Minute) == "1 minute"
FormatPrecise(90 * time.Second, time.Second) == "1 minute and 30 seconds"
FormatPrecise(12345 * time.Second, time.Second) == "3 hours, 25 minutes, and 45 seconds"

Or perhaps for the sake of cross-language grammar simplicity, just 3 hours 25 minutes 45 seconds.

Alternatively this could be part of Config.

No suffix/prefix mode?

Would you consider adding a mode that does not include a suffix or prefix?

I want to be able to pass 120 and get "2 hours".

Not sure what the best API would be. I would suggest changing FormatDuration to not include the prefix/suffix, and then having something like FormatAdverb which adds the suffix or prefix.

Thoughts?

How can I handle a date quite far away?

I tried to use to convert the data far enough, but did not generate the desired data. I entered 91008167 in seconds. Then I got the result 2021-02-25.

waktu := time.Now().Add(91008167 * time.Second)
waktuString := timeago.English.Format(waktu)
fmt.Println(waktuString)

But if the number 91008167 I change to 9100 can get the expected value. I got the result "in 3 hours".

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.