uniplaces/carbon

Index out of range when using DiffForHumans function

andrewmkano opened this issue · 4 comments

Hey, I was going to use this wonderful function of yours called DIffForHumans but it turns out that It was crashing the application because of an index out of range error. The error says that it occurred while invoking the method chooseUnit for the translator which crashes when calling line 81:

return strings.Replace(s[1], ":count", strconv.FormatInt(int64(count), 10), 1), nil

I looked at the current translator that was associated with the carbon obtained through carbon.NewCarbon(time.Time) and the t.resources map was empty which could have been the cause of the index out of range error.

I'm not sure if this is helpful for you guys but here's basically how I invoked the method:

registeredAt := carbon.NewCarbon(comment.CreatedAt)
currentTime := carbon.NewCarbon(time.Now())

difference, err := registeredAt.DiffForHumans(currentTime,true,false,false)
if err != nil {
 fmt.Println(err.Error())
}

return difference

My apologies if I misunderstood the way this method works or how I should use it, I'm still a little bit unexperienced.

Same issue for me @uniplaces

same here

Same issue.

package main

import (
	"fmt"
	"time"

	"github.com/uniplaces/carbon"
)

func main() {
	a := carbon.NewCarbon(time.Now())
	b := carbon.NewCarbon(time.Now().Add(time.Hour * 10))

	gotTime, err := a.DiffForHumans(b, false, false, false)
	fmt.Println(gotTime, err)
}

Go Playground

Panics on DiffForHumans

This is also related to issue #57

Fix with the new version 0.2.0! The only requirement is to use go >= 1.16